-->
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.  [ 1 post ] 
Author Message
 Post subject: Composite Key Problem
PostPosted: Mon Feb 06, 2006 11:36 am 
Newbie

Joined: Mon Mar 21, 2005 9:41 am
Posts: 12
Hello,

I want to map a composite key consiting of two string: userName and role
So I created a Pk-class that has the two properties and annotated it as Embeddable. The field in the class is annotated with EmbeddableId.
The configuration is successful but when I want to persist an object, an IllegalArgument Exception is occuring. Does anybody know, what I am doing wrong?
Thank you

The code looks like that:

Code:
@Entity()
@Table(name = "user_roles")
public class DbUserRole  implements java.io.Serializable {

   
   private UserRolePK id;

    // Constructors

    public DbUserRole(String userName, String role)
   {
      super();
      // TODO Auto-generated constructor stub
      //this.id = new UserRolePK(userName, role);
      this.id = new UserRolePK();
      this.setRole(role);
      this.setUserName(userName);
   }


   public DbUserRole(String role)
   {
      super();
      this.id = new UserRolePK(role);
   }


   /** default constructor */
    public DbUserRole() {
    }

    @EmbeddedId()
    public UserRolePK getId(){
      return this.id;
   }
   
    public void setId(UserRolePK pk){
       this.id = pk;
    }
   
    @Column(nullable = false, length = 255, insertable = false, updatable = false)
    public String getUserName(){
       return this.id.getUserName();
    }
   
   public void setUserName(String userName){
      this.id.setRole(userName);
   }
   
   @Column(insertable = false, updatable = false, nullable = false, length = 255)
   public String getRole(){
      return this.id.getRole();
   }
   
   public void setRole(String role){
      this.id.setRole(role);
   }
}

/**
* Realisiert den Composite Key von UserRole.
*
* @author Mathias Peters
* @version $Id: DbUserRole.java,v 1.5 2006/01/03 11:33:35 mathias peters Exp $
*/
@Embeddable
class UserRolePK implements Serializable{
   
   static final long serialVersionUID = 0;
//    Fields   
   private String userName;
   private String role;
   
   public UserRolePK()
   {
      
   }
   
   public UserRolePK(String userName, String role){
      this.userName = userName;
      this.role = role;
   }
   
   public UserRolePK(String role){
      this.role = role;
   }
   
   public String getRole()
   {
      return role;
   }


   public void setRole(String role)
   {
      this.role = role;
   }


   public String getUserName()
   {
      return userName;
   }


   public void setUserName(String userName)
   {
      this.userName = userName;
   }
   
   public boolean equals(Object obj){
      if(this == obj) return true;
      if(!(obj instanceof UserRolePK))
         return false;
      final UserRolePK pkObj = (UserRolePK)obj;
      
      if(!pkObj.getUserName().equals(this.getUserName()))
         return false;
      
      if(!pkObj.getRole().equals(this.getRole()))
         return false;
      return true;
   }
   
   public int hashCode(){
      return (29 * this.getUserName().hashCode()) + (31 * this.getRole().hashCode());
   }
   
}




Code:
[color=red]
The Exception is the following:
3274 [main] ERROR org.hibernate.property.BasicPropertyAccessor  - IllegalArgumentException in class: i2.smartsearch.db.DbUserRole, getter method of property: id
Exception in thread "main" org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of i2.smartsearch.db.DbUserRole.id
   at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:171)
   at org.hibernate.tuple.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:176)
   at org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:3256)
   at org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:2982)
   at org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:181)
   at org.hibernate.event.def.AbstractSaveEventListener.getEntityState(AbstractSaveEventListener.java:459)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:84)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
   at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:501)
   at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:495)
   at org.hibernate.engine.CascadingAction$1.cascade(CascadingAction.java:134)
   at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:213)
   at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:157)
   at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:108)
   at org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:290)
   at org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:185)
   at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:160)
   at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:108)
   at org.hibernate.engine.Cascade.cascade(Cascade.java:248)
   at org.hibernate.event.def.AbstractFlushingEventListener.cascadeOnFlush(AbstractFlushingEventListener.java:130)
   at org.hibernate.event.def.AbstractFlushingEventListener.prepareEntityFlushes(AbstractFlushingEventListener.java:121)
   at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:65)
   at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
   at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:905)
   at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:345)
   at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
   at i2.smartsearch.db.DbUserManager.update(DbUserManager.java:54)
   at i2.smartsearch.db.HibernateUtil.main(HibernateUtil.java:254)
Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:585)
   at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:145)
   ... 27 more



[/color]


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.