hibernate manytomany
public class Tag implements Serializable {
private ArrayList<Advertisement> advertisements = new ArrayList<Advertisement>();
@ManyToMany(mappedBy="tags")
public ArrayList<Advertisement> getAdvertisements() {
return advertisements;
}
在Advertisement中:
@Entity
public class Advertisement implements Serializable {
private Set<Tag> tags = new HashSet<Tag>();
@ManyToMany
@JoinTable(name = "advertisement_tag",
joinColumns={@JoinColumn(name="advertisementId")},
inverseJoinColumns={@JoinColumn(name="tagId")})
public Set<Tag> getTags() {
return tags;
}
}
测试方法:
Tag t = (Tag) dao.find(2);
//查找已有的tag
Advertisement a = (Advertisement) dao1.find(4);
//查找已有的广告帖
a.getTags().add(t);
//在广告帖的标签list中添加t
dao1.update(a);
//更新到数据库中
此时会抛出org.hibernate.LazyInitializationException: failed to lazily initialize a collect
的错:
这个异常大致意思:在多对一的时候(并且lazy="false"),对象的实例失败。
所以应该是默认为lazy加载,于是设置lazy=false应该就可以了。
修改如下:
在Tag中:
public class Tag implements Serializable {
private ArrayList<Advertisement> advertisements = new ArrayList<Advertisement>();
@ManyToMany(mappedBy="tags",fetch=FetchType.EAGER
)
public ArrayList<Advertisement> getAdvertisements() {
return advertisements;
}
在Advertisement中:
@Entity
public class Advertisement implements Serializable {
private Set<Tag> tags = new HashSet<Tag>();
@ManyToMany(fetch=FetchType.EAGER
)
@JoinTable(name = "advertisement_tag",
joinColumns={@JoinColumn(name="advertisementId")},
inverseJoinColumns={@JoinColumn(name="tagId")})
public Set<Tag> getTags() {
return tags;
}
}
分享到:
相关推荐
`lazily`:金鱼慵懒地(lazily)成群结队地在水面下游动。 - 15. `coats`:他们给前门刷了三层(coats)油漆。 2. **关键短语或词组**: - 1. `come along`:一起来,过来。 - 2. `a novel called…`:一本叫做...
3. 冠词:"a good worker",不定冠词a用于表示泛指,此处表示“一个好工人”。 4. 形容词与过去分词的区别:"Although surprised",形容词surprised用于表示人的感受。 5. 连词:"because he really needed help",...
- **.NET as a Reaction to the Java World**: Microsoft’s response to Sun Microsystems’ Java platform, aiming to provide a robust framework for building and running applications. - **The Open Source...
Kodein is a very simple and yet very useful dependency retrieval container. it is very easy to use and configure. Kodein works: On the JVM. On Android. On Javascript (both in the browser and on Node....
10. **性能优化**:使用`requestAnimationFrame`进行动画更新,或者利用`Lazily Load Images`等策略提高网站加载速度,都是JavaScript性能优化的常见手段。 11. **错误处理**:为了确保网站的健壮性,开发者可能...
- 完成句子练习可以帮助巩固词汇的应用,如:"The young man rose lazily and was not willing to greet us."(年轻人懒洋洋地起身,并不愿意向我们打招呼。),"This appointment was greeted with relief."(这个...
/ specific area difficult, so I lazily redraw almost the whole control whenever / it needs. This results in the slowness (I suggest you can use the control in data / display, ^_^). In next version,...
// Subscribes to a Pub/Sub channel using the internal, lazily initialized SubscriberClient redis.subscriber.subscribe("My Channel") { case message @ PubSubMessage.Message(channel, ...
committed, the command line prompt script failed to notice the current status, which has been improved. * Many GIT_TEST_* environment variables control various aspects of how our tests are run, ...
Slicing rows lazily Getting ready How to do it... How it works... There's more... Slicing lexicographically Getting ready How to do it... How it works... There's more... 5. Boolean Indexing ...
- 以辅音字母+y结尾的词,如"lazily",改y为i再加"-er/est",形成"lazier"和"laziest"。 - 以不发音的e结尾的形容词,如"large",直接加"-r/-st",得到"larger"和"largest"。 - 多音节和某些双音节词,如...
例如: In [1]: video_frame['subtitles'] # loads quickly from in-memory...In [2]: video_frame['image'] # loads lazily from disk能够定义数据对象的任意嵌套层次结构。 例如,“视频”可以具有关联的属性...
spring.servlet.multipart.resolve-lazily=false ``` 在实际应用中,为了处理文件上传,我们需要创建一个Controller,它会接收前端发送的文件。`MultipartFile`是Spring提供的接口,用于处理文件上传请求。以下是一...
2. **情态动词**:A部分中的情态动词如can(能),must(必须),may(可能),might(可能),could(能够)等展示了不同可能性和必要性的表达。在E部分中,针对这些情态动词的否定形式进行了提问和回答,帮助学生...
包括选择合适的图片格式(如WebP)、压缩图片大小、利用惰性加载(Lazily load images)等技术。 综上所述,"安卓 image"涉及了Android开发中的图像资源管理、菜单设计、全屏模式的实现以及图像的显示和处理等多个...
在这样的上下文中,"lazily created"可能意味着某些对象或服务会在实际需要时才进行实例化,以优化性能和资源利用率。 3. **svc.c**: "svc"通常代表"Service",这可能是实现某种特定服务的代码,如网络服务、系统...
Exporting a lazily initialized bean (which implements SelfNaming and is annotated with ManagedResource annotation) gives IllegalStateException [SPR-17592] #22124 MockHttpServletRequest changes Accept-...
npm install --save react-lazily-render 用法 () import React from 'react' ; import LazilyRender from 'react-lazily-render' ; ...lots of content... < LazilyRender xss=removed> } content = { ...
题目中给出的每篇文章后都有对应的答案,例如A篇21-23题答案为DDB,这表明学生需要通过阅读原文,理解文章内容,然后选择正确答案。 2. **七选五**:这种题型要求学生根据文章内容,从七个选项中选出五个最合适的...
lazily. Copyright (c) 2009 Armando Blancas. All rights reserved. The use and distribution terms for this software are covered by the Eclipse Public License 1.0 ...