-->
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: linking to a table with existing field
PostPosted: Tue Mar 02, 2010 7:27 am 
Newbie

Joined: Mon Jul 21, 2008 5:40 am
Posts: 16
Hi,

I have a Usr table which has a field for country, it uses ISO code like 'US', 'CA', 'SG', etc. and I also have a country table, the code is same as ISO code, the two tables are independent from each other. usually I'll query the country table if I need to display the name, works fine. but this is not so efficient if big result set returned from User, it looks like a 'N + 1' problem. but I can't modify the table structures any more, is there a way to specify the country in the User table using country as linking field without changing the structure? Thanks,

Angelo



public class User {

private Long id;
private String code;
private String name;
private String country;
...
@Column(length=2)
public String getCountry() {
return country;
}

public void setCountry(String country) {
this.country = country;
}
}



public class Country {

private String code;
private String name;
...
}


Top
 Profile  
 
 Post subject: Re: linking to a table with existing field
PostPosted: Tue Mar 02, 2010 7:58 am 
Senior
Senior

Joined: Wed Sep 19, 2007 9:31 pm
Posts: 191
Location: Khuntien (Indonesia)
Both of the entities just can be linked by primary key.
Why don't you use join in your query if you don't change the current schema?


Top
 Profile  
 
 Post subject: Re: linking to a table with existing field
PostPosted: Tue Mar 02, 2010 8:10 am 
Newbie

Joined: Mon Jul 21, 2008 5:40 am
Posts: 16
yes, I use link table as follow:

select u, c.name as cname from User as u, Country as c where u.country=c.code

but i can't do a left join fetch to load some other tables:

select u, c.name as cname from User as u left join fetch u.credits, Country as c where u.country=c.code


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.