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.  [ 2 posts ] 
Author Message
 Post subject: composite foreign key as part of composite primary key
PostPosted: Wed Jan 28, 2009 6:42 pm 
Newbie

Joined: Wed Jun 11, 2008 5:21 pm
Posts: 2
Hello,

i'm trying to define a complex composite key szenario but i'm still getting errors.

situation:

1. table MASTERDATA ( xxx, yyy, zzz; pk (xxx,yyy) )

2. table USAGEDATA (aaa,bbb,xxx,yyy, tttt); pk(aaa,bbb,xxx,yyy))
with "virtual" foreignKey(xxx,yyy) references MASTERDATER.

the foreign key is "virtual" which means, that the key is not defined in the datebase. it's only present in the logical model. the fields USAGEDATA.xxx and USAGEDATA.yyy where primary and foreig key at once.

this is my attemt:

Code:
@Embeddable
public class MasterdataPk
{
String xxx;
String yyy;
}

@Entity
@Table(name="MASTERDATA")
public class Masterdata
{
  @Id MasterdataPk id;
  String zzz;
}


@Embeddable
public class UsageDataPk
{
Masterdata masterdata;
String aaa;
String bbb;
}

@Entity
@Table(name="USAGEDATA")
public class UsageData
{
@Id UsageDataPk id;
String ttt;
}


hibernate generates:
Code:
select ... a.masterdata_xxx, a. masterdata_yyy from USAGEDATA a ...


but i'm expecting a join like
Code:
select ... b.xxx, b._yyy from USAGEDATA a, MASTERDATA b where a.xxx = b.xxx and a.yyy = b.yyyy ...


i'm getting confused ;-)

thanks in advance


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 29, 2009 5:52 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
try to use @EmbeddedId instead of @Id, I am wondering why it works without.

Also you should define a toOne-Relationship:
Code:
@Embeddable
public class UsageDataPk
{
@ManyToOne
Masterdata masterdata;
String aaa;
String bbb;
}

_________________
-----------------
Need advanced help? http://www.viada.eu


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