java - How do I join tables on non-primary key columns? -


i have issue join tables on object in orm class hierarchy join column not primary key of base class due lagacy database structure. here example of table design:

create table "sch"."foo" (         "ownerid"       number(10,0) not null enable,         "fooid"         number(10,0) not null enable,         constraint "foo_pk" primary key ("ownerid", "fooid")         constraint "foo_fk1" foreign key ("ownerid") references "sch"."owners" ("ownerid") enable )  create table "sch"."bar" (         "barid"             number(10,0) not null enable,         "fooid"             number(10,0)         constraint "bar_pk" primary key ("barid") ) 

and here mappings (unesessary infomation removed)

@entity @idclass(fooid.class) @table(name = "foo") public class foo implements java.io.serializable {     @id     @column(name = "ownerid")     private biginteger ownerid;      @id     @sequencegenerator(name = "foo_generator", sequencename = "seq_foo")     @generatedvalue(generator = "foo_generator")     @column(name = "fooid")     private biginteger id;      @onetomany(fetch = fetchtype.lazy)     @joincolumn(name = "fooid", referencedcolumnname = "fooid")     @fetch(value = fetchmode.subselect)     @cascade(value = {cascadetype.all})     private set<bar> bar = new linkedhashset<bar>(0); }   @entity @table(name = "bar") public class bar implements java.io.serializable {     @id     @column(name = "barid")     private biginteger id;      @manytoone(fetch = fetchtype.lazy)     @joincolumn(name = "fooid", referencedcolumnname = "fooid")     private foo foo; } 

this fails exception:

caused by: org.hibernate.annotationexception: referencedcolumnnames(fooid) of com.package.bar.foo referencing com.package.foo not mapped single property     @ org.hibernate.cfg.binderhelper.createsyntheticpropertyreference(binderhelper.java:204)     @ org.hibernate.cfg.toonefksecondpass.dosecondpass(toonefksecondpass.java:114)     @ org.hibernate.cfg.configuration.processendofqueue(configuration.java:1580)     @ org.hibernate.cfg.configuration.processfksecondpassinorder(configuration.java:1503)     @ org.hibernate.cfg.configuration.secondpasscompile(configuration.java:1419)     @ org.hibernate.cfg.configuration.buildmappings(configuration.java:1375) 

could please solution?

you must not map bidirectional association twice. 1 side must marked inverse of many side, using mappedby attribute:

@onetomany(fetch = fetchtype.lazy, mappedby = "foo") @fetch(value = fetchmode.subselect) @cascade(value = {cascadetype.all}) private set<bar> bar = new linkedhashset<bar>(0);  ...  @manytoone(fetch = fetchtype.lazy) @joincolumn(name = "fooid", referencedcolumnname = "fooid") private foo foo; 

there no reason tell hibernate twice association mapped join column fooid. , doing error, because defines 2 different unidirectional associations rather 1 bidirectional association.

edit

the above should work, doesn't due following hibernate bug: it's hibernate bug. see hhh-4284.

to circumvent problem, since fooid enough ensure uniqueness, solution remove @id annotation owner id , @idclass annotation.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -