如何在 Hibernate/javax.persistance 中映射一个具有多个表的类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22668350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to map one class with multiple tables in Hibernate/javax.persistance?
提问by Muhammad Bilal Hasan
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
. It allows me to define only one secondary table. But I need 3 tables to be used by the same class.
以下是我使用过的代码@SecondaryTable
。它允许我只定义一个辅助表。但是我需要 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;
回答by user3028989
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. Like that you may do multiple mappings. While loading the object use entityname (virtual name).
在 Hibernate 映射文件中,您可以指定带有虚拟名称的实体名称映射,polymorphism="explicit"
并且类名称将是物理类名称。像这样你可以做多个映射。加载对象时使用 entityname(虚拟名称)。
回答by Abhishek Nayak
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 @SecondaryTablesto map more than one table.
使用@SecondaryTables来映射多个表。
You can map a single entity bean to several tables using the @SecondaryTables
class level annotations. To express that a column is in a particular table, use the table parameter of @Column
or @JoinColumn
.
您可以使用@SecondaryTables
类级别注释将单个实体 bean 映射到多个表。要表示某列在特定表中,请使用@Column
or的表参数@JoinColumn
。
for example there is 3 entity's namely: Name
, Address
& Student
:
例如,有 3 个实体,即:Name
,Address
& Student
:
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
回答by Aaron
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.
他们都将共享主键值。希望这会帮助你。