-->
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: @GeneretedValue in @EmbeddedId class
PostPosted: Wed Aug 27, 2008 8:54 am 
Newbie

Joined: Wed Aug 27, 2008 7:51 am
Posts: 2
Hi,

I'm facing a problem regarding the using of @GeneretedValue annotation in a composite primary key.

I have a first table named PARENT.
Here's its colums :
DBKEY NUMBER(9)
...

Here's this mapped class :
Code:
@Entity
@Table (name = "PARENT")
public class ParentBO implements Serializable {

   /**
    *
    */
   private static final long serialVersionUID = -170862807387203780L;

   private Long dbKey;
   
   
   /**
    * @return the dbKey
    */
   @Id
   @GeneratedValue(generator="increment")
   @GenericGenerator(name="increment", strategy = "increment")
   @Column (name = "DBKEY", precision = 9, nullable = false)
   public Long getDbKey() {
      return dbKey;
   }

   /**
    * @param dbKey the dbKey to set
    */
   public void setDbKey(Long dbKey) {
      this.dbKey = dbKey;
   }


   /**
    * @return the children
    */
   @OneToMany (mappedBy = "childPk.parent", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
   @OrderBy
   public Set<ChildBO> getChildren() {
      return children;
   }

   /**
    * @param children the children to set
    */
   public void setChildren(Set<ChildBO> children) {
      this.children = children;
   }
}


This table has a oneToMany relation with the second table named CHILD.
Here's its colums :
DBKEY NUMBER(9)
SEQNR NUMBER(4)
...

Here's the mapped class :

Code:
@Entity
@Table (name = "CHILD")
public class ChildBO implements Serializable {

   /**
    *
    */
   private static final long serialVersionUID = -6953833174988560128L;

   private ChildPk childPk;

   /**
    * @return the childPk
    */
   @EmbeddedId
   public TitlePk getChildPk() {
      return titlePk;
   }

   /**
    * @param childPk the childPk to set
    */
   public void setChildPk(ChildPk childPk) {
      this.childPk = childPk;
   }


}


And the mapped class for the primary key :

Code:
public class ChildPk implements Serializable {

   /**
    *
    */
   private static final long serialVersionUID = 5758018014541880382L;

   private ParentBO parent

   private Integer sequenceNumber;


   /**
    * @return the parentBO
    */
   @ManyToOne(fetch = FetchType.EAGER)
   @JoinColumn(name = "DBKEY")
   public ParentBO getParent() {
      return parent;
   }

   /**
    * @param parentthe parentto set
    */
   public void setParent(ParentBO parent) {
      this.parent = parent;
   }

   /**
    * @return the sequenceNumber
    */
   @GenericGenerator(name="increment", strategy = "increment")
   @GeneratedValue(generator="increment")
   @Column (name = "SEQNR", precision = 4)
   public Integer getSequenceNumber() {
      return sequenceNumber;
   }

   /**
    * @param sequenceNumber the sequenceNumber to set
    */
   public void setSequenceNumber(Integer sequenceNumber) {
      this.sequenceNumber = sequenceNumber;
   }
}



When I save the ParentBO object containing 2 children, the @GeneratedValue(generator="increment") in the ChildPk class is ignored by hibernate and so the sequenceNumber is null.

As a result, an exception org.hibernate.NonUniqueObjectException is throwed. Indeed, the 2 children objects have a the same DBKEY value and the same SEQNR value, but null ! not good...

Hibernate should select the max id for SEQNR according this way : select max(seqnr) from child where dbkey=<dbkey of parent>.

So my question, can i use a @GeneratedValue in a EmbeddedId
class (composite primary key) ?

Hibernate Version :hibernate-core-3.3.0.SP1, hibernate-annotations-3.4.0.GA

Database :Oracle 10g

Thanks for reading.


Top
 Profile  
 
 Post subject: Re: @GeneretedValue in @EmbeddedId class
PostPosted: Wed Aug 27, 2008 11:07 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

mrenou wrote:
So my question, can i use a @GeneratedValue in a EmbeddedId
class (composite primary key) ?


the sort answer is - no you cannot. When you use @Embeddable or @EmbeddedId the id values have to be assigned. As you noticed, your @GeneratedValue annotation gets ignored.

Depending on your usecase there might be other ways of solving your problem. Maybe you could implement a JPA callback handler which generates the sequence number?

--Hardy


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 27, 2008 12:27 pm 
Newbie

Joined: Wed Aug 27, 2008 7:51 am
Posts: 2
that's what I thought

Thnaks a lot


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.