-->
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: EJB3 and composite primary key:ids for this class must be ma
PostPosted: Tue Aug 09, 2005 3:08 pm 
Newbie

Joined: Tue Aug 09, 2005 3:03 pm
Posts: 14
Hi,

I am not sure if this is the correct forum to ask the question. I am trying to run an example with EJB3 on jboss with HSQL database.

I am getting the error: "ids for this class must be manually assigned before calling save" when i try to save a class with a composite pk.

It is obvious that the container is expecting to set the my pk before saving. Here is the problem: The composite key contains two fields. I wanted to generate one of the fields myself but let the system generate the other field in the key. Here is my code


my pk:
Code:
@Embeddable(access = AccessType.FIELD)
public class CategoryPK implements Serializable {

private int id;
private String type;

  //this is the key I wanted auto generate
  public int getId() {
return id;
}

  //this is the key I wanted the user to input
public String getType() {
return type;

public boolean equals(Object obj){\\some code}
  public int hashCode() {\\some code}

}
}


My class


Code:
@Entity
@Table(name = "Catgory")
public class Category implements Serializable {

private CategoryPK categoryPK;
private String description;

// my pk
@Id(generate = GeneratorType.NONE)
public CategoryPK getCategoryPK() {
return categoryPK;
}
}

Any suggestions?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 09, 2005 3:40 pm 
Expert
Expert

Joined: Thu Sep 04, 2003 8:23 am
Posts: 368
Default generators in Hibernate are for simple id fields. If you want hibernate to generate half of your composite id, you may try to create your own custom generator based on hibernate ones. Take a look at the classes in the org.hibernate.id package to see how you can write a generator.

Be careful, in your case, you will create and assigned your id instance to your domain object. Your generator will have to get this object, check if the field it generates is null and then give it a value. I don't know what kind of unsaved-value you define in this case.

Seb

_________________
Seb
(Please don't forget to give credits if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 09, 2005 4:29 pm 
Newbie

Joined: Tue Aug 09, 2005 3:03 pm
Posts: 14
What if I want to auto generate both fields of my composite primary key. Is there a way to do that? it seems that when using composite keys, the standard set assumes that user will provice all of the key attributes.

Khalid


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 10, 2005 2:20 am 
Expert
Expert

Joined: Thu Sep 04, 2003 8:23 am
Posts: 368
It is true that in the hbm grammar, the generator tag is only defined inside id tag.
So, if you want to generate both fields, you can define an id instead of a composite-id like that
Code:
        <id
            name="id"
            type="com.MyCategoryPkType"
            unsaved-value="none"
        >
            <generator class="com.MyCategoryPkGenerator"/>
        </id>


You then have to define the type MyCategoryPkType as a UserType that store CategoryPk and MyCategoryPkGenerator that generates new instances of CategoryPk.

We did that in our application but the type of the id is the same for all the classes. In you case, I think it will be too much work if you create two more classes for each domain object.

_________________
Seb
(Please don't forget to give credits if you found this answer useful :)


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.