wood burning stoves 2.0*
The moose likes Object Relational Mapping and the fly likes Hibernate join and key tag Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate join and key tag" Watch "Hibernate join and key tag" New topic
Author

Hibernate join and key tag

Nick Carag
Greenhorn

Joined: Dec 08, 2010
Posts: 13
Hi all,
I've a problem with hibernate xml mapping and join. This is my scenario:

Table Resource
id, Pk
code
message
id_locale

Table Locale
id, Pk
code
description

On the table Resource I've a fk on id_locale referencing the column id of the table Locale.

Java Side, I've created a pojo ResourceBean with getter/setter for id, code, message, localeCode and this is my mapping




When i try to retrieve a resource, the join fails in finding the column ID_LOCALE

There's something wrong with the on clause but I don't know how to correct it in the mapping.

Thanks

Nick Carag
Greenhorn

Joined: Dec 08, 2010
Posts: 13
Anyone? I just don't want to create a Locale POJO for a read only information like the locale code, and the use a one-to-one association. I found that with hibernate annotation @SecondaryTable it's possible to do what I'm asking for, but exists an xml equivalent?

Thanks
Arun Kumarr
Ranch Hand

Joined: May 16, 2005
Posts: 513

It says so, because you're trying to join resource id (PK) with locale id_locale and id_locale is not present in locale table.
Please verify this example,

http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html#mapping-declaration-join


If you are not laughing at yourself, then you just didn't get the joke.
Nick Carag
Greenhorn

Joined: Dec 08, 2010
Posts: 13
I think that my situation is similar to the one in the example

where PERSON is my RESOURCE and ADDRESS is my LOCALE, and in RESOURCE ther's ID_LOCALE that refers to ID of LOCALE or I'm wrong?
Arun Kumarr
Ranch Hand

Joined: May 16, 2005
Posts: 513

PERSON is my RESOURCE and ADDRESS is my LOCALE, and in RESOURCE there's ID_LOCALE that refers to ID of LOCALE - Yes.
Nick Carag
Greenhorn

Joined: Dec 08, 2010
Posts: 13
So base on this about key
It appears anywhere the parent mapping element defines a join to a new table that references the primary key of the original table

ther's no way to have

insted of


The only solution is to use a ono-to-one and create the Locale Pojo?
Arun Kumarr
Ranch Hand

Joined: May 16, 2005
Posts: 513

Check this
Nick Carag
Greenhorn

Joined: Dec 08, 2010
Posts: 13
First of all, thanks for your time.

I already try this but

- this needs an association table;
- this needs a POJO that maps the table Locale;
- this searches for a java.lang.String map

Just annoying...
Arun Kumarr
Ranch Hand

Joined: May 16, 2005
Posts: 513

Ah! i overlooked when you mentioned locale pojo. I thought you'd mentioned about a link table pojo. My bad.

Did you try using the property-ref column used specifically for legacy.
http://docs.jboss.org/hibernate/core/3.5/reference/en/html/mapping.html#mapping-declaration-key
Nick Carag
Greenhorn

Joined: Dec 08, 2010
Posts: 13
Yes i've tried with property-ref on key and foreign-key on join but nothing changes in the sql generated.
Nick Carag
Greenhorn

Joined: Dec 08, 2010
Posts: 13
Finally I've mapped property localeCode as follow

But is it correct? Is there another way to achieve the same result?
Arun Kumarr
Ranch Hand

Joined: May 16, 2005
Posts: 513

This would run a scalar sub-query. Though this would get your work done, I'm still worried why the join tags is not working.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate join and key tag
 
Similar Threads
Incorrect joins on composite ID
Joining tables
Getting unrequested nested query
joined-subclass problem
Hibernate generating queries with duplicate columns