我有一個Student
對象,我需要將它保存在數據庫中。的ID:studentId
在HBM定義如下: -在休眠狀態下設置生成的ID時出現錯誤
<id name="studentId" type="long">
<column name="ST_ID" />
<generator class="native" />
</id>
現在,用於生成ID,我已編寫的代碼,這是我一般實現,同樣,因爲它是存在於休眠源,如下: -
// 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)
請幫助,因爲我無法理解的錯誤,因爲我甲肝e從hibernate中選擇了相同的代碼。如果它能正常工作,那麼這個代碼也應該在這裏工作。
感謝
'學生類有方法'setStudent(Long id)'.. –
它應該有setStudentId {Long id)和一個名爲studentId的字段。你應該遵循java bean的約定。 –
正如我之前提到的,學生類包含屬性'studentId'和'setStudentId(Long id)'方法' –