我使用嵌入式註解在JPA努力之間的關係,但我不能夠成功運行,@ElementCollection @CollectionTable在一對多映射
這裏我的數據庫SQL腳本如下,
create table TBL_COLLEGE(
id integer primary key generated always as identity (start with 1000, increment by 5),
name varchar(50)
)
create table TBL_COURSE(
Id integer primary key generated always as identity (start with 10, increment by 1),
college_Id integer references TBL_COLLEGE,
name varchar(50)
)
這裏是下面的代碼爲JPA,
@Embeddable
public class Course {
...
...
..
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
@Column(name="ID")
private Integer courseId;
@Column(name="NAME")
private String courseName;
@Column(name="COLLEGE_ID")
private Integer collegeId;
....
// getter and setter
}
這裏是學院的映射,
@Entity
@Table(name="TBL_COLLEGE")
public class College implements Serializable{
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
@Column(name="ID")
private Integer collegeId;
...
..
@ElementCollection(targetClass=Course.class,fetch= FetchType.LAZY)
@CollectionTable(name="TBL_COURSE",[email protected](name="COLLEGE_ID"))
private Set<Course> course;
..
// getter and setter
}
但如果我嘗試堅持與大學課程收集組, 它給了我一個例外,
ERROR: HCANN000002: An assertion failure occurred (this may indicate a bug in Hibernate)
org.hibernate.annotations.common.AssertionFailure: Declaring class is not found in the inheritance state hierarchy: com.entities.Course
....
..
能否請你告訴我,我的做法是否是錯誤的, 還是我的@CollectionTable理解仍然微乎其微, 是我錯了
沒有必要定義一對多註釋等。這足以從embedde刪除ID註釋d類。當然,GeneratedValue註解也應該被移除。 – asch 2016-12-19 14:19:36
@asch - 如果您認爲OP不希望課程成爲頭等對象,那麼這是真的。根據定義的表格,刪除ID註釋會導致問題。 – sharakan 2017-01-03 04:44:24
課程已經註解爲Embeddable,所以它不是實體(我想你的意思是「第一類對象」) – asch 2017-01-03 06:52:58