-->
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.  [ 10 posts ] 
Author Message
 Post subject: single FK to composite PK
PostPosted: Wed Dec 08, 2004 9:37 am 
Newbie

Joined: Wed Dec 08, 2004 9:13 am
Posts: 6
In my database, I have a "code/decode" table that is used to store key value pair information. The primary key is made up of two columns: TYPE and CODE.

I then have multiple associated tables that contain only the CODE as the foreign key. The TYPE is well known and constant for all associations (i.e. a status TYPE or a person TYPE).

I've been trying to use "discriminator-value" with "subclass" to create these speicific TYPE objects, but I get a mapping exception (below).

The only way I seem to be able to get around this is to create a view for each TYPE, and map my subclass to the view.

Is what I'm attempting possible?



Hibernate version: 2.1.6

Mapping Documents:
<class name="Codec" >
<composite-id>
<key-property name="type" column="TYPE"/>
<key-property name="code" column="CODE"/>
</composite-id>

<discriminator column="TYPE"/>

<property name="description"/>

<subclass name="Status" discriminator-value="STATUS"/>
</class>


<class name="Person">
<id name="id" column="ID">
<generator class="assigned"/>
</id>

<many-to-one name="status" column="STATUS_CODE"
class="Status"/>
</class>


Full stack trace of any exception that occurs:
net.sf.hibernate.MappingException: Foreign key (PROD.PROV_MSTR [PROV_STS_CD])) must have same number of columns as the referenced primary key (PROD.PROV_CS [PROV_CS_TP,PROV_CS_CD])
at net.sf.hibernate.mapping.ForeignKey.setReferencedTable(ForeignKey.java:60)
at net.sf.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:691)
at net.sf.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:666)
at net.sf.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:784)
at main.TableDump.<init>(TableDump.java:35)
at main.TableDump.main(TableDump.java:45)
java.lang.NullPointerException
at main.TableDump.listEvents(TableDump.java:66)
at main.TableDump.main(TableDump.java:51)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 08, 2004 9:47 am 
Newbie

Joined: Wed Dec 08, 2004 8:56 am
Posts: 12
Location: China ChengDu
Quote:
Foreign key (PROD.PROV_MSTR [PROV_STS_CD])) must have same number of columns as the referenced primary key (PROD.PROV_CS [PROV_CS_TP,PROV_CS_CD])


That is cause why.
Your FK associated two PK

Good Luck!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 08, 2004 9:53 am 
Newbie

Joined: Wed Dec 08, 2004 9:13 am
Posts: 6
Thanks for the reply rosenjiang.

What I'm really looking for is a suggestion on how to solve the problem in the hibernate mapping file. Changing the database is not an option.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 08, 2004 10:06 am 
Newbie

Joined: Wed Dec 08, 2004 8:56 am
Posts: 12
Location: China ChengDu
Quote:
What I'm really looking for is a suggestion on how to solve the problem in the hibernate mapping file. Changing the database is not an option.


Yeah! That's my question also.[/quote]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 08, 2004 10:06 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Probably possible to hack around it by using property-ref on the many-to-one


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 08, 2004 10:13 am 
Newbie

Joined: Wed Dec 08, 2004 8:56 am
Posts: 12
Location: China ChengDu
well, Could help me solve this question http://forum.hibernate.org/viewtopic.php?t=936886 ?

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 08, 2004 10:16 am 
Newbie

Joined: Wed Dec 08, 2004 9:13 am
Posts: 6
michael wrote:
Probably possible to hack around it by using property-ref on the many-to-one


I tried the property-ref, but it seems property-ref has to reference an actual "property" as defined in the map, and cant' reference a composite-id key.

I can't define constant properties, can I?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 08, 2004 10:35 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Well define one! use update=false, insert=false - at least try for more than 10 minutes before posting to the forum again. Guys, be a bit creative.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 08, 2004 10:41 am 
Newbie

Joined: Wed Dec 08, 2004 9:13 am
Posts: 6
michael wrote:
Well define one! use update=false, insert=false - at least try for more than 10 minutes before posting to the forum again. Guys, be a bit creative.


Seems a bit presumptuous of you to assume what I have or have not done.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 08, 2004 11:14 am 
Expert
Expert

Joined: Thu Jan 29, 2004 2:31 am
Posts: 362
Location: Switzerland, Bern
@jasontaft

If I got your problem right you sould have a look at the <any> type mapping.

BTW: The solution you mentioned in your first post works as well (with the fix from rosenjiang). If you have composite pk it's actually the only way to do any type mappings since the <any> tag doesn't suport this.

HTH
Ernst


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