我是Hibernate的新手,所以我的問題可能會很愚蠢,因爲我被困住了,很樂意獲得幫助。休眠OneToMany。從數據庫加載時獲取空列表
我有兩個實體:書和標籤具有以下結構:
@Entity
public class BookEntity{
@Column(nullable = false)
private String title;
@Column(nullable = false)
private String author;
private String publisher;
private int edition;
private int yearOfPublishing;
@Id
@Column(name = "isbn")
private String isbn;
@ElementCollection(fetch = FetchType.EAGER)
@OneToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
@JoinTable(joinColumns = { @JoinColumn(name = "isbn") },
inverseJoinColumns = { @JoinColumn(name = "tagId") })
private List<Tag> tags;
//getters & setters
@Entity
public class Tag implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int tagId;
private String tagValue;
//getters & setters
插入正常工作,這裏是HQL查詢:
insert into PUBLIC.BookEntity
(author, edition, publisher, title, yearOfPublishing, isbn)
values (?, ?, ?, ?, ?, ?)
insert into PUBLIC.Tag
(tagId, tagValue)
values (null, ?)
選擇查詢看起來不錯還有:
select
bookentity0_.isbn as isbn35_1_,
bookentity0_.author as author35_1_,
bookentity0_.edition as edition35_1_,
bookentity0_.publisher as publisher35_1_,
bookentity0_.title as title35_1_,
bookentity0_.yearOfPublishing as yearOfPu6_35_1_,
tags1_.isbn as isbn35_3_,
tag2_.tagId as tagId3_,
tag2_.tagId as tagId36_0_,
tag2_.tagValue as tagValue36_0_
from
PUBLIC.BookEntity bookentity0_
left outer join
PUBLIC.BookEntity_Tag tags1_
on bookentity0_.isbn=tags1_.isbn
left outer join
PUBLIC.Tag tag2_
on tags1_.tagId=tag2_.tagId
where
bookentity0_.isbn=?
但是,當我從數據庫中加載BookEntity我得到正確的對象與標籤的空列表。 從數據庫加載對象:
public T read(PK id) {
LOG.debug("Reading by id={}", id.toString());
return (T)getSession().get(type, id);
}
其中T是BookEntity,類型爲類和PK是字符串。
我在做什麼錯了? 在此先感謝。
插入到'BookEntity_Tag'連接表中的位置在哪裏? – millhouse