简体   繁体   中英

Hibernate: Update empty child table

@OneToMany(mappedBy="columnOne", orphanRemoval=true, cascade=CascadeType.ALL) @LazyCollection(LazyCollectionOption.FALSE) private List<Constraint> constraints = new ArrayList<Constraint>();

whenever the Parent child record gets updated the child tables record also should get updated.

When I tired with session.SaveOrUpdate(), the Parent table record got updated but the child table everytime a new record is inserted and not updated which leads to duplicate data

When tired with session.merge(), getting error, A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: com as the constraint may be null as not always the child table has data

To resolve this issue, tried to set empty collection but this leads to lot of rows in database with null values.

Is there any way to update both Parent and Child even when child table data may be null

whenever the Parent child record gets updated the child tables record also should get updated.

Hibernate dirty check would do that for you, all you need to have all your entities managed , hibernate by default on any change of managed entities will check all the manage entities and their attributes, check this link for further info

When I tired with session.SaveOrUpdate(), the Parent table record got updated but the child table everytime a new record is inserted and not updated which leads to duplicate data

I would guess your child entities are not managed and does not have id value set, so yeah your children will be cascaded(because that is what you want by setting cascadetype) and will be persisted as new records in your database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM