当我创建一个新的EntityManager以获得实体数据库版本时,find()方法在每次调用中检索randow结果,即使我使用Hibernate.initialize,列表(项目)也是随机的。 该服务是通过Spring批处理通过@Transactional(transactionManager =“ transactionManager”,传播= Propagation.REQUIRES_NEW)启动的。
这是我的代码:
@Entity
@Table(name = "activity")
public class Activity {
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "activity_detail", joinColumns = @JoinColumn(name = "activity_detail_id"), inverseJoinColumns = @JoinColumn(name = "item_id"))
private final List<Item> items = new ArrayList<>();
}
在我的服务中:
public boolean needUpdate(Activity entity) {
boolean needed;
EntityManager entityManager = emFactory.createEntityManager();
@SuppressWarnings("unchecked")
Activity dbEntityVersionLazy = entityManager.find(entity.getClass(), entity.getId());
Activity dbEntityVersion = EntityHelper.initializeAndUnproxy(dbEntityVersionLazy);
needed = !entity.equals(dbEntityVersion);
entityManager.close();
return needed;
}
谁能解释什么是问题?