-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Setting/transferring an ID to a different table
PostPosted: Wed Oct 22, 2014 7:06 am 
Newbie

Joined: Wed Oct 22, 2014 6:59 am
Posts: 1
Hi.

Currently we have a table/class called User. This has a class belonging to it called VoicemailConfig (which is also a table). This relationship is a one to many, meaning voicemailConfig has a userId (foreign key = id in user).

Both tables are successfully created in the database. In terms of java, the User/UserDTO object have a voicemailConfig object. When it is written to the database, the user is written, but the voicemailConfig object is not written into the database. We think it is something to do with user having a null id until the last possible moment, and because voicemailConfig sees userID as null, it doesn't write it into the database.

Class structure (bold is class, rest are methods/objects inside. id/userId should be the same - using keys)

User : pin, id ... voicemailConfig
voicemailConfig: pin, userId

We are using:

Code:
@OneToMany(mappedBy = "user",fetch = FetchType.EAGER )
  private List<VoicemailConfig> vmConfig;


Inside user to define the voicemailConfig list. User is embeddable - in java, the voicemailConfig has it's user inside it (because we were trying to get the userId).

Inside voicemailConfig we have:

Code:
  @ManyToOne
  @JoinColumn(name = "userId", nullable = false)
  @Embedded
  @AttributeOverrides({ @AttributeOverride(name = "id", column = @Column(name = "userId")) })
  private User user;


Unfortunately it is hard for me to post much code, as there are a LOT of classes. If you need specifics just give me a shout.


Top
 Profile  
 
 Post subject: Re: Setting/transferring an ID to a different table
PostPosted: Thu Oct 23, 2014 9:21 am 
Hibernate Team
Hibernate Team

Joined: Fri Sep 09, 2011 3:18 am
Posts: 295
Hi,
I think the following mapping will obtain the desired effect:
Code:
@OneToMany(mappedBy = "user", fetch = FetchType.EAGER, cascade=CascadeType.PERSIST )
private List<VoicemailConfig> vmConfig;

Code:
@ManyToOne
@JoinColumn(name = "userId", nullable = false)
private User user;


I haven't test it, though.

I hope this help.


Top
 Profile  
 
 Post subject: Re: Setting/transferring an ID to a different table
PostPosted: Thu Oct 23, 2014 9:24 am 
Hibernate Team
Hibernate Team

Joined: Fri Sep 09, 2011 3:18 am
Posts: 295
By the way, you can find some examples for the mapping in the @OneToMany javadoc: http://docs.oracle.com/javaee/6/api/javax/persistence/OneToMany.html


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.