Merge pull request #202 from lukasz-antoniak/HHH-3843
HHH-3843 - NonUniqueObjectException exception while auditing parent with @ManyToMany relation
This commit is contained in:
commit
4de4bad98a
|
@ -231,7 +231,7 @@ public abstract class BaseEnversCollectionEventListener extends BaseEnversEventL
|
|||
auditProcess.addWorkUnit(
|
||||
new CollectionChangeWorkUnit(
|
||||
event.getSession(),
|
||||
relatedEntityName,
|
||||
event.getSession().bestGuessEntityName(relatedObj),
|
||||
getAuditConfiguration(),
|
||||
relatedId,
|
||||
relatedObj
|
||||
|
|
|
@ -0,0 +1,77 @@
|
|||
package org.hibernate.envers.test.integration.inheritance.joined.relation;
|
||||
|
||||
import org.hibernate.ejb.Ejb3Configuration;
|
||||
import org.hibernate.envers.test.AbstractEntityTest;
|
||||
import org.hibernate.envers.test.Priority;
|
||||
import org.hibernate.envers.test.tools.TestTools;
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
|
||||
*/
|
||||
@TestForIssue(jiraKey = "HHH-3843")
|
||||
public class ParentReferencingChildTest extends AbstractEntityTest {
|
||||
Person expLukaszRev1 = null;
|
||||
Role expAdminRev1 = null;
|
||||
|
||||
public void configure(Ejb3Configuration cfg) {
|
||||
cfg.addAnnotatedClass(Person.class);
|
||||
cfg.addAnnotatedClass(Role.class);
|
||||
cfg.addAnnotatedClass(RightsSubject.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Priority(10)
|
||||
public void initData() {
|
||||
EntityManager em = getEntityManager();
|
||||
|
||||
// Revision 1
|
||||
em.getTransaction().begin();
|
||||
Person lukasz = new Person();
|
||||
lukasz.setName("Lukasz");
|
||||
lukasz.setGroup("IT");
|
||||
em.persist(lukasz);
|
||||
Role admin = new Role();
|
||||
admin.setName("Admin");
|
||||
admin.setGroup("Confidential");
|
||||
lukasz.getRoles().add(admin);
|
||||
admin.getMembers().add(lukasz);
|
||||
em.persist(admin);
|
||||
em.getTransaction().commit();
|
||||
|
||||
expLukaszRev1 = new Person(lukasz.getId(), "IT", "Lukasz");
|
||||
expAdminRev1 = new Role(admin.getId(), "Confidential", "Admin");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRevisionsCounts() {
|
||||
Assert.assertEquals(Arrays.asList(1), getAuditReader().getRevisions(Person.class, expLukaszRev1.getId()));
|
||||
Assert.assertEquals(Arrays.asList(1), getAuditReader().getRevisions(RightsSubject.class, expLukaszRev1.getId()));
|
||||
|
||||
Assert.assertEquals(Arrays.asList(1), getAuditReader().getRevisions(Role.class, expAdminRev1.getId()));
|
||||
Assert.assertEquals(Arrays.asList(1), getAuditReader().getRevisions(RightsSubject.class, expAdminRev1.getId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHistoryOfLukasz() {
|
||||
Person lukaszRev1 = getAuditReader().find(Person.class, expLukaszRev1.getId(), 1);
|
||||
RightsSubject rightsSubjectLukaszRev1 = getAuditReader().find(RightsSubject.class, expLukaszRev1.getId(), 1);
|
||||
|
||||
Assert.assertEquals(expLukaszRev1, lukaszRev1);
|
||||
Assert.assertEquals(TestTools.makeSet(expAdminRev1), lukaszRev1.getRoles());
|
||||
Assert.assertEquals(TestTools.makeSet(expAdminRev1), rightsSubjectLukaszRev1.getRoles());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHistoryOfAdmin() {
|
||||
Role adminRev1 = getAuditReader().find(Role.class, expAdminRev1.getId(), 1);
|
||||
|
||||
Assert.assertEquals(expAdminRev1, adminRev1);
|
||||
Assert.assertEquals(TestTools.makeSet(expLukaszRev1), adminRev1.getMembers());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
package org.hibernate.envers.test.integration.inheritance.joined.relation;
|
||||
|
||||
import org.hibernate.envers.Audited;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Table;
|
||||
|
||||
/**
|
||||
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
|
||||
*/
|
||||
@Entity
|
||||
@Audited
|
||||
public class Person extends RightsSubject {
|
||||
private String name;
|
||||
|
||||
public Person() {
|
||||
}
|
||||
|
||||
public Person(Long id, String group, String name) {
|
||||
super(id, group);
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof Person)) return false;
|
||||
if (!super.equals(o)) return false;
|
||||
|
||||
Person person = (Person) o;
|
||||
|
||||
if (name != null ? !name.equals(person.name) : person.name != null) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = super.hashCode();
|
||||
result = 31 * result + (name != null ? name.hashCode() : 0);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Person(" + super.toString() + ", name = " + name + ")";
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,82 @@
|
|||
package org.hibernate.envers.test.integration.inheritance.joined.relation;
|
||||
|
||||
import org.hibernate.envers.Audited;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
|
||||
*/
|
||||
@Entity
|
||||
@Inheritance(strategy = InheritanceType.JOINED)
|
||||
@Audited
|
||||
public class RightsSubject {
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
@Column(name = "APP_GROUP")
|
||||
private String group;
|
||||
|
||||
@ManyToMany(mappedBy="members")
|
||||
private Set<Role> roles = new HashSet<Role>();
|
||||
|
||||
public RightsSubject() {
|
||||
}
|
||||
|
||||
public RightsSubject(Long id, String group) {
|
||||
this.id = id;
|
||||
this.group = group;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof RightsSubject)) return false;
|
||||
|
||||
RightsSubject that = (RightsSubject) o;
|
||||
|
||||
if (group != null ? !group.equals(that.group) : that.group != null) return false;
|
||||
if (id != null ? !id.equals(that.id) : that.id != null) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = id != null ? id.hashCode() : 0;
|
||||
result = 31 * result + (group != null ? group.hashCode() : 0);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "RightsSubject(id = " + id + ", group = " + group + ")";
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Set<Role> getRoles() {
|
||||
return roles;
|
||||
}
|
||||
|
||||
public void setRoles(Set<Role> roles) {
|
||||
this.roles = roles;
|
||||
}
|
||||
|
||||
public String getGroup() {
|
||||
return group;
|
||||
}
|
||||
|
||||
public void setGroup(String group) {
|
||||
this.group = group;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,70 @@
|
|||
package org.hibernate.envers.test.integration.inheritance.joined.relation;
|
||||
|
||||
import org.hibernate.envers.Audited;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.ManyToMany;
|
||||
import javax.persistence.Table;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
|
||||
*/
|
||||
@Entity
|
||||
@Table(name = "APP_ROLE")
|
||||
@Audited
|
||||
public class Role extends RightsSubject {
|
||||
private String name;
|
||||
|
||||
@ManyToMany
|
||||
private Set<RightsSubject> members = new HashSet<RightsSubject>();
|
||||
|
||||
public Role() {
|
||||
}
|
||||
|
||||
public Role(Long id, String group, String name) {
|
||||
super(id, group);
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof Role)) return false;
|
||||
if (!super.equals(o)) return false;
|
||||
|
||||
Role role = (Role) o;
|
||||
|
||||
if (name != null ? !name.equals(role.name) : role.name != null) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = super.hashCode();
|
||||
result = 31 * result + (name != null ? name.hashCode() : 0);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Role(" + super.toString() + ", name = " + name + ")";
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Set<RightsSubject> getMembers() {
|
||||
return members;
|
||||
}
|
||||
|
||||
public void setMembers(Set<RightsSubject> members) {
|
||||
this.members = members;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue