简体   繁体   中英

unidirectional oneToMany relation with java persistence

I have a class TimeTable that contains a set of objects of type Year . A Year does not have any referene to any TimeTable .

On my database the relation is the other way 'round: In the YEARS table is a foreign key 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).

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?

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. Instead of @JoinTable use @JoinColumn to map this one to many relationship.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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