-->
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.  [ 6 posts ] 
Author Message
 Post subject: mapping composite primary key
PostPosted: Mon Apr 21, 2008 7:17 am 
Newbie

Joined: Mon Apr 21, 2008 7:11 am
Posts: 4
Location: Sevilla, Spain
Hi all!

Firstly, excuse my english!

I use hibernate.annotations-3.1beta8; and this works fine with my data base (oracle 10g).

I have this class

class person {

int id
int dni

String name
...}

ID and DNI are primary key in the data base; but i don't know mapping this with Annotations.

Help please!!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 21, 2008 12:16 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Hi,
look for @IdClass
http://www.hibernate.org/hib_docs/annotations/reference/en/html_single/#entity-mapping-identifier

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 22, 2008 3:40 am 
Newbie

Joined: Mon Apr 21, 2008 7:11 am
Posts: 4
Location: Sevilla, Spain
thanks a lot but i have this problem:

Class Municipio
Code:
@Table(name = "MUNICIPIOS")
@Entity
@IdClass(MunicipioPK.class)
public class Municipio ...{

...

   @Id
   @Column(name = "PROVINCIA"  , nullable = false )
   public java.math.BigDecimal getProvincia() {
      return provincia;
   }

   @Id
   @Column(name = "MUNICIPIO"  , nullable = false )
   public java.math.BigDecimal getMunicipio() {
      return municipio;
   }
}



Class MunicipioPK
Code:
//imports...
@Embeddable
   public class MunicipioPK {
       public java.math.BigDecimal provincia;
       public java.math.BigDecimal municipio;

       @Id
       public java.math.BigDecimal getProvincia() {
          return provincia;
       }

       @Id
       public java.math.BigDecimal getMunicipio() {
          return municipio;
       }

//setters included

}



Then, i get this error:

Quote:
FATAL HibernateSessionHelper - Error while configuring the hibernate beans
org.hibernate.AnnotationException: entity.municipiopk.MunicipioPK has not persistent id property


I have added MunicipioPK to config file. :(

Thanks again!

EDIT:
I use hibernate 3.1.2. and hibernate annotations 3.1b8.

EDIT2:
I have not redefined hashcode() and equals() in Municipio and MunicipioPK class. it´s necesary?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 22, 2008 10:11 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Hi,
in your MunicipioPK you should NOT have @Id annotations.
Also remember to write a consistent equals() and hashcode() considering all PK participating fields.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 23, 2008 6:56 am 
Newbie

Joined: Mon Apr 21, 2008 7:11 am
Posts: 4
Location: Sevilla, Spain
thanks again, i have deleted @id annotations in MunicipioPK and don't work.

I have mapping Municipio.class without hibernate annotattions, using <composite-id> and i have the same error.

May be from bad configuration of data base. I'm investigating it.

Thanks!!

PD: i'm newbie programmer (only 1 month) <|:U


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 23, 2008 7:31 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
You should post your errors, a full stacktrace could help.

Also you are going to face lots of similar problems, learning mapping is not very easy for beginners; I would suggest you to create the tables first and then use Hibernate Tools to generate the entities from your schema, so you can see how it maps them. I learned a lot this way..

_________________
Sanne
http://in.relation.to/


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