cannot simultaneously fetch multiple bags 问题的解决办法

问题是什么时候出现的呢?

当一个实体对象中包含多于一个non-lazy获取策略时,比如@OneToMany,@ManyToMany或者@ElementCollection时,获取策略为(fetch = FetchType.EAGER)

出现问题的原因:

当(fetch = FetchType.EAGER)多余一个时,持久框架抓取一方的对象时,同时又将多方的对象加载进容器中,多方又可能关联其它对象,Hibernate实现的JPA,默认最高抓取深度含本身级为四级(它有个属性配置是0-3),若多方(第二级)存在重复值,则第三级中抓取的值就无法映射,就会出现 multiple bags。

解决方法:

1、将(fetch = FetchType.EAGER)改为(fetch = FetchType.LAZY)

2、将List修改成Set集合,即推荐@ManyToMany或@OneToMany的Many方此时用Set容器来存放,而不用List集合。

3、改变FetchMode为@Fetch(FetchMode.SUBSELECT),即发送另外一条select语句抓取前面查询到的所有实体对象的关联实体。

4、在对应的属性上添加@IndexColumn,该注解允许你指明存放索引值的字段,目的跟Set容器不允许重复元素的道理一样。

推荐的处理办法:

方法2;

方法3和方法4是Hibernate特有的,非JPA标准;

如果可以用方法1,那就不会出现这个问题。

参考:

1、http://blog.eyallupu.com/2010/06/hibernate-exception-simultaneously.html
2、http://dyldragon.iteye.com/blog/788385
3、http://howsun.blog.sohu.com/119020926.html

转自 : http://www.quanlei.com/2011/10/2817.html


### Ghost Collision Issue Solution in Game Development or Physics Engine Context In the context of game development and physics engines, ghost collisions refer to situations where objects appear to pass through each other without interacting properly. This phenomenon often arises due to inaccuracies in collision detection algorithms or improper handling of object states over time. To address ghost collisions effectively: #### Enhancing Collision Detection Accuracy Improving the precision of collision detection is crucial. One approach involves refining the temporal resolution during simulation steps. By reducing the timestep size between frames, more accurate tracking of object positions occurs, thereby minimizing chances for objects to skip past one another undetected[^1]. Another method focuses on continuous collision detection (CCD), which predicts future positions based on current velocities rather than relying solely on discrete checks per frame update cycle. Implementing CCD ensures smoother interactions even when high-speed movements occur within scenes[^2]. ```cpp // Example C++ code snippet demonstrating reduced timestep implementation float deltaTime = 0.016f; // Smaller delta time improves accuracy but increases computational cost while (!gameOver) { UpdatePhysics(deltaTime); } ``` #### Handling Multiple Inputs Correctly When dealing with complex structures like arrays that receive multiple inputs simultaneously, ensuring all changes propagate correctly prevents unexpected behaviors such as ghost collisions. Proper synchronization mechanisms should be applied so updates do not interfere negatively with ongoing processes. For instance, consider an animation system managing several input sources affecting character motion. Ensuring these modifications integrate seamlessly into property values avoids anomalies similar to those described earlier regarding fast paths working improperly under certain conditions. #### Maintaining Consistent Object States Maintaining consistent state information across different parts of applications helps prevent inconsistencies leading to ghost collisions. For example, landscapes generated using world composition modes previously experienced problems because coordinate adjustments affected UV mappings unpredictably. Avoid altering fundamental attributes unless absolutely necessary, maintaining stable references whenever possible. By applying strategies focused on improving collision detection fidelity, accurately processing simultaneous inputs, and preserving reliable object representations, developers can significantly reduce occurrences of ghost collisions in games and simulations.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值