Java Hibernate(JPA)针对同一型号的多个@OneToMany
我有两个模型Java Hibernate(JPA)针对同一型号的多个@OneToMany,java,hibernate,jpa,one-to-many,Java,Hibernate,Jpa,One To Many,我有两个模型 @Entity public class Student { @Id @GeneratedValue(strategy=GenerationType.AUTO) protected long id; @? protected Address homeAddress; @? protected Address schoolAddress; } @Entity public class Address { @Id
@Entity
public class Student
{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
protected long id;
@?
protected Address homeAddress;
@?
protected Address schoolAddress;
}
@Entity
public class Address
{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
protected long id;
@?
protected List<Student> students;
}
及
使用这种方法,在数据库中存储模型时,即使在存储了两端(学生和地址模型)之后,学生家庭地址id和学生学校地址id也始终为空
我的想法是,在Address
表上会有3个额外的列:student\u homemaddress\u id(学生表中学生的id表示家庭地址)、student\u schoolAddress\u id(学生表中学生的id表示学校地址)和students\u index(学生列表中基于0的位置)。那就足够了,对吗
有什么想法吗
非常感谢 如果您有一个实体要在多个字段中与@OneToMany关联,则应使用@JoinTable,以便Hibernate可以为该关系生成两个表
从@JoinTable javadoc:
联接表通常用于多对多和多对多的映射
单向一对多关联。它也可以用来绘制地图
双向多对一/一对多关联,单向
多对一关系和一对一关联(两者都是
双向和单向)
当连接表用于映射与可嵌入对象的关系时
在关系的拥有方(包含实体)上初始化
而不是可嵌入类被认为是
关系
如果缺少JoinTable注释,则
应用注释元素。联接表的名称假定为
连接在一起的关联主表的表名
(首先是拥有方)使用下划线
举个例子:
@Entity
@Indexed(index = "causa_penal")
@Table(name = "causas_penales")
public class CausaPenal implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
@Column(name = "id")
@DocumentId
private Integer id;
@Version
@Column(name = "opt_lock")
private Integer version;
@ManyToMany(cascade = { CascadeType.ALL })
@JoinTable(name = "causapenal_imputados")
@IndexedEmbedded(depth = 1)
private List<ParteMaterial> imputados;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "causapenal_victimas")
@IndexedEmbedded(depth = 1)
private List<ParteMaterial> victimas;
//Getters and Setters
}
@实体
@索引(index=“原因”)
@表(name=“causas_penales”)
公共类CausaPenal实现了可序列化{
@身份证
@GeneratedValue(策略=GenerationType.TABLE)
@列(name=“id”)
@文档ID
私有整数id;
@版本
@列(name=“opt_lock”)
私有整数版本;
@ManyToMany(cascade={CascadeType.ALL})
@可接合(name=“causapenal\u imputados”)
@已嵌入索引(深度=1)
私人名单;
@多个(级联=级联类型.ALL)
@JoinTable(name=“causapenal_victimas”)
@已嵌入索引(深度=1)
维克蒂马斯私人名单;
//接球手和接球手
}
正如你们所看到的,我有两次将因果关系指向partemail。但我需要这些列表相互独立。因此,对于@JoinTable,我告诉Hibernate,这个关系必须映射为4个表:“causa_penal”用于CausaPenal实体,“causa_penal_imputados”用于CausaPenal和imputados字段的关系,该字段映射到一个ParteMeterial实体,对于victimas也是如此,最后,用于partemantial实体的表。我们尝试了mael的建议,但无法使其生效
我们最终遵循了有效的方法
换句话说,我们有OneToMany
关系:
在学生
上:
protected List<AddressStudentAssociation> addresses;
在地址学生协会上
:
@ManyToOne
@PrimaryKeyJoinColumn(name="STUDENTID", referencedColumnName="id")
private Student student;
@ManyToOne
@PrimaryKeyJoinColumn(name="ADDRESSID", referencedColumnName="id")
private Address address;
加上一个参数,将一个地址与另一个地址分开(iHome
)
最后,在Student
中,我们有public Address getHomeAddress()
,它遍历addresses
列表并返回正确的地址。我们还必须使用注释才能使其正常工作。一般来说不是最优的,但它是有效的,我们已经花了太多的时间试图让事情顺利进行|
@Entity
@Indexed(index = "causa_penal")
@Table(name = "causas_penales")
public class CausaPenal implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
@Column(name = "id")
@DocumentId
private Integer id;
@Version
@Column(name = "opt_lock")
private Integer version;
@ManyToMany(cascade = { CascadeType.ALL })
@JoinTable(name = "causapenal_imputados")
@IndexedEmbedded(depth = 1)
private List<ParteMaterial> imputados;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "causapenal_victimas")
@IndexedEmbedded(depth = 1)
private List<ParteMaterial> victimas;
//Getters and Setters
}
protected List<AddressStudentAssociation> addresses;
protected List<AddressStudentAssociation> students;
@ManyToOne
@PrimaryKeyJoinColumn(name="STUDENTID", referencedColumnName="id")
private Student student;
@ManyToOne
@PrimaryKeyJoinColumn(name="ADDRESSID", referencedColumnName="id")
private Address address;