2017-07-17 42 views
0

更新子表 時,我有在Hibernate中的一個問題。當重複記錄來自父表的記錄被更新,但新的一列被插入子表,其中的子表也更新子表需要更新並且不能插入。使用session.saveOrUpdate(storeObject)休眠 - 當父母得到更新

試過級聯presist,SaveOrUpdate,但問題沒有解決

@Entity 
@Table(name = "Garage") 
public class Garage { 

@OneToMany(mappedBy = "garage") 
@Cascade(CascadeType.PERSIST) 
    public Set<Car> getCars() { 
    return cars; 
    } 

} 

@Entity 
@Table(name="Car", [email protected](columnNames={"GarageId"})) 
public class Car { 

    private Garage garage; 

    @ManyToOne(optional=false) 
    @JoinColumn(name="GarageId") 
    public Garage getGarage() { 
    return garage; 
    } 

} 
+0

寫一個適當的equals和hashCode方法可能會解決這個問題恕我直言。 –

回答

0

我也面臨着同樣的issue.I搜索了很多,但不能得到正確的方法在子表更新數據,而在parent.Finally插入新的數據,我只是做了一個黑客通過映射ID,以滿足我的need.Get子項,並刪除所有行和插入的代碼again.A片斷我習慣於在下面寫作。

for (Car car: garag.getCars()) { 
    String hql4 = "delete from Car where garage.garageId=:id"; 
    Query<EventDetails> query4 = session.createQuery(hql4); 
    query4.setParameter("id", garageid); 
    query4.executeUpdate(); 
    } 

試試這個。它可以幫助你。