我一直在撓我的頭一陣子,並認爲我會得到一些幫助:)我不明白爲什麼Hibernate會創建2個會話?
我正在與遺留數據庫,我不能改變。我有以下域:
@Entity
public class Institution {
@Id
private Long id;
@OneToMany(mappedBy="institution", fetch=FetchType.EAGER)
private List<Subscription> subscriptions = new ArrayList<Subscription>();
}
@Entity
public class Subscription {
@Id
private Long id;
@ManyToOne
@JoinColumn(name="sub_id", referencedColumnName="ins_sub_id", insertable=false, updatable=false)
private Institution institution;
}
爲了簡潔起見,我已經排除表示getter/setter方法。沒有連接表。
所以;
1)映射是否正確?我想要一個雙向關聯,我希望該機構成爲關係的所有者。
2)如果我打開一個機構,創建new Subscription()
和訂閱添加到收藏subscriptions
...
@RequestMapping(value="/add/{institutionId}", method=RequestMethod.POST)
public String submitSubscriptionForm(@ModelAttribute SubscriptionForm form) {
Institution institution = institutionService.getById(form.getInstitutionId());
Subscription subscription = new Subscription();
//...set properties on subscripton from data in the form
subscription.setInstitution(institution);
institution.getSubscriptions().add(subscription);
institutionService.saveOrUpdateInstitution(institution);
}
...當我保存機構...
institutionService.saveOrUpdateInstitution(institution);
剛委託給擴展HibernateDaoSupport的DAO。
...我收到以下錯誤:
org.springframework.orm.hibernate3.HibernateSystemException: Illegal attempt to associate a collection with two open sessions; nested exception is org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions
at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:679)
at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:411)
at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
at org.springframework.orm.hibernate3.HibernateTemplate.saveOrUpdate(HibernateTemplate.java:737)
at com.f1000.dao.hibernate.InstitutionDaoImpl.saveOrUpdate(InstitutionDaoImpl.java:161)
我使用了Spring和我利用了OpenSessionInViewFilter的,爲什麼創建第二屆我想不通?