0

我试图将我的应用程序从 Hibernate 3.2 升级到 Hibernate 3.5.6,但我得到了以下以前没有的异常:

org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of Employee.phoneNumbers    
at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:198) ~[hibernate-core-3.5.6-Final.jar:3.5.6-Final]
at org.hibernate.tuple.entity.AbstractEntityTuplizer.getPropertyValue(AbstractEntityTuplizer.java:506) ~[hibernate-core-3.5.6-Final.jar:3.5.6-Final]
at org.hibernate.tuple.entity.AbstractEntityTuplizer.getPropertyValue(AbstractEntityTuplizer.java:524) ~[hibernate-core-3.5.6-Final.jar:3.5.6-Final]
at org.hibernate.persister.entity.AbstractEntityPersister.getPropertyValue(AbstractEntityPersister.java:3844) ~[hibernate-core-3.5.6-Final.jar:3.5.6-Final]
at org.hibernate.engine.StatefulPersistenceContext.isFoundInParent(StatefulPersistenceContext.java:1236) ~[hibernate-core-3.5.6-Final.jar:3.5.6-Final]
at org.hibernate.engine.StatefulPersistenceContext.getOwnerId(StatefulPersistenceContext.java:1146) ~[hibernate-core-3.5.6-Final.jar:3.5.6-Final]
at org.hibernate.property.BackrefPropertyAccessor$BackrefGetter.getForInsert(BackrefPropertyAccessor.java:139) ~[hibernate-core-3.5.6-Final.jar:3.5.6-Final]
at org.hibernate.tuple.entity.AbstractEntityTuplizer.getPropertyValuesToInsert(AbstractEntityTuplizer.java:500) ~[hibernate-core-3.5.6-Final.jar:3.5.6-Final]
at org.hibernate.tuple.entity.PojoEntityTuplizer.getPropertyValuesToInsert(PojoEntityTuplizer.java:255) ~[hibernate-core-3.5.6-Final.jar:3.5.6-Final]
at org.hibernate.persister.entity.AbstractEntityPersister.getPropertyValuesToInsert(AbstractEntityPersister.java:3952) ~[hibernate-core-3.5.6-Final.jar:3.5.6-Final]
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:290) ~[hibernate-core-3.5.6-Final.jar:3.5.6-Final]
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:204) ~[hibernate-core-3.5.6-Final.jar:3.5.6-Final]
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:144) ~[hibernate-core-3.5.6-Final.jar:3.5.6-Final]
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210) ~[hibernate-core-3.5.6-Final.jar:3.5.6-Final]
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195) ~[hibernate-core-3.5.6-Final.jar:3.5.6-Final]
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:117) ~[hibernate-core-3.5.6-Final.jar:3.5.6-Final]
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93) ~[hibernate-core-3.5.6-Final.jar:3.5.6-Final]
at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:677) ~[hibernate-core-3.5.6-Final.jar:3.5.6-Final]
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:669) ~[hibernate-core-3.5.6-Final.jar:3.5.6-Final]
at org.hibernate.engine.CascadingAction$5.cascade(CascadingAction.java:252) ~[hibernate-core-3.5.6-Final.jar:3.5.6-Final]
at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:392) ~[hibernate-core-3.5.6-Final.jar:3.5.6-Final]
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:335) ~[hibernate-core-3.5.6-Final.jar:3.5.6-Final]
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:204) ~[hibernate-core-3.5.6-Final.jar:3.5.6-Final]
at org.hibernate.engine.Cascade.cascade(Cascade.java:161) ~[hibernate-core-3.5.6-Final.jar:3.5.6-Final]
at org.hibernate.event.def.AbstractSaveEventListener.cascadeBeforeSave(AbstractSaveEventListener.java:451) ~[hibernate-core-3.5.6-Final.jar:3.5.6-Final]
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:283) ~[hibernate-core-3.5.6-Final.jar:3.5.6-Final]
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:204) ~[hibernate-core-3.5.6-Final.jar:3.5.6-Final]
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:144) ~[hibernate-core-3.5.6-Final.jar:3.5.6-Final]
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210) ~[hibernate-core-3.5.6-Final.jar:3.5.6-Final]
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56) ~[hibernate-core-3.5.6-Final.jar:3.5.6-Final]
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195) ~[hibernate-core-3.5.6-Final.jar:3.5.6-Final]
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50) ~[hibernate-core-3.5.6-Final.jar:3.5.6-Final]
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93) ~[hibernate-core-3.5.6-Final.jar:3.5.6-Final]
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:705) ~[hibernate-core-3.5.6-Final.jar:3.5.6-Final]
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:693) ~[hibernate-core-3.5.6-Final.jar:3.5.6-Final]
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:689) ~[hibernate-core-3.5.6-Final.jar:3.5.6-Final]

...

Caused by: java.lang.IllegalArgumentException: java.lang.ClassCastException@424ada59
    at sun.reflect.GeneratedMethodAccessor376.invoke(Unknown Source) ~[na:na]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) ~[na:1.6.0_26]
    at java.lang.reflect.Method.invoke(Method.java:597) ~[na:1.6.0_26]
    at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:172) ~[hibernate-core-3.5.6-Final.jar:3.5.6-Final]

我在这个页面堆栈溢出上发现它可能是缓存的问题。我仍在使用ehcache,没有改变任何东西。所以我尝试禁用它,但我仍然遇到问题。

此问题发生在此方法中

@Transactional
public void createLocalEntriesForEmployeeType(List<RemotePhone> dataToBeAddedLocally, Integer employeeId) {
{
    for (RemotePhone remote : dataToBeAddedLocally) {
        ExternalDetails externalDetails = new ExternalDetails();
        externalDetails.setRemoteId(remote.getRemoteId().toString());
        externalDetails.setLastUpdated(remote.getUpdateDate());
        EmployeePhoneNumber localPhone = createLocalPhone(remote);

        employeeService.savePhoneNumber(localPhone, employeeId);
              
        ExternalPhoneDetails externalPhoneDetails = new externalPhoneDetails();
        externalPhoneDetails.setPayrollDataType(localPhone);
        externalPhoneDetails.setExternalDetails(externalDetails);
        externalDao.save(externalPhoneDetails);
    }
}

方法 savePhoneNumber 是:

@Transactional
public void savePhoneNumber(EmployeePhoneNumber phoneNumber, Integer employeeId) {
    validationService.assertValid(phoneNumber, "employeePhoneNumber");
    Employee employee = getEmployee(employeeId);
    employee.getPhoneNumbers().add(phoneNumber);
}

映射是:

<class
    name="Employee"
    table="EMPLOYEE"
    lazy="true"
>
    <id
        name="id"
        column="EMPLOYEEID"
        type="java.lang.Integer"
        unsaved-value="undefined"
    >
        <generator class="sequence">
            <param name="sequence">EMPLOYEEID_SEQ</param>
        </generator>
    </id>

    <property name="firstName" column="FIRSTNAME" type="java.lang.String"/>
    <bag
        name="phoneNumbers"
        table="PHONE_NUMBERS"
        cascade="all, delete-orphan"
        lazy="true"
        order-by="id"
    >
        <key column="EMPLOYEE" not-null="true"/>
        <one-to-many class="EmployeePhoneNumber"/>
    </bag>
</class>

<class name="EmployeePhoneNumber"
       table="PHONE_NUMBERS"
       lazy="true" >
            
    <id name="id"
        column="ID"
        type="java.lang.Integer"
        unsaved-value="undefined">
          <generator class="sequence">
            <param name="sequence">employee_phone_numbers_id_seq</param>
          </generator>
    </id>
              
    <property name="number" column="NUMBER" type="java.lang.String"/>
    <many-to-one
        name="employee" 
        class="Employee" 
        column="EMPLOYEE"
        insert="false"
        update="false"
    />
</class>

<class
    name="ExternalDetails"
    lazy="true"
    table="EXTERNAL_DETAILS"
>
    <id
        name="id"
        column="ID"
        type="java.lang.Integer"
        unsaved-value="undefined"
    >
          <generator class="sequence">
              <param name="sequence">EXTERNAL_DETAILS_ID_SEQ</param>
          </generator>
    </id>

    <property name="remoteId"       column="REMOTE_ID"      type="java.lang.String"/>
</class>

<class
    name="ExternalPhoneDetails"
    table="EXTERNAL_PHONE_NUMBER_DETAILS"
    lazy="true"
>
    <id
        name="id"
        column="ID"
        type="java.lang.Integer"
        unsaved-value="undefined"
    >
        <generator class="sequence">
            <param name="sequence">EXTERNAL_PHONE_NUMBER_DETAILS_ID_SEQ</param>
        </generator>
    </id>
    
    <many-to-one
        name="externalDetails"
        column="EXTERNAL_DETAILS"
        class="ExternalDetails"
        cascade="save-update, delete"
    />

    <many-to-one
        name="payrollDataType"
        column="PHONE_NUMBER"
        class="EmployeePhoneNumber"
        cascade="save-update, delete"
    />
</class>

我找到了一种通过在调用 employeeService.savePhoneNumber() 后刷新 hibernateSession 来解决此问题的方法,但我不喜欢此解决方案,并且它曾经在 Hibernate 3.2 中正常工作。这是hibernate 3.5中的错误还是我想升级时需要更改的东西?

谢谢

4

0 回答 0