Hello team,
I was using PropertyAccessor interface to create unknown column in database while runtime.But this interfave is removed in hibernate-5. I an i am not sure how to implement the newly provided interface PropertyAccessStrategy to fulfill my need.
I was using PropertyAccessor and reflection to generate getters and setter at run time.
Please suggest how i could fullfill my requirement.
// hbm file unknown field entry:
//Class implementing Dynamic behaviour.
public class MyDynamicClass implements PropertyAccessStrategy{
It’s almost the same. A PropertyAccessStrategy builds the PropertyAccess which then offers methods to retrieve getters/setters. Think a bit about it and you will find a way to adapt your implementation.
Thanks for your quick response.
But I am getting below error and i am not sure whether I ma implementing it correctly or not:
//Pojo Class.
public Employee{
private int id;
private String firstName,lastName;
getter/setter
}
//hbmfile.xml
<property name=“department” //this field is not declared in pojo and i need to create this runtime in db
type=“string”
column=“DEPARTMENT”
length=“100”
access=“org.example.service.MyDynamicClass”/>
//Implementation class.
public class MyDynamicClass implements PropertyAccessStrategy{
Caused by: org.hibernate.HibernateException: Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]
at org.hibernate.tuple.entity.EntityTuplizerFactory.constructTuplizer(EntityTuplizerFactory.java:91)
at org.hibernate.tuple.entity.EntityTuplizerFactory.constructDefaultTuplizer(EntityTuplizerFactory.java:116)
at org.hibernate.tuple.entity.EntityMetamodel.(EntityMetamodel.java:388)
at org.hibernate.persister.entity.AbstractEntityPersister.(AbstractEntityPersister.java:502)
at org.hibernate.persister.entity.SingleTableEntityPersister.(SingleTableEntityPersister.java:122)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:96)
… 6 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.hibernate.tuple.entity.EntityTuplizerFactory.constructTuplizer(EntityTuplizerFactory.java:88)
… 15 more
Caused by: org.hibernate.PropertyNotFoundException: Could not locate field name [department] on class [org.example.model.Employee]
at org.hibernate.internal.util.ReflectHelper.findField(ReflectHelper.java:348)
at org.hibernate.property.access.internal.PropertyAccessFieldImpl.(PropertyAccessFieldImpl.java:34)
at org.hibernate.property.access.internal.PropertyAccessStrategyFieldImpl.buildPropertyAccess(PropertyAccessStrategyFieldImpl.java:26)
at org.example.service.MyDynamicClass.buildPropertyAccess(MyDynamicClass.java:30)
at org.hibernate.mapping.Property.getGetter(Property.java:308)
at org.hibernate.tuple.entity.PojoEntityTuplizer.buildPropertyGetter(PojoEntityTuplizer.java:270)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.(AbstractEntityTuplizer.java:145)
at org.hibernate.tuple.entity.PojoEntityTuplizer.(PojoEntityTuplizer.java:63)
… 20 more
You are trying to lookup the field/getter/setter methods for department on the java class Employee through calling PropertyAccessStrategyFieldImpl.buildPropertyAccess which the exception tells you doesn’t exist. I don’t know what you are trying to achieve here or how you did this before, but these are very basic exceptions that you can easily analyze yourself.
I have a scenario where there is a pojo class say “Employee.java”.
This class contains few defined fields like"id,FirstName,LastName" of employee details.
But during runtime there can be chance of getting one more employee detail say “department”
Note:- 1. The requirement is that i don’t have to declare “department” in pojo class but when we
run the application hibernate automatically creates “department” column in table which
was achieved by declaring access=“anypackage.MyDynamicClass”(Ref: attached img)
4. This was working using "propertyAccessor" interface of hibernate-4 but same i am
trying to achieve using "PropertyAccessStrategy" in hibernate 5 is not working.
i.e. The Pojo class have two methods:
getProperty(String propName)
which returns the property value whose name is passed through propName.
setProperty(String propName, Object value)
which sets the value of property whose name is passed through propName
So I wanted to know how this requirement can be achieved.
I don’t understand your problem. You can do the same as you could do before. Just implement the interfaces PropertyAccessStrategy, PropertyAccess, Getter and Setter. I won’t do your work for you. If you need someone to do your work, you will have to pay a consultant.
@Vivek_Kumar Exactly I am facing the same issue currently during the hibernate 4 to 5 migration. I tried with both 'PropertyAccessStrategy,PropertyAccess` but nothing works. Did you find any solution for this?