-->
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.  [ 4 posts ] 
Author Message
 Post subject: integer foreign keys
PostPosted: Tue Jul 21, 2009 4:34 pm 
Newbie

Joined: Tue Jul 21, 2009 4:24 pm
Posts: 2
Hi,
I have the following two classes: Student and Department.

class Student {
@Id
public void getId() {
...
}

...
@ManyToOne(optional = false)
@JoinColumn(name = "DEPT_ID")
public Department getDepartment() {
return this._department;
}

private Long _id;
private Department _department;
}

All this works fine. But the problem is that my foreign key, getDepartment has to return a Department object. I was wondering if this can be made to return a Long (which is the id of the department row concerned). The reason I was this is that I do not want to read in the entire department column (avoid a join of the user with the department table).

This problem becomes worse if the getDepartment is a set. Any set operation that requires equals/hashCode which I currently override to do business key based equality/ But any set operation leads to loading of all the department objects unnecessarily. I need to have this as a Set and not a bag.

Thanks,


Top
 Profile  
 
 Post subject: Re: integer foreign keys
PostPosted: Wed Jul 22, 2009 1:43 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
You can simply map the department id as a property of type long and implement an additional method for retrieveing the department object if needed.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Re: integer foreign keys
PostPosted: Wed Jul 22, 2009 2:05 am 
Newbie

Joined: Tue Jul 21, 2009 4:24 pm
Posts: 2
Thanks for the reply. Will this also work for @manytomany mappings with collections/sets?


Top
 Profile  
 
 Post subject: Re: integer foreign keys
PostPosted: Wed Jul 22, 2009 2:55 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
No, I'm afraid not. You could model the many-to-many with an entity that connects the objects and use many-to-one associations instead: A n:1 AB 1:m B.

You can maybe write your own proxy implementation and use lazy-loading. But that is a complex way to go. I can't help you there, but you probably find enough information on the net.

_________________
--Wolfgang


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