相关文章
更多最近更新
更多Hibernate 异常之:associate a collection with two ...
2019-03-25 13:36|来源: 网路
Hibernate exception - Illegal attempt to associate a collection with two open session
采用 struts2 + hibernate3.3 + spring2.5.6
<!-- 由spring控制hibernate的session的打开与关闭 --> <filter> <filter-name>openSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> <init-param> <param-name>singleSession</param-name> <param-value>false</param-value> </init-param> </filter>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- 事务处理 --> <aop:config> <aop:pointcut id="serviceMethod" expression="execution(* com.dlut.service.*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod" /> </aop:config> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="get*" read-only="true" /> <tx:method name="addOrUpdate*" rollback-for="Exception" /> <tx:method name="add*" rollback-for="Exception" /> <tx:method name="update*" rollback-for="Exception" /> <tx:method name="delete*" rollback-for="Exception" /> </tx:attributes> </tx:advice>
Site site = siteService.getSiteByName("site01"); if (site == null) { site = new Site(); } site.setName(item.getName()); siteService.addOrUpdateSite(site);
public void addOrUpdateSite(Site site) { siteDao.saveOrUpdate(site); }
public void saveOrUpdate(Site site) { getHibernateTemplate().saveOrUpdate(site); }
只要是执行update或者saveOrUpdate方法就会抛以上异常,执行insert是没有问题的.
有人说我的事务处理没做好,我做了一下调试,发现没有问题, open session in view filter里面默认的FlushMode=NEVER,当执行到addOrUpdate*方法时,spring自动将FlushMode设为AUTO,当执行到"doInHibernate()"方法的时候抛出了以上异常,但是我调试的时候看到session只是打开了一个,好几天了,一直没想到怎么解决,非常的郁闷.
问题补充:
<param-name>singleSession</param-name>
<param-value>false</param-value>
以前用的是true,因为出了那个异常,实在没办法了,又改成了false试了试,后来忘改回来了,谢谢提醒.先把分给你吧,我自己再检查一下
相关问答
更多-
TCP/IP模型是一个________。[2023-05-19]
a -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
对不起,您正在尝试做什么,特别是,在Hibernate中不支持。 看这个常见问题: http://community.jboss.org/wiki/HibernateFAQ-AdvancedProblems#Im_getting_orghibernateMappingException_collection_was_not_an_association_when_I_try_to_join_a_collection_of_components_with_Criteria_queries 我也对此非常不满。 ...
-
您需要执行以下操作: 1)让Spring管理你的交易: 你可以在这里阅读更多关于它的信息: http : //www.mkyong.com/spring/spring-aop-transaction-management-in-hibernate/ 和这里: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/transaction.html#transaction-declarative 2)现在 ...
-
嗯, u.courses是一个集合,没有students属性(这是一个单一Course的属性)。 Well, u.courses is a Collection and doesn't have a students property (which is a property of a single Course).
-
Hibernate:如何使用@Embeddables的自定义集合?(Hibernate: How to use custom collection of @Embeddables?)[2022-03-03]
在将它们关联为@ElementCollection时,您不应该覆盖集合类型。 Hibernate使用自己的集合代理来检查实体的肮脏并提供延迟初始化。 即使指定HashSet,Hibernate也会用PersistentSet替换类型。 UserCollectionType可用于实体关联,但它可能不适用于可嵌入集合。 如果您愿意将MyEmbeddable转换为实体,则自定义集合的外观如下所示: @OneToMany(fetch = FetchType.LAZY, cascade = Casc ... -
复制实体集合并坚持在Hibernate / JPA中(Duplicate a collection of entities and persist in Hibernate/JPA)[2022-04-10]
Aaron Diguila的答案是要走到这里的方法,即您需要detach您的实例,将业务关键字设置为null ,然后persist它们。 可悲的是,没有办法用JPA 1.x断开实体管理器中的一个对象(JPA 2.0将有EntityManager.detach(Object)并修复此问题)。 所以,要么等待JPA 2.x(不是我猜测的选项),要么使用Hibernate的底层Session 。 为此,您可以将EntityManager的委托转换为休眠会话。 Session session = (Session) ... -
看起来你有一个名为Person的实体,它有一个延迟加载的地址映射集合? 您已经加载了Person,并且它所加载的会话现在已关闭。 会话关闭后,您尝试访问该地址集合,Hibernate试图加载它们。 但是,如果原始会话不再可用,则这是不可能的。 为了访问地址属性,您有几个选项: 使用OpenSessionInView模式确保Hibernate会话在请求/响应循环期间保持打开状态(因为您已经标记了Spring MVC,我将假设这是一个基于Web的操作)。 这基本上将你的Hibernate会话范围扩展到HTTP请 ...
-
将上述SQL转换为条件后,可以设置结果转换器。 在这种情况下,AliasToBeanConstructorResultTransformer可以工作 首先创建一个类来包含条件中的结果列, public class ResultPresenter{ private String a; private String bField1; private Integer bField2; private String bField3; private String bField4; public ResultPr ...
-
Hibernate映射异常(Hibernate Mapping Exception)[2022-02-01]
因为您将上面的@Id和@GeneratedValue java持久性注释放在: @Id @GeneratedValue private int ParameterGroupId; 并在同一时间上面: @Id @GeneratedValue public int getParameterGroupId() { return ParameterGroupId; } 它足以将注释放在private int ParameterGroupId;之上private int ParameterGroupId; ...