Reputation: 16646
I have a Student
object which i need to save in database. The id : studentId
is defined like in HBM :-
<id name="studentId" type="long">
<column name="ST_ID" />
<generator class="native" />
</id>
Now, for generating the ID, i have written the code, which i have generally implemented, same as it was present in hibernate Source, as below :-
// fetching the entity persister for the entity
EntityPersister persister =
((SessionImpl)session.).getEntityPersister(entity.getClass().getName(), entity);
// get the model
PersistentClass model = configuration.getClassMapping(persister.getEntityName());
// cache concurrency
CacheConcurrencyStrategy strategy = persister.getCache();
Class persiterClass = model.getEntityPersisterClass();
SessionFactoryImpl sessionFactoryImpl =
(SessionFactoryImpl) session.getSessionFactory();
if(persiterClass == null) {
persister = new SingleTableEntityPersister(model, strategy, sessionFactoryImpl)
}
this.id = persister.getIdentifierGenerator().generate((SessionImpl)session, entity);
persister.setIdentifier(entity, id, EntityMode.POJO);
Now, when i reach the code line persister.setIdentifier(entity, id, EntityMode.POJO);
, I get the following exception :-
IllegalArgumentException in class:
com.school.class.Student, setter method of property: studentId
org.hibernate.property.BasicPropertyAccessor$BasicSetter set
SEVERE: expected type: long, actual value: org.hibernate.id.IdentifierGeneratorFactory$2
org.hibernate.PropertyAccessException: IllegalArgumentException occurred while calling setter of com.school.class.Student.studentId
at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:104)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.setIdentifier(AbstractEntityTuplizer.java:211)
at org.hibernate.persister.entity.AbstractEntityPersister.setIdentifier(AbstractEntityPersister.java:3601)
at com.school.class.Student.<init>(Student.java:140)
Please Help, as i am unable to understand the error, as I have picked the code same from hibernate. If it works, properly over there, then this code should also work here.
Thanks
Upvotes: 0
Views: 3965
Reputation: 8722
That error is caused because the Student
class does not have the method setStudentId{Long id)
Upvotes: 0
Reputation: 16646
In the HBM File, i mentioned the generator as <generator class="native" />
, with which it was not working, then i tried with <generator class="increment"/>
, I don't know the exact reason, but it worked properly with that.
I feel the proper reason can be that the identifier generator was POST_INSERT_INDICATOR
, which i feel means that the id will be generated after the object is being inserted into the database, whereas I was trying to generate the same before insetion, thats the reason it was failing.
I even tried with other id generators also, Refer This , i tried uuid
, assigned
, increment
, sequence
, and hilo
, they have worked properly, but native
& identity
, failed with the same problem.
Any updates to this answer are invited, as even i want to know the exact reason for this thing and will like to know, the exact reason.
Upvotes: 0
Reputation: 242686
Some generators can't generate the identifier before the actual insert, therefore they return a special marker object from the generate()
method. This object indicates that Hibernate should obtain identifier in different way, using PostInsertIdentifierGenerator.getInsertGeneratedIdentifierDelegate()
method.
Upvotes: 1