Hibernate 注释与集合
我正在尝试使用休眠注释来实现我的模型。我有 3 个类:图像、人物和标签。 Tags 是一个由 4 个字段组成的表,id、personId、imageId 和createdDate。 Person 具有名称、id、出生日期等字段。我的图像类定义如下:
@Entity
@Table(name="Image")
public class Image {
private Integer imageId;
private Set<Person> persons = new HashSet<Person>();
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID")
public Integer getImageId() {
return imageId;
}
public void setImageId(Integer imageId) {
this.imageId = imageId;
}
@ManyToMany
@JoinTable(name="Tags",
joinColumns = {@JoinColumn(name="imageId", nullable=false)},
inverseJoinColumns = {@JoinColumn(name="personId", nullable=false)})
public Set<Person> getPersons() {
return persons;
}
public void setPersons(Set<Person> persons) {
this.persons = persons;
}
如果删除 getPersons() 方法上的注释,我可以使用这些类并添加和删除记录。我想获取带有图像的所有标签,并且我正在尝试使用一组标签。我不断收到以下错误:
org.hibernate.LazyInitializationException - failed to lazily initialize a collection of role: com.exmaple.persons, no session or session was closed
有人可以帮助我并让我知道我做错了什么吗?
谢谢
I am trying to implement my model using hibernate annotations. I have 3 classes, image, person, and tags. Tags is a a table consisting of 4 fields, an id, personId, imageId, and a createdDate. Person has the fields name, id, birthdate, etc. My image class is defined as follows:
@Entity
@Table(name="Image")
public class Image {
private Integer imageId;
private Set<Person> persons = new HashSet<Person>();
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID")
public Integer getImageId() {
return imageId;
}
public void setImageId(Integer imageId) {
this.imageId = imageId;
}
@ManyToMany
@JoinTable(name="Tags",
joinColumns = {@JoinColumn(name="imageId", nullable=false)},
inverseJoinColumns = {@JoinColumn(name="personId", nullable=false)})
public Set<Person> getPersons() {
return persons;
}
public void setPersons(Set<Person> persons) {
this.persons = persons;
}
If I remove the annotations on the getPersons() method I can use the classes and add and remove records. I want to fetch all the tags with the image and I am trying to use a set. I keep getting the following error:
org.hibernate.LazyInitializationException - failed to lazily initialize a collection of role: com.exmaple.persons, no session or session was closed
Can someone please help me and let me know what I am doing wrong?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此错误消息(实际上与您的关联映射策略或注释无关)意味着您在
Session
关闭后尝试访问域对象之一上的延迟加载集合。解决方案是禁用此集合的延迟加载,在
Session
关闭之前显式加载该集合(例如,通过调用foo.getBars().size()
),或者确保Session
保持打开状态,直到不再需要为止。如果您不确定延迟加载是什么,这是 Hibernate 手册中的部分。
This error message - which actually has nothing to do with your association mapping strategy or annotations - means that you have attempted to access a lazy-loaded collection on one of your domain objects after the
Session
was closed.The solution is to either disable lazy-loading for this collection, explicitly load the collection before the
Session
is closed (for example, by callingfoo.getBars().size()
), or making sure that theSession
stays open until it is no longer needed.If you are not sure what lazy-loading is, here is the section in the Hibernate manual.
感谢马特的回复。我现在很困惑。我检索图像的查询如下所示:
我认为我可以调用单个 hql 查询,如果我的映射正确,它将带回关联的数据。
我正在这个链接中查看这个示例 hibernate映射:
Thanks for the response matt. I am confused now. My query to retrieve the image looks like this:
I thought that I can call the single hql query and if my mappings are correct it will bring back the associated data.
I was looking at this example at this link hibernate mappings: