在休眠5.1及更高版本中删除了DirectPropertyAccessor,可以吗?

时间:2019-03-23 19:19:32

标签: hibernate hibernate-mapping

我将项目中的休眠版本从3.6.10升级到5.4.1

我在3.6.10-中使用BasicPropertyAccessor和DirectPropertyAccessor进行getter和setter方法:

public class accessorClass extends DirectPropertyAccessor {
    private DirectPropertyAccessor mDirectPropertyAccessor;
    private BasicPropertyAccessor mBasicPropertyAccessor;

    public PSPMixedAccessor() {
        mDirectPropertyAccessor = new DirectPropertyAccessor();
        mBasicPropertyAccessor = new BasicPropertyAccessor();
    }

    @Override
    public Setter getSetter(Class theClass, String propertyName) throws PropertyNotFoundException {
        return mDirectPropertyAccessor.getSetter(theClass, "m" + propertyName);
    }

    @Override
    public Getter getGetter(Class theClass, String propertyName) throws PropertyNotFoundException {
        return mBasicPropertyAccessor.getGetter(theClass, propertyName);
    }

但是在5.1及更高版本中,BasicPropertyAccessor和DirectPropertyAccessor已被删除

我用下面的代码替换了代码:

public class accessorClass  implements PropertyAccessStrategy {

    private final PropertyAccessStrategyBasicImpl strategy;

    public PSPMixedAccessor(){
        PropertyAccessStrategyBasicImpl accessor = PropertyAccessStrategyBasicImpl.INSTANCE;
        this.strategy = accessor;

    }


    @Override
    public PropertyAccess buildPropertyAccess(Class containerJavaType, final String propertyName) {
        return new PSPMIxedaccess( strategy, containerJavaType, "m" + propertyName );
    }
}

和访问类别-:

public class MIxedaccess  implements PropertyAccess {
            private final PropertyAccessStrategy strategy;

            private final Getter getter;
            private final Setter setter;

            public PSPMIxedaccess(
                    PropertyAccessStrategy strategy,
                    Class containerJavaType,
                    String propertyName) {
                this.strategy = strategy;

                //remove m for getter
                String getpropertyName=propertyName.substring(1);
                final Method getterMethod = ReflectHelper.findGetterMethod( containerJavaType, getpropertyName );
                this.getter = new GetterMethodImpl( containerJavaType, propertyName, getterMethod );

                final Method setterMethod = ReflectHelper.findSetterMethod( containerJavaType, getpropertyName, getterMethod.getReturnType() );
                this.setter = new SetterMethodImpl( containerJavaType, propertyName, setterMethod );
            }

            @Override
            public PropertyAccessStrategy getPropertyAccessStrategy() {
                return strategy;
            }
            @Override
            public Getter getGetter() {
                return getter;
            }
            @Override
            public Setter getSetter() {
                return setter;
            }
}

但是settermethodimpl与DirectPropertyAccessor的作用不同

我的休眠映射文件(file.hbm.xml-:)

<hibernate-mapping package="package-name" default-cascade="merge" auto-import="false" default-access="accessorClass">
    <class name="Company" table="COMPANY" optimistic-lock="dirty" dynamic-update="true">

        <many-to-one name="Address" class="package.Address" column="ADDRESS_FK" unique="true"  />

Company.java

public void setAddress(Address pAddress) {
    if (!ObjectUtils.equals(getAddress(), pAddress)) {
        onUpdate();
    }
    super.setAddress(pAddress);
}

public void onUpdate() {
    if (getInfo() != null) {
        getInfo().onUpdate();
    }
}

public Info getInfo()
{
    if (mInfoSet.size() == 0) {
       return null;
    }
    else {
       return mInfoSet.get(0);
    }
}

在加载公司对象时,它不直接链接地址代理对象(如DirectPropertyAccessor那样),而是调用setaddress方法,该方法又在尝试引发lazyInitalization时引发异常

org.hibernate.LazyInitializationException: failed to lazily initialize a collection, no session or session was closed
    at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:602)
    at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:594)
    at org.hibernate.collection.internal.AbstractPersistentCollection.access$200(AbstractPersistentCollection.java:56)
    at org.hibernate.collection.internal.AbstractPersistentCollection$1.doWork(AbstractPersistentCollection.java:181)
    at org.hibernate.collection.internal.AbstractPersistentCollection$1.doWork(AbstractPersistentCollection.java:162)
    at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:263)
    at org.hibernate.collection.internal.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:161)
    at org.hibernate.collection.internal.PersistentSet.size(PersistentSet.java:168)
    at Company.getQuickbooksInfo(Company.java:)
    at package.Company.onUpdate(Company.java:)
    at package.Company.setAddress(Company.java:)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.hibernate.property.access.spi.SetterMethodImpl.set(SetterMethodImpl.java:45)
    at org.hibernate.tuple.entity.AbstractEntityTuplizer.setPropertyValues(AbstractEntityTuplizer.java:649)
    at org.hibernate.tuple.entity.PojoEntityTuplizer.setPropertyValues(PojoEntityTuplizer.java:205)
    at org.hibernate.persister.entity.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:4905)
    at org.hibernate.engine.internal.TwoPhaseLoad.doInitializeEntity(TwoPhaseLoad.java:190)
    at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:129)
    at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:1151)
    at org.hibernate.loader.Loader.processResultSet(Loader.java:1010)
    at org.hibernate.loader.Loader.doQuery(Loader.java:948)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:340)
    at org.hibernate.loader.Loader.doList(Loader.java:2695)
    at org.hibernate.loader.Loader.doList(Loader.java:2678)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2512)
    at org.hibernate.loader.Loader.list(Loader.java:2507)
    at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:109)

同一代码可以在DirectPropertyAccessor中使用,所以它与我使用PropertyAccessStratergy的方式有关,有人可以帮我吗?

0 个答案:

没有答案