hibernate - DirectPropertyAccessor removed in hibernate 5.1 and above, alternative?
问题描述
I am upgrading my hibernate version in project from 3.6.10 to 5.4.1
I was using BasicPropertyAccessor and DirectPropertyAccessor for getter and setter methods as below in 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);
}
however in 5.1 and above both BasicPropertyAccessor and DirectPropertyAccessor have been removed
I replaced the code with below-:
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 );
}
}
and the access class-:
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;
}
}
however the settermethodimpl doesnt work same as DirectPropertyAccessor
My hibernate mapping file( 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);
}
}
when the company object is loaded , it doesnt link the address proxy object directly(as DirectPropertyAccessor does) instead calls the setaddress method, which in turns throws exception while trying to throw 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)
The same code works wit DirectPropertyAccessor, so its some issue with the way I am using PropertyAccessStratergy, can someone help me on this?
解决方案
推荐阅读
- python - 打印无限参数的索引和值的脚本
- tailwind-css - 有没有办法禁用 Tailwind 的响应?
- outlook - 用于将邮件作为附件转发到特定地址的 Outlook 加载项
- reactjs - 获取 e.timeScale(...).unsubscribeVisibleTimeRangeChange 不是函数
- flutter - 错误:不支持的操作:Platform._localeName
- android - Xamarin 形式;条件编译符号在 MSBUILD 命令行中不起作用
- python - 如何在一个请求中放置或删除多个对象?Django 休息
- php - ASP.NET Web API 与 PHP 的区别
- sql - 如何为postgresql中的每个用户返回具有最大值的类别?
- python - M1 Mac 的 OpenCV 问题:分配前引用了局部变量“x”