-->
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.  [ 7 posts ] 
Author Message
 Post subject: Table having Primary Key and Identity Column
PostPosted: Mon Jul 04, 2011 10:53 am 
Newbie

Joined: Thu Jun 23, 2011 5:08 am
Posts: 9
I have a table which an identity column and a primary key.

CREATE TABLE &LIB/TABLE_STATE
(
ROWID NUMERIC(16)
UNIQUE GENERATED ALWAYS AS IDENTITY (CACHE 20 CYCLE ORDER),
USERID CHAR(10) NOT NULL,
CODE_TYPE VARCHAR(20) NOT NULL,
PRIMARY KEY (USERID, CODE_TYPE),
);

Code:
@Entity
@Table(name = "TABLE_STATE")
public class TableState implements Serializable {
   private static final long serialVersionUID = 1L;

   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   @Column(name = "ROWID", nullable = false, length = 255)
   private Long rowId;

   @EmbeddedId
   @GeneratedValue(strategy = GenerationType.AUTO)
   private TableStateKey tableStateKey;
}


Code:
@Embeddable
public class TableStateKey implements Serializable {
   private static final long serialVersionUID = 1L;

   @Column(name = "USERID", nullable = false, length = 10)
   @Type(type = "trimString")
   private String userId;

   @Column(name = "CODE_TYPE", nullable = false, length = 20)
   @Type(type = "trimString")
   private String codeType;
}


When i perform an object persist using entity manager, it throws the below error.


javax.persistence.PersistenceException: org.hibernate.PropertyAccessException: could not set a field value by reflection setter of TableState.rowId
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1235)
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1168)
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1174)
at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:674)
at Main.method(Main.java:70)
at Main.main(Main.java:17)
Caused by: org.hibernate.PropertyAccessException: could not set a field value by reflection setter of TableState.rowId
at org.hibernate.property.DirectPropertyAccessor$DirectSetter.set(DirectPropertyAccessor.java:151)
at org.hibernate.mapping.Component$ValueGenerationPlan.execute(Component.java:438)
at org.hibernate.id.CompositeNestedGeneratedValueGenerator.generate(CompositeNestedGeneratedValueGenerator.java:122)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:122)
at org.hibernate.ejb.event.EJB3PersistEventListener.saveWithGeneratedId(EJB3PersistEventListener.java:69)
at org.hibernate.event.def.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:179)
at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:135)
at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:61)
at org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:800)
at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:774)
at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:778)
at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:668)
... 2 more
Caused by: java.lang.IllegalArgumentException: Can not set java.lang.Long field TableState.rowId to org.hibernate.id.IdentifierGeneratorHelper$2
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:146)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:150)
at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:63)
at java.lang.reflect.Field.set(Field.java:657)
at org.hibernate.property.DirectPropertyAccessor$DirectSetter.set(DirectPropertyAccessor.java:139)
... 13 more


Top
 Profile  
 
 Post subject: Re: Table having Primary Key and Identity Column
PostPosted: Tue Jul 05, 2011 7:09 am 
Beginner
Beginner

Joined: Fri Jul 20, 2007 10:38 am
Posts: 49
Isn't rowId a reserved word?
Anyway, according to the error message there's a set-method missing.
So I guess you should either add that or configure hibernate to use field injection.


Top
 Profile  
 
 Post subject: Re: Table having Primary Key and Identity Column
PostPosted: Tue Jul 05, 2011 7:25 am 
Newbie

Joined: Thu Jun 23, 2011 5:08 am
Posts: 9
Hi Lino,
ROWID is not a reserved word in AS400. I am able to create the table.
The getters and setters are available in the entity and i have removed it for the sake of reducing some spaces.


Top
 Profile  
 
 Post subject: Re: Table having Primary Key and Identity Column
PostPosted: Tue Jul 05, 2011 11:53 am 
Beginner
Beginner

Joined: Fri Jul 20, 2007 10:38 am
Posts: 49
rajkumarsrikanth wrote:
The getters and setters are available in the entity and i have removed it for the sake of reducing some spaces.

Sorry, I don't get this. You mean the setRowId(Long rowId) was there, but you removed it?


Top
 Profile  
 
 Post subject: Re: Table having Primary Key and Identity Column
PostPosted: Tue Jul 05, 2011 1:44 pm 
Newbie

Joined: Thu Jun 23, 2011 5:08 am
Posts: 9
Lino Cibran wrote:
rajkumarsrikanth wrote:
The getters and setters are available in the entity and i have removed it for the sake of reducing some spaces.

Sorry, I don't get this. You mean the setRowId(Long rowId) was there, but you removed it?

Lino, Please assume that all the getters and setters are available for all the properties in the code. I have removed them in this post for the sake of simplicity.


Top
 Profile  
 
 Post subject: Re: Table having Primary Key and Identity Column
PostPosted: Wed Jul 06, 2011 4:31 am 
Beginner
Beginner

Joined: Fri Jul 20, 2007 10:38 am
Posts: 49
Quote:
Can not set java.lang.Long field TableState.rowId to org.hibernate.id.IdentifierGeneratorHelper$2

I'm not sure here either, assuming you have the proper set-method. It's almost as if the error message is saying that the set method is taking the wrong argument. So your rowId is of type java.lang.Long, the set and get methods deal with java.lang.Long, but there's something else being put in there. Any chance that hibernate thinks it's something else, like a String?


Top
 Profile  
 
 Post subject: Re: Table having Primary Key and Identity Column
PostPosted: Wed Jul 06, 2011 4:59 am 
Newbie

Joined: Wed May 04, 2011 2:24 am
Posts: 10
I've had a similair problem, there is an issue within Hibernate that occurs when using an auto-generated PK part in a composite PK.

Check out these links:

http://opensource.atlassian.com/project ... e/HHH-6044
http://stackoverflow.com/questions/7344 ... rimary-key


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