3 Replies Latest reply on Jan 29, 2011 3:44 PM by Adam Warski

    Problem with @ManyToOne relation using @MappedSuperclass

    Ernst-Udo Wallenborn Newbie



      i'm using Hibernate Envers 3.5.0-Final in a project. I have two entities, Country and City, inheriting from a common superclass, as in




      public abstract class DBEntity {


        @GeneratedValue(strategy = GenerationType.AUTO)

        private Long         id;



        private Integer      version;



      @Audited @Entity

      public class Country extends DBEntity {

        @Column(name = "COUNTRY_NAME")

        private String countryName;



      @Audited @Entity

      public class City extends DBEntity {

        @Column(name = "CITY")

        private String  cityName;


        @ManyToOne(fetch = FetchType.EAGER)

        @JoinColumn(name = "COUNTRY_ID")

        private Country country;




      I would like to find a particular revision of City, including the corresponding Country. After populating the database using Java:



      Country co = new Country();


      City ci = new City();







      and making sure the database contains what i'd expect, the following unit test fails:



          query = reader.createQuery().forRevisionsOfEntity(City.class, false, true)

            .add(AuditEntity.revisionNumber().between(minRev, maxRev))



          revs = query.getResultList();

          Object[] revision = revs.iterator().next();

          City ci = (City) revision[0];

          Country co = ci.getCountry();

          assertNotNull(co)   // OK

          assertEquals("Germany", co.getCountryName()); // OK

          assertNotNull(co.getId()); // FAILS

          assertNotNull(co.getVersion()); // FAILS



      The problem seems to be the @MappedSuperclass inheritance. If i remove the superclass and let City and Country define their own @Id and @Version, the unit test succeeds. But since the database has more entities than just City and Country, i'd really like to keep the superclass.


      What can i do? is there a Envers Annotation i'm missing? Or do i really have to give up the @MappedSuperclass?