简体   繁体   English

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

[英]How to map one class with multiple tables in Hibernate/javax.persistance?

I want to use one class to map three tables. 我想用一个类来映射三个表。 I know javax.persistance provides the @SecondaryTable annotation to map two tables to one class. 我知道javax.persistance提供了@SecondaryTable注释来将两个表映射到一个类。

Below is the code, where I have used @SecondaryTable . 下面是我使用@SecondaryTable的代码。 It allows me to define only one secondary table. 它允许我只定义一个辅助表。 But I need 3 tables to be used by the same class. 但是我需要同一个类使用3个表。

@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;

I want to use one class to map three tables, From what I know is that javax.persistance provides @SecondaryTable annotation to map two tables to one class 我想使用一个类来映射三个表,据我所知,javax.persistance提供了@SecondaryTable注释来将两个表映射到一个类

use @SecondaryTables to map more than one table. 使用@SecondaryTables映射多个表。

You can map a single entity bean to several tables using the @SecondaryTables class level annotations. 您可以使用@SecondaryTables类级别注释将单个实体bean映射到多个表。 To express that a column is in a particular table, use the table parameter of @Column or @JoinColumn . 要表示列在特定表中,请使用@Column@JoinColumn的table参数。


for example there is 3 entity's namely: Name , Address & Student : 例如,有3个实体即: NameAddressStudent

Name entity will look like: 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 will look like: 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 will look like: 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
}

Store like: 存储如:

    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());

ouput: 输出继电器:

1
Bilal Hasan
India

you can define one class like below : 你可以定义一个类,如下所示:

@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...
}

In your DB, should has three table, and all of them should has the same primary key "id". 在你的数据库中,应该有三个表,并且它们都应该具有相同的主键“id”。

then, use can test like this: 然后,使用可以像这样测试:

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

em.merge(test);

after test, in your DB, you will find one record in each table: 在测试之后,在您的数据库中,您将在每个表中找到一条记录:

table1: 表格1:

 1, field1

table2: 表2:

 1, field2

table3: 表3:

 1, field3

all of them will share the primary key value. 所有这些都将共享主键值。 Hope this will help you. 希望这会帮助你。

In Hibernate mapping file you can specify the entity-name mapping with virtual name along with polymorphism="explicit" and class name would be physical class name. 在Hibernate映射文件中,您可以使用虚拟名称指定实体名称映射以及polymorphism="explicit" ,类名称将是物理类名称。 Like that you may do multiple mappings. 像那样你可以做多个映射。 While loading the object use entityname (virtual name). 加载对象时使用entityname(虚拟名称)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM