-->
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.  [ 3 posts ] 
Author Message
 Post subject: mapping for a table that only has 2 foreign keys
PostPosted: Thu Jun 19, 2008 4:33 am 
Newbie

Joined: Sun Jul 01, 2007 12:15 pm
Posts: 7
Location: Philippines
how do i create the hbm.xml file of a db table that only has 2 foreign keys as it's fields? It has no other fields but the 2 foreign keys..

I tried this:

<hibernate-mapping>
<class name="com.ibm.pli.databeans.CustomerCurrency" table="tab1" >

<many-to-one name="customer_id" class="com.Customer"/>
<many-to-one name="currency_label_id" class="com.Prod"/>
</class>
</hibernate-mapping>

But this is not working..it's asking for an ID.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 19, 2008 4:40 am 
Newbie

Joined: Wed Jun 18, 2008 1:12 am
Posts: 3
hi,

Yes, you have to create primary key relation or composite key relation some thing like this,

<hibernate-mapping>

<class
name="com.ibm.pli.databeans.CustomerCurrency"
table="tab1">
<composite-id>
<key-many-to-one name="customer" class="com.Customer" column="customer_id" />
<key-many-to-one name="currencylabelid" class="com.Prod" column="currency_label_id" lazy="false"/>
</composite-id>
</class>
</hibernate-mapping>

i hope this will solve your problem.
i also had this problem, and i tried to create composite key with two foreign keys. and its went away.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 23, 2008 10:47 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
It can also be done quite easily with annotations:

Code:
package com.examscam.mappings;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
import org.hibernate.Session;
import com.examscam.HibernateUtil;

@Entity
@IdClass(com.examscam.mappings.CompoundKey.class)
public class Fracture {
  Long bankId; Long userId; String bone;

  @Id
  public Long getBankId() {return bankId;}

  @Id
  public Long getUserId() {return userId;}
  public void setBankId(Long bankId){this.bankId = bankId;}
  public void setUserId(Long userId){this.userId = userId;}
  public String getBone() {return bone;}
  public void setBone(String bone) {this.bone = bone;}

  public static void main(String args[]) {
    Fracture bone = new Fracture();
    bone.setBone("arm");
    bone.setBankId( new Long(99));
    bone.setUserId(new Long(88));
    HibernateUtil.recreateDatabase();
    Session session=HibernateUtil.beginTransaction();
    session.save(bone);
    HibernateUtil.commitTransaction();
  }

}



There are other approaches to compound primary key syntax as well. Here's a little Hibernate3 tutorial on the topic of creating compound primary keys for your entities:

http://www.hibernate.org/43.html

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


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