-->
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: Foreign Keys without the member variable object
PostPosted: Fri Nov 24, 2006 10:44 am 
Beginner
Beginner

Joined: Fri Nov 24, 2006 10:33 am
Posts: 42
Hi

I'm new to Hibernate so sorry if this is a really basic question, but I'm having trouble finding the answer in the docs.

I have two existing tables A, and B. Table A has a column called 'b_id' which is a foreign key to the primary key of B.

My Java value object for A has a member variable called 'bId'

I'm converting a system from SpringJDBC to Hibernate, but all the mapping examples I come across require A to have an instance of B, which means I have to change my Java code.

I don't mind doing that if I have to, but I was wondering if I can just map 'bId' as a property of A, and somehow mark it as a foreign key in the mapping.

If someone could show me an example of this basic mapping example, I'd be really grateful. I think my head is still in 'relational-mode' which is why I'm having trouble with it...

Regards

Richard


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 24, 2006 11:33 am 
Newbie

Joined: Tue Mar 08, 2005 8:00 am
Posts: 10
You can just map the "B_ID" column as a property in the A mapping, without worrying about the relationship, so you have not to change your java code.

Just put something like that in A mapping file:
Code:
<property name="bId" column="b_id"/>


Then you can use theta-style joins in your HQL queries to retreive the relationship value:
Code:
select b from A a, B b where a.bId = b.id and a.id = :something

Or just use get:
Code:
B b = session.get(B.class, a.getBId())


You should however really think about converting the "bId" column in a "b" property of type B. Having an "A" object you can just do:
Code:
a.getB()

and you'll get the "b" object without any query.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 24, 2006 11:49 am 
Beginner
Beginner

Joined: Fri Nov 24, 2006 10:33 am
Posts: 42
Thanks Kirby

I will change the Java on your recommendation. The system I'm converting is quite large, so I want to get everything clear in my head so I know I'm doing things right.

Thanks for the clear explanation - really appreciate it.

Richard


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.