简体   繁体   English

Java持久性的单向oneToMany关系

[英]unidirectional oneToMany relation with java persistence

I have a class TimeTable that contains a set of objects of type Year . 我有一个TimeTable类,其中包含Year类型的一组对象。 A Year does not have any referene to any TimeTable . Year没有引用任何TimeTable

On my database the relation is the other way 'round: In the YEARS table is a foreign key TIME_TABLE_ID . 在我的数据库中,这种关系是相反的:在YEARS表中是外键TIME_TABLE_ID

I'd now like to add my persistence annotations to those classes, but I can't find out the exact syntax for pure JPA (only for Hibernate which I don't want to use at the moment). 现在,我想将持久性注释添加到这些类中,但是我无法找到纯JPA的确切语法(仅适用于目前不希望使用的Hibernate)。

I think it could be sth like this: 我认为可能是这样的:

class TimeTable{
....
@OneToMany
@JoinTable(name = "YEARS", 
  joinColumns = @JoinColumn(name = "TIME_TABLE_ID"), 
  inverseJoinColumns = @JoinColumn(name ="ID" ???))  // #### here ####
private Set<Year> years;
...
}

.. but I can't find out what this inverseJoinColumn is :( Is it the name of the ID of an entry in the TIMETABLE table? ..但我找不到这个inverseJoinColumn是什么:(这是TIMETABLE表中条目ID的名称吗?

ID对应于Year实体中的主键

The inverse join column is optional, I would recommend removing it, since your setting up a one to many relationship and it is not needed. 反向连接列是可选的,我建议将其删除,因为您无需设置一对多关系。 The @JoinTable annotation is primarily used for mapping many to many relationships, a scenario you do not need to account for. @JoinTable批注主要用于映射多对多关系,您无需考虑这种情况。 Instead of @JoinTable use @JoinColumn to map this one to many relationship. 代替@JoinTable使用@JoinColumn将这一关系映射到许多关系。

class TimeTable{
....
@OneToMany
@JoinColumn(name = "TIME_TABLE_ID"); 
private Set<Year> years;
...
}

http://www.objectdb.com/api/java/jpa/JoinColumn http://www.objectdb.com/api/java/jpa/JoinColumn

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

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