Java – hibernate – how to keep only the parent node and keep the child node unchanged

Someone can help me understand how to configure hibernate to do what I want

I have a parent entity "apartment", which has a "room" list as a child I have a table to edit "apartment". In this table, I list all children's "rooms" for reference only Guest rooms are added and edited separately

So because I listed the rooms in the apartment form, I set lazloading to false:

@OneToMany
@JoinColumn (name = "appartmentId")
@LazyCollection (LazyCollectionOption.FALSE)
private List<Room> room;

But if I edit an apartment and store it, all the apartment rooms suddenly disappear In the database, they are not deleted, but dereferenced (e.g. appartmentid = null)

So how to configure hibernate to keep only my app object Instead of touching the children?

This is my save action:

public String save() throws Exception {
    boolean isNew = (appartment.getAppartmentId() == null);

    appartment = appartmentManager.save(appartment);

    String key = (isNew) ? "appartment.added" : "appartment.updated";
    saveMessage(getText(key));

    return SUCCESS;
}

Solution

It's simple There is no need to repopulate your child or create a separate dto

If you will never insist on children, just add insertable = false and updatable = false in your join column comment like this:

@OneToMany
@JoinColumn (name = "appartmentId",insertable = false,updatable = false)
@Fetch(value = FetchMode.JOIN)
private List<Room> room;
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>