-->
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.  [ 5 posts ] 
Author Message
 Post subject: should be an easy one-to-one question...
PostPosted: Wed Apr 05, 2006 8:31 pm 
Beginner
Beginner

Joined: Wed Apr 05, 2006 8:16 pm
Posts: 20
I am fairly new to Hibernate, and hopefully this is possible. I have two objects: An Issue.java, and a Customer.java. Their tables look like so:


-Issue Table-
---------------------------------
ID | fieldA | fieldB ...
---------------------------------

- Customer Table -
---------------------------------
ID | fieldA | fieldB ...
---------------------------------


They share primary key associations. If an issue's ID = 7, it's customer's ID = 7, and they are *always* one to one -- our requirements are that whenever there is an issue, also a customer. Our dba's won't let us just keep them as one table :[

Is there a way I can do something like this?

Issue issue = new Issue();
// ... populate the issue...

session.save(issue);

And have to it not only create a record in the Issue table, but also take the ID it just auto-gened in the id table, and set that id to a new row in the customer table?

Or do we *have* to do something like this:

issue = new Issue();
// ...
session.save(issue);
customer = new Customer();
customer.setIssue(issue);
session.save(customer);

We have tried many variations on foreign key generators and one-to-one mappings between our issue and customer file and can't seem to get these creates to automagically cascade in one call. Thanks very much if any one can help, or just fer reading my post through! :),

Jim[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 05, 2006 10:28 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Sure it's easy. It's so easy that Hibernate doesn't even do it for you.
Code:
public class Issue ...
{
  public Issue()
  {
    setCustomer(new Customer());
  }
  ...
}
In your hibernate mapping for Customer, set the id generator to "foreign". In your hibernate mapping for Issue, set the cascade on the one-to-one to Customer to "all", or at least "save". Then when session.save(issue) is called, hibernate saves the issue, cascade-saves the customer, and gets the customer id from the property at the other end of the one-to-one (the id, of course).


Top
 Profile  
 
 Post subject: Thank you, thank you!
PostPosted: Thu Apr 06, 2006 10:20 am 
Beginner
Beginner

Joined: Wed Apr 05, 2006 8:16 pm
Posts: 20
It worked :)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 06, 2006 5:38 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Good to hear. A click on the Yes link on the relevant post would be appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 07, 2006 11:10 am 
Beginner
Beginner

Joined: Wed Apr 05, 2006 8:16 pm
Posts: 20
I clicked the yes button -- thanks again! :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.