Note that there are some explanatory texts on larger screens.

plurals
  1. POInsert with Hibernate with child objects does not return full object
    primarykey
    data
    text
    <p>I am using Spring 3.2 and hibernate-core 4.1.4 and hibernate-jpa-2.0.1. The application resource file has all the correct objects.</p> <p>I have a child object RoleEntity, and it has two parents: User and Award, so the userId and the awardId are foreign-keys that already exist, and MUST exist for the role entity to be created.</p> <pre><code>public class RoleEntity implements Serializable { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "role_id") private long roleId; @Column(name = "role_description") private String roleDescription; @Column(name = "role_name") private String roleName; @ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name = "user_id") private UserEntity user; @ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name = "award_id") private AwardEntity award; ... getters/setters ... hash/equals/toString } </code></pre> <p>My DAO insert looks very simple:</p> <pre><code>@Override public RoleEntity saveRoleEntity(RoleEntity role) { logger.debug("saveRoleEntity: role=" + role); return role; } </code></pre> <p>I have unit tested this code, and I can confirm that when I select an existing roleId, I can fully get back the loaded object, with User and Award fully populated.</p> <p>However, when I insert new role, I do the following:</p> <ol> <li>set roleId to 0, and the role description ...</li> <li>create a userEntity and set the id only, this id already exists and is not new</li> <li>create a awardEntity and set the id only, this id already exists and is not new</li> </ol> <p>I can successfully do a save and this is great!!!! Works for me. And in the new object return, I can clearly see the new roleId is returned to me!!! All the other fields to RoleEntity, like the description, that is there.</p> <p>But, what I ALSO want is for the User and Award fields of the RoleEntity to be fully populated as if I had done a select after the insert. Is this even possible?</p> <p>I would prefer to not do a select to the user and award tables to get those objects and then assign them to this object, but if I have to do that, then that's fine.</p> <p>If I need to provide any more information, please let me know. Thanks for any help in advance.</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