Note that there are some explanatory texts on larger screens.

plurals
  1. POHibernate annotations cascading doesn't work
    primarykey
    data
    text
    <p>I've decided to change hbm.xml style to annotations using hibernate. I had in my hbm.xml:</p> <pre><code>&lt;hibernate-mapping package="by.sokol.jpr.data"&gt; &lt;class name="Licence"&gt; &lt;id name="licenceId"&gt; &lt;generator class="native" /&gt; &lt;/id&gt; &lt;many-to-one name="user" lazy="false" cascade="save-update" column="usr"/&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; </code></pre> <p>And changed it to:</p> <pre><code>@Entity public class Licence { @Id @GeneratedValue(strategy = GenerationType.AUTO) private int licenceId; @ManyToOne(targetEntity=User.class, fetch=FetchType.EAGER, cascade = CascadeType.ALL) @Cascade(value = { org.hibernate.annotations.CascadeType.SAVE_UPDATE }) private User user; } </code></pre> <p>User class:</p> <pre><code>@Entity(name = "Usr") public class User { // BEGIN user info @Basic private String uid; @Basic private String name; @Basic private String company; @Basic private String street; // user's zip code @Basic private String ubication; @Basic private String city; @Basic private String po; @Column(name = "useremail") @Id @GeneratedValue(strategy = GenerationType.IDENTITY, generator = "assignedGen") @GenericGenerator(name = "assignedGen", strategy = "assigned") private String email; @Basic private String challengPassword; @Basic private String serialNumber; } </code></pre> <p>Hibernate.cfg.xml </p> <pre><code>... &lt;mapping class="by.sokol.jpr.data.Licence" /&gt; &lt;mapping class="by.sokol.jpr.data.User" /&gt; ... </code></pre> <p>Java code to get session</p> <pre><code>... AnnotationConfiguration cfg = new AnnotationConfiguration(); cfg.configure(new File(PROPERTIES_FILENAME)); sessionFactory = cfg.buildSessionFactory(); ... </code></pre> <p>Java code for saving Licence object</p> <pre><code>org.hibernate.Transaction t = session.beginTransaction(); session.saveOrUpdate(licence); t.commit(); </code></pre> <p>generated sql:</p> <pre><code>Hibernate: select this_.licenceId as licenceId0_2_, this_.creationDate as creation2_0_2_, this_.limitDate as limitDate0_2_, this_.user_useremail as user4_0_2_, this_.workstation_motherboardId as workstat5_0_2_, user1_.useremail as useremail1_0_, user1_.challengPassword as challeng2_1_0_, user1_.city as city1_0_, user1_.company as company1_0_, user1_.name as name1_0_, user1_.po as po1_0_, user1_.serialNumber as serialNu7_1_0_, user1_.street as street1_0_, user1_.ubication as ubication1_0_, user1_.uid as uid1_0_, workstatio4_.motherboardId as motherbo1_2_1_, workstatio4_.computerName as computer2_2_1_, workstatio4_.macAddress as macAddress2_1_, workstatio4_.osId as osId2_1_ from Licence this_ inner join Usr user1_ on this_.user_useremail=user1_.useremail left outer join Workstation workstatio4_ on this_.workstation_motherboardId=workstatio4_.motherboardId where user1_.useremail=? Hibernate: select nextval ('hibernate_sequence') Hibernate: insert into Licence (creationDate, limitDate, user_useremail, workstation_motherboardId, licenceId) values (?, ?, ?, ?, ?) </code></pre> <p>APPEND_1: working code</p> <pre><code>org.hibernate.Transaction t = session.beginTransaction(); session.save(licence.getUser()); session.save(licence); t.commit(); </code></pre> <p>And hibernate doesn't save user on saving. I really need help!</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