Can';当使用hibernate@LazyCollection(LazyCollectionOption.FALSE)注释时,不能删除唯一的子对象
我在实体类中阅读并使用@LazyCollection(LazyCollectionOption.FALSE)的原因与此类似: EntityCity.java:Can';当使用hibernate@LazyCollection(LazyCollectionOption.FALSE)注释时,不能删除唯一的子对象,hibernate,jpa-2.0,Hibernate,Jpa 2.0,我在实体类中阅读并使用@LazyCollection(LazyCollectionOption.FALSE)的原因与此类似: EntityCity.java: @ManyToOne @JoinColumn(name="Country_Id") private EntityCountry mcountry; public EntityCountry getMcountry() { return mcountry; } public void setMcountry(EntityCountry m
@ManyToOne
@JoinColumn(name="Country_Id")
private EntityCountry mcountry;
public EntityCountry getMcountry() { return mcountry; }
public void setMcountry(EntityCountry mcountry) { this.mcountry = mcountry; }
EntityCountry.java:
@LazyCollection(LazyCollectionOption.FALSE)
@OneToMany(mappedBy="mcountry", targetEntity= EntityCity.class, cascade= CascadeType.ALL)
private List<EntityCity> cities;
public List<EntityCity> getCities() { return cities; }
public void setCities(List<EntityCity> cities) { this.cities = cities; }
并删除代码:
public void delete() {
EntityManager em = emf.createEntityManager();
try {
EntityTransaction entr = em.getTransaction();
boolean committed = false;
entr.begin();
try {
EntityCity city = em.find(EntityCity.class, 7);
em.remove(city);
entr.commit();
committed = true;
} finally {
if(!committed) entr.rollback();
}
} finally {
em.close();
}
当我不使用“@LazyCollection(LazyCollectionOption.FALSE)”代码时,代码运行良好,但我需要它。最好的解决方案是什么?请包括完整的堆栈跟踪我包括完整的堆栈跟踪。我没有任何答案。这可能与您的事务管理有关,也可能与您对Weld的使用有关,但我不知道。您是否尝试过声明性事务?Spring/EJB3/CDI
public void delete() {
EntityManager em = emf.createEntityManager();
try {
EntityTransaction entr = em.getTransaction();
boolean committed = false;
entr.begin();
try {
EntityCity city = em.find(EntityCity.class, 7);
em.remove(city);
entr.commit();
committed = true;
} finally {
if(!committed) entr.rollback();
}
} finally {
em.close();
}