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: Hibernate many-to-one composite key as foreign key
PostPosted: Fri Sep 18, 2009 10:14 am 
Newbie

Joined: Fri Sep 18, 2009 10:06 am
Posts: 1
Hi,

I have two table. Table1 has composite primary key. Table2 has table1 composite primary key as foreign key.

CREATE TABLE Group (
gid varchar(128) NOT NULL default '',
cId integer NOT NULL,
name varchar(256) NOT NULL default '',
PRIMARY KEY ( gid, cId ),
foreign key (cId) references cluster(cId)
) ;

CREATE TABLE GroupMember (
gmid varchar(128) NOT NULL default '',
cId integer NOT NULL,
fkgroup varchar(128) NOT NULL default '',
priority integer default NULL,
PRIMARY KEY ( gmid, cId ),
foreign key (cId) references cluster(cId),
foreign key (fkgroup,cId) references Group(gid, cId)
) ;

cId is primary key of another table, hope this table definition is not required.

I used HibernateTool 3.2.4 to generate Mapping File and POJOs.

Here is the mapping File generated for table GroupMember

<hibernate-mapping>
<class name="my.test.pkg.Groupmember" table="GroupMember">
<composite-id name="id" class="my.test.pkg.GroupMemberId">
<key-property name="gmid" type="string">
<column name="gmid" length="128" />
</key-property>
<key-property name="cId" type="int">
<column name="cId" />
</key-property>
</composite-id>
<many-to-one name="Group" class="my.test.pkg.Group" update="false" insert="false" fetch="select">
<column name="fkgroup" length="128" not-null="true" />
<column name="cId" not-null="true" />
</many-to-one>
<many-to-one name="cluster" class="my.test.pkg.Cluster" update="false" insert="false" fetch="select">
<column name="cId" not-null="true" />
</many-to-one>
<property name="priority" type="java.lang.Integer">
<column name="priority" />
</property>
</class>
</hibernate-mapping>

Related POJO File looks like this.

public class GroupMemberId implements java.io.Serializable {
private String gmid;
private int cId;
}

public class GroupMember implements java.io.Serializable {

private GroupmemberId id;
private Group group;
private Cluster cluster;
private Integer priority;
}

Now when i attempt to Save GroupMember object by filling all member variable, Hibernate only insert 3 column, one column fkgroup is not getting inserted.
insert into GroupMember (priority, gmid, cId) values (?, ?, ?)

I'm unable to save column fkgroup of GroupMember table and get following exception

SEVERE: SQL Anywhere Error -194: No primary key value for foreign key 'group' in table 'GroupMember'
org.hibernate.exception.ConstraintViolationException: could not insert: [my.test.pkg.GroupMember]


Then i realized that, insert="false" is set in many-to-one relation. This could cause issue for fkgroup not to being inserted. I changed property insert="true" in many-to-one relation, but then i get following exception

org.hibernate.MappingException: Repeated column in mapping for entity: my.test.pkg.GroupMember column: cId (should be mapped with insert="false" update="false")

For now, i have no idea how can one insert/save GroupMember with fkgroup foreign key.

I have checked forum, but couldn't find any solution. Appreciate any help for resolving this issue.


Top
 Profile  
 
 Post subject: Re: Hibernate many-to-one composite key as foreign key
PostPosted: Sun Sep 20, 2009 3:37 am 
Senior
Senior

Joined: Mon Jul 07, 2008 4:35 pm
Posts: 141
Location: Berlin
Hi kbhattad,

the exception
Quote:
org.hibernate.MappingException: Repeated column in mapping for entity: my.test.pkg.GroupMember column: cId (should be mapped with insert="false" update="false")

indicates that you have mapped a column, in this case cId, more than once.

You have it within two <many-to-one> elements:
Code:
...
  <many-to-one name="Group" class="my.test.pkg.Group" update="false" insert="false" fetch="select">
    <column name="fkgroup" length="128" not-null="true" />
    <column name="cId" not-null="true" />
  </many-to-one>
  <many-to-one name="cluster" class="my.test.pkg.Cluster" update="false" insert="false" fetch="select">
    <column name="cId" not-null="true" />
  </many-to-one>
...

I could imagine that the second <many-to-one> association to my.test.pkg.Cluster is not needed since you should receive the value from the <many-to-one> association to my.test.pkg.Group that already gets the FK from my.test.pkg.Cluster.

CU
Froestel

P.S.: It is much easier to view your code snippets if you'd surround them in BBCode Code-Tags to make it appear formatted. Try the buttons below the subject field.

_________________
Have you tried turning it off and on again? [Roy]


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.