Note that there are some explanatory texts on larger screens.

plurals
  1. POManyToMany in Spring JPA deleting previous records
    primarykey
    data
    text
    <p>Hi I'm trying to develop petclinic application using HSQLDB,Spring,Webflow &amp; JSF.</p> <p>In which Vets and Specialities entities in BusinessEntities layer has ManyToMany relation as follows.</p> <p><b>Vets.java</b></p> <pre><code>@Entity @Table(name = "VETS", schema = "PUBLIC", catalog = "PUBLIC") public class Vets implements BaseEntity, java.io.Serializable { private Long id; private String firstName; private String lastName; private Set&lt;Specialties&gt; specialtieses = new HashSet&lt;Specialties&gt;(0); public Vets() { } public Vets(Long id) { this.id = id; } public Vets(Long id, String firstName, String lastName, Set&lt;Specialties&gt; specialtieses) { this.id = id; this.firstName = firstName; this.lastName = lastName; this.specialtieses = specialtieses; } @Id @GeneratedValue(strategy=GenerationType.IDENTITY) @Column(name = "ID", unique = true, nullable = false) public Long getId() { return this.id; } public void setId(Long id) { this.id = id; } @Column(name = "FIRST_NAME", length = 30) public String getFirstName() { return this.firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } @Column(name = "LAST_NAME", length = 30) public String getLastName() { return this.lastName; } public void setLastName(String lastName) { this.lastName = lastName; } @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY) @JoinTable(name = "VET_SPECIALTIES", schema = "PUBLIC", catalog = "PUBLIC", joinColumns = { @JoinColumn(name = "VET_ID", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "SPECIALTY_ID", nullable = false, updatable = false) }) public Set&lt;Specialties&gt; getSpecialtieses() { return this.specialtieses; } public void setSpecialtieses(Set&lt;Specialties&gt; specialtieses) { this.specialtieses = specialtieses; } } </code></pre> <p><b>Specialties.java</b></p> <pre><code>@Entity @Table(name = "SPECIALTIES", schema = "PUBLIC", catalog = "PUBLIC") public class Specialties implements BaseEntity, java.io.Serializable { private Long id; private String name; private Set&lt;Vets&gt; vetses = new HashSet&lt;Vets&gt;(0); public Specialties() { } public Specialties(Long id) { this.id = id; } public Specialties(Long id, String name, Set&lt;Vets&gt; vetses) { this.id = id; this.name = name; this.vetses = vetses; } @Id @GeneratedValue(strategy=GenerationType.IDENTITY) @Column(name = "ID", unique = true, nullable = false) public Long getId() { return this.id; } public void setId(Long id) { this.id = id; } @Column(name = "NAME", length = 80) public String getName() { return this.name; } public void setName(String name) { this.name = name; } @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY) @JoinTable(name = "VET_SPECIALTIES", schema = "PUBLIC", catalog = "PUBLIC", joinColumns = { @JoinColumn(name = "SPECIALTY_ID", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "VET_ID", nullable = false, updatable = false) }) public Set&lt;Vets&gt; getVetses() { return this.vetses; } public void setVetses(Set&lt;Vets&gt; vetses) { this.vetses = vetses; } } </code></pre> <p>And when I add vets object with specialities using setSpecialtieses method, the vets,specialities and vet_secialities tables are populated but the previous relations are getting loss, let me put in this way.</p> <p>Adding vet with ID 1</p> <pre><code> -------------- ----------------- -------------------- | Vets | | Specialities | | vet_specialities | | --------------| |-----------------| |--------------------| | ID Name | | ID Name | |Spe_ID Vet_ID | | --------------| |-----------------| |--------------------| | 1 ABC | | 1 Surgery | | 1 1 | -------------- | 2 Radiology | | 2 1 | ----------------- -------------------- </code></pre> <p>And when I add another Vet object with ID 2, </p> <pre><code> -------------- ----------------- -------------------- | Vets | | Specialities | | vet_specialities | | --------------| |-----------------| |--------------------| | ID Name | | ID Name | |Spe_ID Vet_ID | | --------------| |-----------------| |--------------------| | 2 ABC | | 1 Surgery | | 1 2 | -------------- | 2 Radiology | | 2 2 | ----------------- -------------------- </code></pre> <p>Here my previous entries gets deleted and new are getting stored, my requirement is to preserve older vets entries too.</p> <p>And I have vetsPage.xhtml in which I have One textbox with submit button and corresponding vets.xml flow on entry of which creates new vetsVO object and save it in List using JPA and this moment the Specialities set is null and in same way I have specialitiesPage with one textbox and submit button with corresponding flow which also creates SpecialitiesVO object and save in List and now I have one more page i.e., vetsSpecialitiesPage where I will choose one vetsVO object from dropdown list and map specialities with it and save. By this its saving properly but overriting existing relations. Tried couple of suggestions, i.e., using of mappedBy,CascadeType.MERGE etc.. but none give desired result.Any help is appreciated.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload