[英]hibernate one to many single direction
I'm trying to make some hibernate stuff and create sql scripts automatically based on the hibernate annotations. 我正在尝试制作一些休眠的东西,并基于休眠的注释自动创建sql脚本。 Here is what I have: 这是我所拥有的:
Schedule class: 时间表课程:
@Entity
@Table(name = ScheduleTableConsts.TABLE_NAME)
public class Schedule implements Serializable {
@Id
@Column(name = ScheduleTableConsts.ID_COLUMN)
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name = ScheduleSlotTableConsts.ID_COLUMN)
private List<ScheduleSlot> scheduleSlots;
@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name = LessonTableConsts.ID_COLUMN)
private List<Lesson> lessons;
Constructors, getters, setters...
ScheduleSlot class: ScheduleSlot类:
@Entity
@Table(name = ScheduleSlotTableConsts.TABLE_NAME,
uniqueConstraints = {@UniqueConstraint(columnNames = {TimeSlotTableConsts.ID_COLUMN,
PlaceSlotTableConsts.ID_COLUMN})})
public class ScheduleSlot implements Serializable {
@Id
@Column(name = ScheduleSlotTableConsts.ID_COLUMN)
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@OneToOne
@JoinColumn(name = TimeSlotTableConsts.ID_COLUMN)
private TimeSlot timeSlot;
@OneToOne
@JoinColumn(name = PlaceSlotTableConsts.ID_COLUMN)
private PlaceSlot placeSlot;
Constructors, getters, setters...
Lesson class: 课程课:
@Entity
@Table(name = LessonTableConsts.TABLE_NAME)
public class Lesson implements Serializable {
@Id
@Column(name = LessonTableConsts.ID_COLUMN)
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@OneToMany(fetch = FetchType.LAZY)
private Set<Professor> professors;
@OneToMany(fetch = FetchType.LAZY)
private Set<Course> courses;
@OneToMany(fetch = FetchType.LAZY)
private Set<Group> groups;
@Column(name = LessonTableConsts.LAB_COLUMN)
private boolean lab;
Constructors, getters, setters...
What I'm trying to achieve is to let schedule know about it's slots and lessons and not to let slots and lessons know about the schedule they are belong to. 我要实现的目标是让日程表知道它的空位和课程,而不是让它们知道它们所属的日程表。 The code above seems to be ok, but when I'm trying to generate sql script (using maven and hibernate3-maven-plugin for that purposes) and run it the following happens: 上面的代码似乎还可以,但是当我尝试生成sql脚本(为此目的使用maven和hibernate3-maven-plugin)并运行它时,会发生以下情况:
Could somebody, please, tell me what I am doing wrong? 有人可以告诉我我做错了吗?
Thank in advance! 预先感谢!
There is a thing you are forgetting to notice: 您会忘记一件事:
When using @OneToMany
annotation, you cannot simply use @JoinColumn
. 使用@OneToMany
注释时,不能简单地使用@JoinColumn
。 However, on the other side, I mean @ManyToOne
: you can use @JoinColumn
. 但是,另一方面,我的意思是@ManyToOne
:您可以使用@JoinColumn
。
It should be clear to you why this works this way, and not the other way around. 您应该清楚为什么会这样,而不是相反。 (How can an entity keep so many IDs in just one column?) (实体如何仅在一列中保留这么多ID?)
So you have two options here: 因此,您在这里有两个选择:
1) Use a specific table to store relation between your One
and Many
entities. 1)使用特定的表存储您的One
和Many
实体之间的关系。 Something like this: 像这样:
@OneToMany(cascade=CascadeType.ALL)
@JoinTable(name = "SCHEDULE_SLOTS_MAPPING", joinColumns = @JoinColumn(name = "SCHEDULE_ID"), inverseJoinColumns = @JoinColumn(name = "SCHEDULE_SLOT_ID"))
private List<ScheduleSlot> scheduleSlots;
2) Use the @JoinColumn
on the Many
side of the relationship: (eg your ScheduleSlot
class) 2)使用@JoinColumn
对Many
关系的一面:(例如,您ScheduleSlot
类)
@ManyToOne
@JoinColumn(name="SCHEDULE_ID")
private Schedule schedule;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.