Note that there are some explanatory texts on larger screens.

plurals
  1. POHibernate fails to update collection of elements in database
    primarykey
    data
    text
    <p><b>Embedded Class</b></p> <pre><code>@Embeddable public class TeachingExperience { private String courseName; private String instructor; private String description; } </code></pre> <p><br/> <b>Entity 1</b></p> <pre><code>@Entity public class CV { @ElementCollection(fetch= FetchType.EAGER) @Fetch(value = FetchMode.SELECT) private List&lt;TeachingExperience&gt; teachingExperiences; } </code></pre> <p><br/> <b>Entity 2</b></p> <pre><code>@Entity public class Student { @OneToOne(cascade= CascadeType.ALL, orphanRemoval=true) private CV cv; } </code></pre> <p><br/> The CV class stores a list of teaching experiences a person has. There are other fields that should be fetched eagerly, hence the @Fetch annotation.</p> <p>I use this method to add new teaching experiences to cv of a given student:</p> <pre><code>public static void add (String course, String prof, String desc) { TeachingExperience TE = new TeachingExperience(course, prof, desc); student.getCv().getTeachingExperiences().add(TE); Session session = HibernateUtil.getSessionFactory().openSession(); session.beginTransaction(); session.merge(student); session.getTransaction().commit(); session.close(); } </code></pre> <p>with this configuration each time i try to add a new Teaching Experience, data stored in DB table, get duplicated first, then new entry get inserted. My database table after two consecutive method call looks like this:</p> <p><i> add('DS', 'Feili', 'TA'); </i></p> <pre><code>- DS Feili TA </code></pre> <p><br/> <i> add('AP', 'Ramtin', 'TA'); </i></p> <pre><code>- DS Feili TA - DS Feili TA - AP Ramtin TA </code></pre> <p><br/></p> <p>I have the same problem when trying to remove an object form the list of teaching experiences and I am not sure which part of my code is the root of this odd behavior. </p> <p>Your help will be highly appreciated.</p> <p><br/> <b> EDIT 1: </b></p> <pre><code>@Entity public class TeachingExperience { @Id @GeneratedValue private Integer id; private String courseName; private String instructor; private String description; } @Entity public class CV { @OneToMany(fetch= FetchType.EAGER, cascade=CascadeType.ALL, orphanRemoval=true) @Fetch(value = FetchMode.SELECT) @JoinColumn(name="cv") private List&lt;TeachingExperience&gt; teachingExperiences; } </code></pre> <p><br/></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.
 

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