Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Two things here. First of all you shouldn't create <strong>new</strong> entities with known IDs just to use this ID. This approach is an accident waiting to happen: an entity with this ID already exists and you will run into a <code>ConstraintViolationException</code> or <code>NonUniqueObjectException</code> because of this duplicate key.</p> <p>With Hibernate you can use <strong>proxies</strong> if you want to use an entity with a known ID but you do not want to select the full entity from the database.</p> <pre><code>UserEntity user = session.load(UserEntity.class, userId); AwardEntity award = session.load(AwardEntity.class, awardId); </code></pre> <p>This will not hit the database to select the entities but a proxy (which wraps the known id) will be returned. Within the same session you can than use the proxy like the full entity and create the new <code>RoleEntity</code></p> <pre><code>RoleEntity role = new RoleEntity(); role.setUser(user); role.setAward(award); session.save(role); </code></pre> <p>The second thing is: how should Hibernate <em>know</em> the non-id properties of your entities without hitting the database!? The only way Hibernate could do this is using the <a href="http://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html/ch20.html#performance-cache" rel="nofollow">Second Level Cache</a> (L2C), but from your question I assume that there is no L2C in place.</p> <p>Even though your <code>@ManyToOne</code> associations are <em>eager-fetched</em> Hibernate must hit the database to populate the according entities. So you may use the approach from @Bogdan's answer and re-load the role and associated user and award entities after the insert</p> <pre><code>session.save(role); session.refresh(role); </code></pre> <p>but this will have no performance benefit over selecting the according entities first in order to assign them to the new role</p> <pre><code>UserEntity user = session.get(UserEntity.class, userId); AwardEntity award = session.get(AwardEntity.class, awardId); </code></pre> <p>This will hit the database immediately and populate your entities (or return null if no row with the given ID exists).</p> <p>Note that when you are using proxies you can populate your entities <em>on-demand</em> within the same session. As soon as a non-id property is accessed the according entity is selected from the database.</p> <p>That said, I recommend to take a look at the Hibernate <a href="http://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html/ch11.html" rel="nofollow">docs</a> for a better understanding. If you want to work with Hibernate on a production level you should also read a good Hibernate book like <a href="http://rads.stackoverflow.com/amzn/click/1932394885" rel="nofollow">Java Persistence with Hibernate</a>. </p> <p>Hope this helps. </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.
    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