Я обновляю свою версию hibernate в проекте с 3.6.10 до 5.4.1
Я использовал BasicPropertyAccessor и DirectPropertyAccessor для методов получения и установки, как показано ниже в 3.6.10 -:
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, кто-то может мне помочь в этом?