-->
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.  [ 5 posts ] 
Author Message
 Post subject: composite id, strange insert
PostPosted: Wed May 11, 2011 10:50 am 
Newbie

Joined: Wed May 11, 2011 10:47 am
Posts: 3
Hi,

I have a problem with composite id, it tries to insert composite id's parts into db. Code is as follows:

--------------------------------------------------------------------------

Entity:

Code:
@Entity
@Table(name = "LOV_SET_ELEM")
@IdClass(LovSetElementPK.class)
public class LovSetElement implements Serializable {

   private static final long serialVersionUID = -5088707631626664039L;

   @Id
   @Column(name = "NLOVSETID", insertable = false, updatable = false)
   private long lovSetId;

   @Id
   @Column(name = "NLOVID", insertable = false, updatable = false)
   private long lovId;

   @ManyToOne
   @JoinColumn(name = "NLOVSETID")
   private LovSet lovSet;

   @OneToOne
   @JoinColumn(name = "NLOVID")
   private GenericLov lov;

   public LovSetElement() {

   }

   public LovSetElement(LovSet lovSet, GenericLov lov) {
      this.lovSetId = lovSet.getId();
      this.lovSet = lovSet;
      System.out.println(lov.getId());
      this.lovId = lov.getId();
      this.lov = lov;
   }

   public long getLovSetId() {
      return lovSetId;
   }

   public void setLovSetId(long lovSetId) {
      this.lovSetId = lovSetId;
   }

   public long getLovId() {
      return lovId;
   }

   public void setLovId(long lovId) {
      this.lovId = lovId;
   }

   public LovSet getLovSet() {
      return lovSet;
   }

   public void setLovSet(LovSet lovSet) {
      this.lovSet = lovSet;
   }

   public GenericLov getLov() {
      return lov;
   }

   public void setLov(GenericLov lov) {
      this.lov = lov;
   }

}


--------------------------------------------------------------------------

PK:

Code:
public class LovSetElementPK implements Serializable {
   private static final long serialVersionUID = 3685792951484393301L;

   private long lovSetId;
   private long lovId;
   
   public LovSetElementPK() {
      
   }
   
   public LovSetElementPK(long lovSetId, long lovId) {
      this.lovSetId = lovSetId;
      this.lovId = lovId;
   }

   public long getLovSetId() {
      return lovSetId;
   }

   public void setLovSetId(long lovSetId) {
      this.lovSetId = lovSetId;
   }

   public long getLovId() {
      return lovId;
   }

   public void setLovId(long lovId) {
      this.lovId = lovId;
   }

   @Override
   public boolean equals(Object o) {
      return ((o instanceof LovSetElementPK)
            && lovSetId == ((LovSetElementPK) o).getLovSetId() && lovId == ((LovSetElementPK) o)
            .getLovId());
   }

   @Override
   public int hashCode() {
      return (int) (lovSetId + lovId);
   }
}


--------------------------------------------------------------------------

and after:

LovSetElement lse = new LovSetElement(lovSet, lov);
em.persist(lse);

where both lovSet and lov are valid objects i get:

Code:
Hibernate:
    /* insert mrted.domain.lovhierarchy.LovSetElement
        */ insert
        into
            mtu1142.LOV_SET_ELEM
            (NLOVID, NLOVSETID, lovId, lovSetId)
        values
            (?, ?, ?, ?)
16:21:28,175  WARN JDBCExceptionReporter:100 - SQL Error: 904, SQLState: 42000
16:21:28,176 ERROR JDBCExceptionReporter:101 - ORA-00904: "LOVSETID": invalid identifier



Why those two additional colums ? there is insertable = false etc..... what's wrong with my code ?

Thanks in advance,
Tom


Top
 Profile  
 
 Post subject: Re: composite id, strange insert
PostPosted: Thu May 12, 2011 12:24 am 
Newbie

Joined: Thu May 12, 2011 12:04 am
Posts: 3
Hi tom ,
I hope u got the answer.
Please define the column name in your respective PK class n no need to define coumn name in main class


Top
 Profile  
 
 Post subject: Re: composite id, strange insert
PostPosted: Thu May 12, 2011 2:54 am 
Newbie

Joined: Wed May 11, 2011 10:47 am
Posts: 3
what do you mean ?

delete column annotation in main class, so it's like this:

Code:
@Id
   private long lovSetId;

   @Id
   private long lovId;


and add coluln annotation to PK class, like this:

Code:
@Column(name = "NLOVSETID", insertable = false, updatable = false)
   private long lovSetId;

   @Column(name = "NLOVID", insertable = false, updatable = false)
   private long lovId;


but that doesn't work, i got:

Code:
Caused by: org.hibernate.MappingException: broken column mapping for: lov.id of: mrted.domain.lovhierarchy.LovSetElement
   at org.hibernate.persister.entity.AbstractPropertyMapping.initPropertyPaths(AbstractPropertyMapping.java:143)
   at org.hibernate.persister.entity.AbstractPropertyMapping.initIdentifierPropertyPaths(AbstractPropertyMapping.java:206)


Top
 Profile  
 
 Post subject: Re: composite id, strange insert
PostPosted: Thu May 12, 2011 4:06 am 
Newbie

Joined: Thu May 12, 2011 12:04 am
Posts: 3
hi Tom ,Sorry I was not much clear in my answer .define an object of your Pk class in your main class and give annotation @Id for that object .

Say
class Testmain{

@Id
private Testpk test
}

note: no need to define the variables lovSetId & lovId in the main class .Please define them with column annotation only in PK class .

Hope this time it will work .:D


Top
 Profile  
 
 Post subject: Re: composite id, strange insert
PostPosted: Thu May 12, 2011 5:36 am 
Newbie

Joined: Wed May 11, 2011 10:47 am
Posts: 3
Still not working :)

If i delete the long values, so in entity class i have:

Code:
@Id
   private LovSetElementPK id;

   public LovSetElementPK getId() {
      return id;
   }

   public void setId(LovSetElementPK id) {
      this.id = id;
   }


I get:

Code:
Caused by: org.hibernate.AnnotationException: Unable to find properties (lovId, lovSetId) in entity annotated with @IdClass:mrted.domain.lovhierarchy.LovSetElement
   at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:774)


Or should those objects in PK not be longs, but of the Entity types and names ?


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