如何在Hibernate/javax.persistance中映射一个具有多个表的类?

2022-09-01 20:38:52

我想使用一个类来映射三个表。我知道javax.persistance提供了将两个表映射到一个类的注释。@SecondaryTable

下面是我使用的代码。它只允许我定义一个辅助表。但是我需要3个表供同一类使用。@SecondaryTable

@Entity
@Table(name = "table1")
@SecondaryTable(name="table2")
public class TableConfig
    implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "mac", table= "table1")
    private String uniqueIdentifier;

答案 1

我想使用一个类来映射三个表,据我所知,javax.persistance提供了@SecondaryTable注释,可以将两个表映射到一个类

使用@SecondaryTables映射多个表。

您可以使用类级批注将单个实体 Bean 映射到多个表。要表示某列位于特定表中,请使用 的 table 参数 。@SecondaryTables@Column@JoinColumn


例如,有 3 个实体,即: 、 & :NameAddressStudent

Name实体将如下所示:

@Entity
@Table(name="name")
public class Name implements Serializable {

    @Id
    @Column(name="id")
    private int id;
    @Column(name="name")
    private String name;

    public Name(){}

    public Name(int id,String name){
        this.id=id;
        this.name=name;
    }
        //getters and setters
}

Address实体将如下所示:

@Entity
@Table(name="address")
public class Address implements Serializable {

    @Id
    @Column(name="id")
    private int id;
    @Column(name="address")
    private String address;

    public Address(){}

    public Address(int id, String address) {
        super();
        this.id = id;
        this.address = address;
    }
        //getters and setters
}

Student实体将如下所示:

@Entity
@Table(name="student")
@SecondaryTables({
    @SecondaryTable(name="name", pkJoinColumns={
        @PrimaryKeyJoinColumn(name="id", referencedColumnName="student_id") }),
    @SecondaryTable(name="address", pkJoinColumns={
        @PrimaryKeyJoinColumn(name="id", referencedColumnName="student_id") })
})
public class Student implements Serializable {

    @Id
    @Column(name="student_id")
    private int studentId;

    @Column(table="name")
    private String name;

    @Column(table="address")
    private String address;

    public Student(){}

    public Student(int studentId){
        this.studentId=studentId;
    }
        //getters and setters
}

商店喜欢:

    Student s= new Student(1);
    session.save(s);

    Name n=new Name(s.getStudentId(),"Bilal Hasan");
    session.save(n);    

    Address address = new Address(s.getStudentId(), "India");
    session.save(address);

    Student ob = (Student)session.get(Student.class, s.getStudentId());

    System.out.println(ob.getStudentId());
    System.out.println(ob.getName());
    System.out.println(ob.getAddress());

输出:

1
Bilal Hasan
India

答案 2

你可以定义一个类,如下所示:

@Entity
@Table(name="table1")
@SecondaryTables({
      @SecondaryTable(name="table2", pkColumnJoins={@PrimaryKeyJoinColumn(name = "id")}),
      @SecondaryTable(name="table3", pkColumnJoins={@PrimaryKeyJoinColumn(name = "id")})
  })
public class TestEntity {
      @Id
      @GeneratedValue
      private int id;

      private String field1;

      @Column(name="column2", table="table2")
      private String field2;

      @Column(name="column3", table="table3")
      private String field3;

      getter and setter...
}

在数据库中,应该有三个表,并且所有表都应该具有相同的主键“id”。

然后,使用可以像这样测试:

TestEntity test = new TestEntity();
test.setField1("field1");
test.setField2("field2");
test.setField3("field3");

em.merge(test);

测试后,在您的数据库中,您将在每个表中找到一条记录:

表 1:

 1, field1

表 2:

 1, field2

表 3:

 1, field3

它们都将共享主键值。希望这会帮助你。


推荐