我有一个Student
需要保存在数据库中的对象。id :studentId
的定义类似于 HBM :-
<id name="studentId" type="long">
<column name="ST_ID" />
<generator class="native" />
</id>
现在,为了生成 ID,我编写了我通常实现的代码,与 hibernate Source 中的代码相同,如下所示:-
// 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);
现在,当我到达代码行时persister.setIdentifier(entity, id, EntityMode.POJO);
,出现以下异常:-
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)
请帮助,因为我无法理解错误,因为我从休眠中选择了相同的代码。如果它在那里正常工作,那么这段代码也应该在这里工作。
谢谢