2

I am trying to persist an object which has one to many relation with other entity. Using CascadeType.ALL works but, indeed I do not want to delete that entity when I delete main object. Therefore I tried to use CascadeType.PERSIST. But when I use it it makes a select query and says that entity not found. So why hibernate does it when I use cascadeType.PERSIST.

Here is my class basically:

public class Employeer{

    @Id
    private String id;

    @Column
    private String name;

    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinTable(name = "employer_details", joinColumns = {@JoinColumn(name = "employeer_id", referencedColumnName = "id")}
    , inverseJoinColumns = {@JoinColumn(name = "details_id", referencedColumnName = "id")})
    private Set<Address> otherObjects= new HashSet<>();

}

So the question is why using CascadeType.PERSIST, does not persist Address element and creates a select query and says that entity not found. Why I want to use it, I do not want to delete entity from address table when I delete employeer.

Note: I was looking at this post. Unable find Entity .. while trying to save data . But there I am trying to get why CascadeType.PERSIST does not work.

user1474111
  • 1,356
  • 3
  • 23
  • 47
  • Ques. 1 why using CascadeType.PERSIST, does not persist Address element? Ans. 1 It should work - please check are you setting address into Employee. – Atul Jain Nov 20 '19 at 07:42
  • Yes I do set it. But as I said it generates select query into adress table before inserting. And as there is not this record in adress table, it says that entity not found. – user1474111 Nov 20 '19 at 10:13

0 Answers0