0

I am upgrading a Spring 4.3.2 project to Spring 5.1.5. One of my test cases started failing with the error.

ClassNotFoundException: org.hibernate.property.DirectPropertyAccessor

Which makes sense since it has been deprecated in Hibernate 5.x which is the lowest compatible Hibernate version for Spring 5.x. I am consuming it in my hbm.xml as below:

<class name="Notification"
       table="T_NOTIFICATION"
       lazy="false">

    <id name="id"
        type="integer"
        column="ID"
        unsaved-value="null"
        access="property">
        <generator class="identity"/>
    </id>

    <!-- A versioned entity. -->
    <version name="version"
             column="VERSION"
             access="org.hibernate.property.DirectPropertyAccessor"/>

What should I replace the access field with, to maintain the same behaviour?

Vivek Shankar
  • 770
  • 1
  • 15
  • 37
  • What has a deprecation in hibernate to do with Spring? That is hibernate related NOT spring related. Also why not simply use `access="field"` which indirectly does the same, without being bound to a specific Hibernate version. – M. Deinum May 27 '19 at 08:46
  • @M.Deinum My apologies, I have edited the question to better reflect what I was trying to convey. I'm not sure how `access="field"` works. I'll give it a go. – Vivek Shankar May 27 '19 at 08:51
  • 1
    `access="field"` tells hibernate you want field access instead of using getter/setters which is also what `DirectPropertyAccessor`. Eventually it will lead to the same configuration, but with one added advantage it is portable to newer hibernate versions. – M. Deinum May 27 '19 at 08:53

2 Answers2

1

Using access=<class-name> you should only be using that if you have your own custom class you want to use. If you want to use direct field access instead of properties use access="field" instead of what you have now. See also the Hibernate Reference Guide.

Internally your current version will use the DirectPropertyAccessor when upgrading Hibernate it will automatically adapt to the newly introduced other class to use. Now that burden lies with hibernate instead of you having to know the internal API of hibernate.

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
  • Thanks, that exactly looks like what I need. I just tried it. However, I still see an error, `javax.persistence.TransactionRequiredException: no transaction is in progress at com.xxx.server.accessFabric.dao.impl.AccessPointDAOImplTest.setUp(AccessPointDAOImplTest.java:54)` Since I have made no other change than to replace `DirectPropertyAccessor` with `field`. I suspect this is a side affect? – Vivek Shankar May 27 '19 at 09:06
  • You are trying to save something without a transaction, that should have failed before as well. There have been changes in Spring 5 with the hibernate support in that regard. It is now less forgiving in case of a wrong configuration. – M. Deinum May 27 '19 at 09:06
  • I just modified my comment above. In short changes regarding Spring 5 and hibernate integration have been made, so this could lead to test failures if you ugprade things. You would probably see the same if you keep Spring 4.3.x and only upgrade to Hibernate 5 (looks like you are upgrading 2 things at the same time). Which is why I always advice to take small steps instead of doing everything at once. – M. Deinum May 27 '19 at 09:08
  • Sure, can you point me to the docs where these stricter config checks are described? – Vivek Shankar May 27 '19 at 09:14
  • No, they are somewhere in the changelogs for Spring 5/Hibernate 4+5 integration. – M. Deinum May 27 '19 at 09:14
  • I'll go dig. Thanks. – Vivek Shankar May 27 '19 at 09:15
0

The issue is not with Spring version but with Hibernate version.

org.hibernate.property.DirectPropertyAccessor is deprecated in 5.x version of hibernate.

DirectPropertyAccessor should be replaced with either org.hibernate.property.access.internal.PropertyAccessFieldImpl or org.hibernate.property.access.internal.PropertyAccessMixedImplin higher hibernate versions.

Abhijeet
  • 4,069
  • 1
  • 22
  • 38
  • With `org.hibernate.property.access.internal.PropertyAccessMixedImpl` I see the error: `Caused by: java.lang.NoSuchMethodException: org.hibernate.property.access.internal.PropertyAccessMixedImpl.()` With `org.hibernate.property.access.internal.PropertyAccessFieldImpl` I see: `Caused by: java.lang.NoSuchMethodException: org.hibernate.property.access.internal.PropertyAccessFieldImpl.()` I have already checked out this [solution](https://discourse.hibernate.org/t/directpropertyaccessor-alternatives-in-5-2-12/39), unfortunately it did not work for me. – Vivek Shankar May 27 '19 at 07:08
  • I'm using many can you be more specific? For Hibernate I use `hibernate-core:5.4.1.Final` – Vivek Shankar May 27 '19 at 07:27
  • You need to create new class which implements PropertyAccessFieldImpl and specify that class in xml file – Abhijeet May 27 '19 at 07:28
  • Which `implements` PropertyAccessFieldImpl? But it isn't an abstract class or an interface. It's already implementing PropertyAccess, can you point me to an example which corroborates this? – Vivek Shankar May 27 '19 at 07:31