I am migrating application from Hibernate 4 to Hibernate 5. We have used:
import org.hibernate.property.PropertyAccessor;
import org.hibernate.property.Setter;
and then in code we were calling:
propertyAccessor.getSetter(target, fieldName).set(currentTarget, value, factory);
propertyAccessor.getGetter(target, fieldName).get(currentTarget);
I found Are PropertyAccessor and PropertyAccessorFactory removed or deprecated? and it says that two clases should be replaced by:
import org.hibernate.property.access.spi.PropertyAccessStrategy;
import org.hibernate.property.access.spi.Setter;
That's fine but PropertyAccessStrategy
doesn't have any methods which are similar to .getSetter()
and .getGetter()
from old packages.
How it should be replaced?
The getGetter()
and getSetter()
methods are part of org.hibernate.property.access.spi.PropertyAccess
interface in Hibernate 5.
For Example: Assume that you have an Entity named Person
given below:
@Table
@Entity
public class Person {
@Id
private int id;
private String fullName;
// constructor and getters and setters already defined....
}
Now, let's say if you want to access the fullName
property from the entity Person
and the getter/setter associated with that property.
Example on how to do it.
import org.hibernate.property.access.internal.PropertyAccessStrategyFieldImpl;
import org.hibernate.property.access.spi.Getter;
import org.hibernate.property.access.spi.Setter;
import org.hibernate.property.access.spi.PropertyAccess;
import org.hibernate.property.access.spi.PropertyAccessStrategy;
.......
.........
PropertyAccessStrategyFieldImpl propertyAccessStrategy = PropertyAccessStrategyFieldImpl.INSTANCE;
PropertyAccess fullNamePropertyAccess = propertyAccessStrategy.buildPropertyAccess(Person.class, "fullName");
Getter fullNameGetter = fullNamePropertyAccess.getGetter();
Setter fullNameSetter = fullNamePropertyAccess.getSetter();