-->
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.  [ 4 posts ] 
Author Message
 Post subject: Where to put @GeneratedValue when using an Embedded Pk
PostPosted: Tue Oct 31, 2006 3:16 pm 
Newbie

Joined: Wed Oct 11, 2006 5:16 pm
Posts: 5
Hi,
I'm having some problems persisting new objects in my db. I've gotten the update/delete to work flawlessly, but the creation just isn't flying.

I have auto-increment set on the primary key of the table. We're using Embedded Pk's in our entities, and I'm not sure where the @GeneratedValue should go. I've tried on the @Column in the embedded class, next to the @Id, pretty much everywhere. Nothing seems to work.

I keep getting IdentifierGenerationException: ids for this class must be manually assigned before calling save()

Code's below for the Entity, the pk and the simple test

Code:
@Entity
@Table(name = "account_preferences")
public class AccountPreference extends Value<AccountPreferencePk> {
   private static final long serialVersionUID = 5433615494763666998L;

   /**
    * Unique id of this account preference
    */
   private AccountPreferencePk pk;

   /**
    * The type of preference this is
    */
   private String preferenceKey;

   /**
    * String representation of the value of this preference
    */
   private String preferenceValue;

   /**
    * When was the preference last modified
    */
   private Date lastModifiedDate;

   /**
    * User profile that the preference belongs to
    */
   private UserProfile userProfile;

   public AccountPreference() {
      super();
   }

   public AccountPreference(String key, String value) {
      this();
      this.preferenceKey = key;
      this.preferenceValue = value;
   }

   public AccountPreference(String key, String value, Date date) {
      this(key, value);
      this.lastModifiedDate = date;
   }

   @Id
   @Embedded
   @GeneratedValue(strategy = GenerationType.AUTO)
   public AccountPreferencePk getPk() {
      return this.pk;
   }

   public void setPk(AccountPreferencePk key) {
      this.pk = key;
   }


Code:
@Embeddable
public class AccountPreferencePk extends PrimaryKeyValue {
   private static final long serialVersionUID = 1L;

   public long pkValue;

   public AccountPreferencePk() {
   }

   public AccountPreferencePk(long pkValue) {
      super(pkValue);
   }

   @GeneratedValue(strategy = GenerationType.AUTO)
   @Column(name = "account_preferences_pk")
   public long getPkValue() {
      return this.pkValue;
   }

   public void setPkValue(long pkValue) {
      this.pkValue = pkValue;
   }
}


And the most basic test ...
Code:

   @Test
   public void testCRUD() {
      AccountPreference pref = new AccountPreference("my_test", "is so cool", new Date());
      pref = dao.makePersistent(pref);
      assertNotNull(pref.getPk().pkValue);
   }


I'm using JBoss 4 w/ EJB3 plugin as our deploy server, and the embeddable ejb3 plugin as our test server.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 07, 2006 6:34 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
you cannot use @GeneratedValue on a part of the primary key

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 07, 2006 6:37 pm 
Newbie

Joined: Wed Oct 11, 2006 5:16 pm
Posts: 5
Our PrimaryKey is only a long. It's just that it's embedded in it's own class.

Is there no way to generate values for a class that uses an embedded id?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 07, 2006 8:24 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
if you map it as @Id (not an embeddable object) there should be a way to write a custom UserType for AccountPreferencePk

_________________
Emmanuel


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