-->
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.  [ 8 posts ] 
Author Message
 Post subject: Simple Java relationship mapping
PostPosted: Sun Mar 28, 2004 7:30 pm 
Beginner
Beginner

Joined: Sun Mar 28, 2004 7:18 pm
Posts: 26
Hi All,

I have tried reading up on this everywhere possible, but I still dont see a "simple example" of this anywhere....spent 2 days on this, so now I'm turning to u guys for help. Here is simply what I wanna do :

- I have 2 classes - Candidate and Skill

- Candidate contains a relation to Skill :

public class Candidate {

....
....
Skill skill;
}

A candidate can have only one skill (traversal is from Candidate to Skill, not the other way around).

I want to be able to populate a candidate object (along with the skill info in candidate) and say session.save(candidate) and have Hibernate save the candidate and the associated Skill object embedded in candidate for me. Simple I know, but I cant get it to work.

Hibernate docs say that in this case, a <many-to-one> should be used. I even read the Foo/Bar example that shows table mappings, but my question is, shouldnt the FK be in the SKILL table and not in the CANDIDATE table ? The Candidate class contains the Skill instance, so from that perspective Candidate is the parent and Skill is the child, so the FK should be in the SKILL table, so the <many-to-one> mapping should be in Skill.hbm.xml right ?

I guess what I'm trying to find out is :

a) Can someone tell me the class structure for both Candidate and Skill ?
b) Can someone tell me the database table structure for CANDIDATE and SKILL (where should the FK be, etc)
c) What should the mapping file look like (where does the <many-to-one> go ?

Sorry if this is too mundane a thing for u guys, but I dont see a "good straightforward example" of this kind anywhere (most articles jump into dealing with collections and many-to-many)

Thanks for your help.
Suman


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 28, 2004 7:41 pm 
Newbie

Joined: Thu Mar 18, 2004 12:29 pm
Posts: 9
This link helped me out a lot.

http://www.xylax.net/hibernate/


Top
 Profile  
 
 Post subject: MikeC
PostPosted: Sun Mar 28, 2004 8:49 pm 
Beginner
Beginner

Joined: Sun Mar 28, 2004 7:18 pm
Posts: 26
Mike,

Thanks for the reply, but like I said, I've scanned everything on the net for Hibernate related docs. If you have any "concrete" solution that would give me a better idea of the problem, then please let me know.

Thanks,
Suman


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 28, 2004 8:57 pm 
Senior
Senior

Joined: Wed Mar 24, 2004 11:40 am
Posts: 146
Location: Indianapolis, IN, USA
Here is my opinion on the design structure. However, I happen to be a Hibernate newbie so you can take it for what it's worth.

If a candidate could have one and only one skill but a skill could be possessed by many candidates, the relationship between Candidate and Skill would probably be a many-to-one declared in the Candidate.hbm.xml file.

The class structure would probably be something like this.

Code:
public class Candidate
{
  public Candidate() {}

  private Skill skill = null;

  public Skill getSkill()
  {
    return this.skill;
  }

  public void setSkill(Skill skill)
  {
    this.skill = skill;
  }

}


The table structure would be something similar to this.

    TABLE CANDIDATE
    (
    CANDIDATEID NUMBER PK
    SKILLID NUMBER
    ...
    ...
    ...
    FK_SKILLID ASSOCIATED TO SKILL.SKILLID
    )

    TABLE SKILL
    (
    SKILLID NUMBER PK
    )


The many-to-one mapping in the Candidate.hbm.xml file would be something like this.

Code:
<many-to-one name="skill" column="SKILLID" not-null="true"/>



Hope that helps.


Top
 Profile  
 
 Post subject: Solved
PostPosted: Sun Mar 28, 2004 9:22 pm 
Beginner
Beginner

Joined: Sun Mar 28, 2004 7:18 pm
Posts: 26
The problem was with <unsaved-value="null">

It should have been <unsaved-value="0">. I changed it to that and everything was fine.

I can now fill a skill into a candidate and say session.save(candidate) and it persists both candidate and skill with the right PKs and FKs in both tables.

Question is, what part did <unsaved-value> play in this ? Whats it really used for ? Guess I gotta go back and read some more...but incase anyone is having a similar problem and u get the (row not found) error when u're persisting, then try this solution.

Suman


Top
 Profile  
 
 Post subject: Re: Solved
PostPosted: Sun Mar 28, 2004 9:28 pm 
Senior
Senior

Joined: Wed Mar 24, 2004 11:40 am
Posts: 146
Location: Indianapolis, IN, USA
suman wrote:
Question is, what part did <unsaved-value> play in this ? Whats it really used for ? Guess I gotta go back and read some more...but incase anyone is having a similar problem and u get the (row not found) error when u're persisting, then try this solution.

Suman



The unsaved-value attribute helps Hibernate determine whether the object should be inserted or updated. I was screwing up the very same thing last week because I was setting my primary key field as a 0 (I use java.lang.Long as the corresponding Bean attribute) which caused it to try and update a column in the database with primary key value of 0. So in order to be on the safe side, I just went ahead and had the initial value in the bean as a null. So whenever I implicitly call a setId() and pass in a non null value, Hibernate knows to update, otherwise it performs an insert.


Top
 Profile  
 
 Post subject: gpani
PostPosted: Sun Mar 28, 2004 9:37 pm 
Beginner
Beginner

Joined: Sun Mar 28, 2004 7:18 pm
Posts: 26
So, can you please elaborate a little more by what you mean.

If I say <unsaved-value="0"> as opposed to <unsaved-value="null", what is actually happening when I am trying to persist A (which has an instance of B) ?

Thx,
Suman


Top
 Profile  
 
 Post subject: <nsaved-value> issue
PostPosted: Sun Mar 28, 2004 9:41 pm 
Beginner
Beginner

Joined: Sun Mar 28, 2004 7:18 pm
Posts: 26
Actually, I just read up on it and it makes sense to me now. Thanks gpani, but you dont have to elaborate :-)

S


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