Skip to content

Commit 3461187

Browse files
HHH-7073 - Fix and test
1 parent 84f3821 commit 3461187

File tree

4 files changed

+388
-1
lines changed

4 files changed

+388
-1
lines changed

hibernate-envers/src/main/java/org/hibernate/envers/entities/EntitiesConfigurations.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public Set<String> getToPropertyNames(String fromEntityName, String fromProperty
152152
for (RelationDescription relationDescription : getRelationDescriptions(toEntityName)) {
153153
String relToEntityName = relationDescription.getToEntityName();
154154
String mappedByPropertyName = relationDescription.getMappedByPropertyName();
155-
if (entityAndParentsNames.contains(relToEntityName) && mappedByPropertyName.equals(fromPropertyName)) {
155+
if (entityAndParentsNames.contains(relToEntityName) && mappedByPropertyName != null && mappedByPropertyName.equals(fromPropertyName)) {
156156
toPropertyNames.add(relationDescription.getFromPropertyName());
157157
}
158158
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package org.hibernate.envers.test.integration.multiplerelations;
2+
3+
import org.hibernate.envers.Audited;
4+
5+
import javax.persistence.*;
6+
import java.io.Serializable;
7+
import java.util.HashSet;
8+
import java.util.Set;
9+
10+
/**
11+
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
12+
*/
13+
@Entity
14+
@Audited
15+
public class Address implements Serializable {
16+
@Id
17+
@GeneratedValue
18+
private long id;
19+
20+
private String city;
21+
22+
@ManyToMany(cascade = {CascadeType.PERSIST})
23+
private Set<Person> tenants = new HashSet<Person>();
24+
25+
@ManyToOne
26+
@JoinColumn(nullable = false)
27+
Person landlord;
28+
29+
public Address() {
30+
}
31+
32+
public Address(String city) {
33+
this.city = city;
34+
}
35+
36+
public Address(String city, long id) {
37+
this.id = id;
38+
this.city = city;
39+
}
40+
41+
@Override
42+
public boolean equals(Object o) {
43+
if (this == o) return true;
44+
if (!(o instanceof Address)) return false;
45+
46+
Address address = (Address) o;
47+
48+
if (id != address.id) return false;
49+
if (city != null ? !city.equals(address.city) : address.city != null) return false;
50+
51+
return true;
52+
}
53+
54+
@Override
55+
public int hashCode() {
56+
int result = (int) (id ^ (id >>> 32));
57+
result = 31 * result + (city != null ? city.hashCode() : 0);
58+
return result;
59+
}
60+
61+
@Override
62+
public String toString() {
63+
return "Address(id = " + id + ", city = " + city + ")";
64+
}
65+
66+
public long getId() {
67+
return id;
68+
}
69+
70+
public void setId(long id) {
71+
this.id = id;
72+
}
73+
74+
public String getCity() {
75+
return city;
76+
}
77+
78+
public void setCity(String city) {
79+
this.city = city;
80+
}
81+
82+
public Set<Person> getTenants() {
83+
return tenants;
84+
}
85+
86+
public void setTenants(Set<Person> tenants) {
87+
this.tenants = tenants;
88+
}
89+
90+
public Person getLandlord() {
91+
return landlord;
92+
}
93+
94+
public void setLandlord(Person landlord) {
95+
this.landlord = landlord;
96+
}
97+
}
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
package org.hibernate.envers.test.integration.multiplerelations;
2+
3+
import org.hibernate.ejb.Ejb3Configuration;
4+
import org.hibernate.envers.test.AbstractEntityTest;
5+
import org.hibernate.envers.test.Priority;
6+
import org.hibernate.envers.test.tools.TestTools;
7+
import org.hibernate.testing.TestForIssue;
8+
import org.junit.Assert;
9+
import org.junit.Test;
10+
11+
import javax.persistence.EntityManager;
12+
import java.util.Arrays;
13+
14+
/**
15+
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
16+
*/
17+
@TestForIssue(jiraKey = "HHH-7073")
18+
public class MultipleAssociationsTest extends AbstractEntityTest {
19+
private long lukaszId = 0;
20+
private long kingaId = 0;
21+
private long warsawId = 0;
22+
private long cracowId = 0;
23+
24+
@Override
25+
public void configure(Ejb3Configuration cfg) {
26+
cfg.addAnnotatedClass(Person.class);
27+
cfg.addAnnotatedClass(Address.class);
28+
}
29+
30+
@Test
31+
@Priority(10)
32+
public void initData() {
33+
EntityManager em = getEntityManager();
34+
35+
// Revision 1
36+
em.getTransaction().begin();
37+
Person lukasz = new Person("Lukasz");
38+
Person kinga = new Person("Kinga");
39+
Address warsaw = new Address("Warsaw");
40+
warsaw.getTenants().add(lukasz);
41+
warsaw.setLandlord(lukasz);
42+
warsaw.getTenants().add(kinga);
43+
lukasz.getAddresses().add(warsaw);
44+
lukasz.getOwnedAddresses().add(warsaw);
45+
kinga.getAddresses().add(warsaw);
46+
em.persist(lukasz);
47+
em.persist(kinga);
48+
em.persist(warsaw);
49+
em.getTransaction().commit();
50+
51+
lukaszId = lukasz.getId();
52+
kingaId = kinga.getId();
53+
warsawId = warsaw.getId();
54+
55+
// Revision 2
56+
em.getTransaction().begin();
57+
kinga = em.find(Person.class, kinga.getId());
58+
Address cracow = new Address("Cracow");
59+
kinga.getAddresses().add(cracow);
60+
cracow.getTenants().add(kinga);
61+
cracow.setLandlord(kinga);
62+
em.persist(cracow);
63+
em.getTransaction().commit();
64+
65+
cracowId = cracow.getId();
66+
67+
// Revision 3
68+
em.getTransaction().begin();
69+
cracow = em.find(Address.class, cracow.getId());
70+
cracow.setCity("Krakow");
71+
em.merge(cracow);
72+
em.getTransaction().commit();
73+
74+
// Revision 4
75+
em.getTransaction().begin();
76+
lukasz = em.find(Person.class, lukasz.getId());
77+
lukasz.setName("Lucas");
78+
em.merge(lukasz);
79+
em.getTransaction().commit();
80+
81+
// Revision 5
82+
em.getTransaction().begin();
83+
warsaw = em.find(Address.class, warsaw.getId());
84+
lukasz = em.find(Person.class, lukasz.getId());
85+
kinga = em.find(Person.class, kinga.getId());
86+
warsaw.setLandlord(kinga);
87+
kinga.getOwnedAddresses().add(warsaw);
88+
lukasz.getOwnedAddresses().remove(warsaw);
89+
em.merge(warsaw);
90+
em.merge(lukasz);
91+
em.merge(kinga);
92+
em.getTransaction().commit();
93+
}
94+
95+
@Test
96+
public void testRevisionsCounts() {
97+
Assert.assertEquals(Arrays.asList(1, 4, 5), getAuditReader().getRevisions(Person.class, lukaszId));
98+
Assert.assertEquals(Arrays.asList(1, 2, 5), getAuditReader().getRevisions(Person.class, kingaId));
99+
Assert.assertEquals(Arrays.asList(1, 5), getAuditReader().getRevisions(Address.class, warsawId));
100+
Assert.assertEquals(Arrays.asList(2, 3), getAuditReader().getRevisions(Address.class, cracowId));
101+
}
102+
103+
@Test
104+
public void testHistoryOfLukasz() {
105+
Person lukasz = new Person("Lukasz", lukaszId);
106+
Address warsaw = new Address("Warsaw", warsawId);
107+
lukasz.getAddresses().add(warsaw);
108+
lukasz.getOwnedAddresses().add(warsaw);
109+
110+
Person ver1 = getAuditReader().find(Person.class, lukaszId, 1);
111+
Assert.assertEquals(lukasz, ver1);
112+
Assert.assertEquals(lukasz.getAddresses(), ver1.getAddresses());
113+
Assert.assertEquals(lukasz.getOwnedAddresses(), ver1.getOwnedAddresses());
114+
115+
lukasz.setName("Lucas");
116+
117+
Person ver4 = getAuditReader().find(Person.class, lukaszId, 4);
118+
Assert.assertEquals(lukasz, ver4);
119+
120+
lukasz.getOwnedAddresses().remove(warsaw);
121+
122+
Person ver5 = getAuditReader().find(Person.class, lukaszId, 5);
123+
Assert.assertEquals(lukasz.getOwnedAddresses(), ver5.getOwnedAddresses());
124+
}
125+
126+
@Test
127+
public void testHistoryOfKinga() {
128+
Person kinga = new Person("Kinga", kingaId);
129+
Address warsaw = new Address("Warsaw", warsawId);
130+
kinga.getAddresses().add(warsaw);
131+
132+
Person ver1 = getAuditReader().find(Person.class, kingaId, 1);
133+
Assert.assertEquals(kinga, ver1);
134+
Assert.assertEquals(kinga.getAddresses(), ver1.getAddresses());
135+
Assert.assertEquals(kinga.getOwnedAddresses(), ver1.getOwnedAddresses());
136+
137+
Address cracow = new Address("Cracow", cracowId);
138+
kinga.getOwnedAddresses().add(cracow);
139+
kinga.getAddresses().add(cracow);
140+
141+
Person ver2 = getAuditReader().find(Person.class, kingaId, 2);
142+
Assert.assertEquals(kinga, ver2);
143+
Assert.assertEquals(kinga.getAddresses(), ver2.getAddresses());
144+
Assert.assertEquals(kinga.getOwnedAddresses(), ver2.getOwnedAddresses());
145+
146+
kinga.getOwnedAddresses().add(warsaw);
147+
cracow.setCity("Krakow");
148+
149+
Person ver5 = getAuditReader().find(Person.class, kingaId, 5);
150+
Assert.assertEquals(TestTools.makeSet(kinga.getAddresses()), TestTools.makeSet(ver5.getAddresses()));
151+
Assert.assertEquals(TestTools.makeSet(kinga.getOwnedAddresses()), TestTools.makeSet(ver5.getOwnedAddresses()));
152+
}
153+
154+
@Test
155+
public void testHistoryOfCracow() {
156+
Address cracow = new Address("Cracow", cracowId);
157+
Person kinga = new Person("Kinga", kingaId);
158+
cracow.getTenants().add(kinga);
159+
cracow.setLandlord(kinga);
160+
161+
Address ver2 = getAuditReader().find(Address.class, cracowId, 2);
162+
Assert.assertEquals(cracow, ver2);
163+
Assert.assertEquals(cracow.getTenants(), ver2.getTenants());
164+
Assert.assertEquals(cracow.getLandlord().getId(), ver2.getLandlord().getId());
165+
166+
cracow.setCity("Krakow");
167+
168+
Address ver3 = getAuditReader().find(Address.class, cracowId, 3);
169+
Assert.assertEquals(cracow, ver3);
170+
}
171+
172+
@Test
173+
public void testHistoryOfWarsaw() {
174+
Address warsaw = new Address("Warsaw", warsawId);
175+
Person kinga = new Person("Kinga", kingaId);
176+
Person lukasz = new Person("Lukasz", lukaszId);
177+
warsaw.getTenants().add(lukasz);
178+
warsaw.getTenants().add(kinga);
179+
warsaw.setLandlord(lukasz);
180+
181+
Address ver1 = getAuditReader().find(Address.class, warsawId, 1);
182+
Assert.assertEquals(warsaw, ver1);
183+
Assert.assertEquals(warsaw.getTenants(), ver1.getTenants());
184+
Assert.assertEquals(warsaw.getLandlord().getId(), ver1.getLandlord().getId());
185+
186+
warsaw.setLandlord(kinga);
187+
188+
Address ver5 = getAuditReader().find(Address.class, warsawId, 5);
189+
Assert.assertEquals(warsaw.getLandlord().getId(), ver5.getLandlord().getId());
190+
}
191+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package org.hibernate.envers.test.integration.multiplerelations;
2+
3+
import org.hibernate.envers.Audited;
4+
5+
import javax.persistence.*;
6+
import java.io.Serializable;
7+
import java.util.HashSet;
8+
import java.util.Set;
9+
10+
/**
11+
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
12+
*/
13+
@Entity
14+
@Audited
15+
public class Person implements Serializable {
16+
@Id
17+
@GeneratedValue
18+
private long id;
19+
20+
private String name;
21+
22+
@ManyToMany(cascade = {CascadeType.PERSIST})
23+
@JoinTable(name = "PERSON_ADDRESS",
24+
joinColumns = {@JoinColumn(name = "personId", nullable = false)},
25+
inverseJoinColumns = {@JoinColumn(name = "addressId", nullable = false)})
26+
private Set<Address> addresses = new HashSet<Address>();
27+
28+
@OneToMany(mappedBy = "landlord", cascade = {CascadeType.PERSIST}, orphanRemoval = true)
29+
private Set<Address> ownedAddresses = new HashSet<Address>();
30+
31+
public Person() {
32+
}
33+
34+
public Person(String name) {
35+
this.name = name;
36+
}
37+
38+
public Person(String name, long id) {
39+
this.id = id;
40+
this.name = name;
41+
}
42+
43+
@Override
44+
public boolean equals(Object o) {
45+
if (this == o) return true;
46+
if (!(o instanceof Person)) return false;
47+
48+
Person person = (Person) o;
49+
50+
if (id != person.id) return false;
51+
if (name != null ? !name.equals(person.name) : person.name != null) return false;
52+
53+
return true;
54+
}
55+
56+
@Override
57+
public int hashCode() {
58+
int result = (int) (id ^ (id >>> 32));
59+
result = 31 * result + (name != null ? name.hashCode() : 0);
60+
return result;
61+
}
62+
63+
@Override
64+
public String toString() {
65+
return "Person(id = " + id + ", name = " + name + ")";
66+
}
67+
68+
public long getId() {
69+
return id;
70+
}
71+
72+
public void setId(long id) {
73+
this.id = id;
74+
}
75+
76+
public String getName() {
77+
return name;
78+
}
79+
80+
public void setName(String name) {
81+
this.name = name;
82+
}
83+
84+
public Set<Address> getAddresses() {
85+
return addresses;
86+
}
87+
88+
public void setAddresses(Set<Address> addresses) {
89+
this.addresses = addresses;
90+
}
91+
92+
public Set<Address> getOwnedAddresses() {
93+
return ownedAddresses;
94+
}
95+
96+
public void setOwnedAddresses(Set<Address> ownedAddresses) {
97+
this.ownedAddresses = ownedAddresses;
98+
}
99+
}

0 commit comments

Comments
 (0)