我一直在挠我的头一阵子,并认为我会得到一些帮助:)我不明白为什么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的,为什么创建第二届我想不通?