diff --git a/envers/src/main/java/org/hibernate/envers/entities/EntityInstantiator.java b/envers/src/main/java/org/hibernate/envers/entities/EntityInstantiator.java index f4b938d8e2..3726f95a68 100644 --- a/envers/src/main/java/org/hibernate/envers/entities/EntityInstantiator.java +++ b/envers/src/main/java/org/hibernate/envers/entities/EntityInstantiator.java @@ -32,6 +32,7 @@ import org.hibernate.envers.entities.mapper.id.IdMapper; import org.hibernate.envers.exception.AuditException; import org.hibernate.envers.reader.AuditReaderImplementor; import org.hibernate.envers.tools.reflection.ReflectionTools; +import org.hibernate.util.ReflectHelper; /** * @author Adam Warski (adam at warski dot org) @@ -80,7 +81,7 @@ public class EntityInstantiator { Object ret; try { Class cls = ReflectionTools.loadClass(entityName); - ret = cls.newInstance(); + ret = ReflectHelper.getDefaultConstructor(cls).newInstance(); } catch (Exception e) { throw new AuditException(e); } diff --git a/envers/src/test/java/org/hibernate/envers/test/integration/accesstype/Country.java b/envers/src/test/java/org/hibernate/envers/test/integration/accesstype/Country.java new file mode 100644 index 0000000000..5e79495931 --- /dev/null +++ b/envers/src/test/java/org/hibernate/envers/test/integration/accesstype/Country.java @@ -0,0 +1,72 @@ +package org.hibernate.envers.test.integration.accesstype; + +import org.hibernate.envers.Audited; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; + +@Entity +@Audited +public class Country { + @Id + @Column(length = 4) + private Integer code; + + @Column(length = 40) + private String name; + + /** + * Default constructor for persistence provider. + */ + @SuppressWarnings({"UnusedDeclaration"}) + private Country() { } + + private Country(Integer code, String naam) { + this.code = code; + this.name = naam; + } + + public Integer getCode() { + return code; + } + + public String getName() { + return name; + } + + public static Country of(Integer code, String name) { + return new Country(code, name); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((code == null) ? 0 : code.hashCode()); + result = prime * result + ((name == null) ? 0 : name.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Country other = (Country) obj; + if (code == null) { + if (other.code != null) + return false; + } else if (!code.equals(other.code)) + return false; + if (name == null) { + if (other.name != null) + return false; + } else if (!name.equals(other.name)) + return false; + return true; + } +} diff --git a/envers/src/test/java/org/hibernate/envers/test/integration/accesstype/ImmutableClassAccessType.java b/envers/src/test/java/org/hibernate/envers/test/integration/accesstype/ImmutableClassAccessType.java new file mode 100644 index 0000000000..01c6796359 --- /dev/null +++ b/envers/src/test/java/org/hibernate/envers/test/integration/accesstype/ImmutableClassAccessType.java @@ -0,0 +1,50 @@ +package org.hibernate.envers.test.integration.accesstype; + +import java.util.Arrays; + +import javax.persistence.EntityManager; + +import org.hibernate.ejb.Ejb3Configuration; +import org.hibernate.envers.test.AbstractEntityTest; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; +import static org.testng.Assert.assertEquals; + +public class ImmutableClassAccessType extends AbstractEntityTest { + private Country country; + + public void configure(Ejb3Configuration cfg) { + cfg.addAnnotatedClass(Country.class); + } + + @BeforeClass(dependsOnMethods = "init") + public void initData() { + EntityManager em = getEntityManager(); + + // Revision 1 + em.getTransaction().begin(); + country = Country.of(123, "Germany"); + em.persist(country); + em.getTransaction().commit(); + + } + + @Test + public void testRevisionsCounts() { + assert Arrays.asList(1) + .equals( + getAuditReader().getRevisions(Country.class, + country.getCode())); + } + + @Test + public void testHistoryOfId1() { + Country country1 = getEntityManager().find(Country.class, + country.getCode()); + assertEquals(country1, country); + + Country history = getAuditReader().find(Country.class, country1.getCode(), 1); + assertEquals(country, history); + } + +} \ No newline at end of file