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

    Problem with @ManyToOne relation using @MappedSuperclass

    Ernst-Udo Wallenborn Newbie

      Hi,

       

      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

       

      {code}

      @MappedSuperclass

      public abstract class DBEntity {

        @Id

        @GeneratedValue(strategy = GenerationType.AUTO)

        private Long         id;

       

        @Version

        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;

      }

      {code}

       

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

       

      {code}

      Country co = new Country();

      co.setCountryName("Germany");

      City ci = new City();

      ci.setCityName("Berlin");

      ci.setCountry(co);

      em.persist(co);

      em.persist(ci);

      {code}

       

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

       

      {code}

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

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

            .addOrder(AuditEntity.revisionNumber().desc());

       

          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

      {code}

       

      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?