forked from hibernate/hibernate-orm
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
81ee788
commit 12464d7
Showing
4 changed files
with
288 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
...a/org/hibernate/envers/test/integration/manytoone/bidirectional/ImplicitMappedByTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package org.hibernate.envers.test.integration.manytoone.bidirectional; | ||
|
||
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; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
/** | ||
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com) | ||
*/ | ||
@TestForIssue(jiraKey = "HHH-4962") | ||
public class ImplicitMappedByTest extends AbstractEntityTest { | ||
private Long ownedId = null; | ||
private Long owning1Id = null; | ||
private Long owning2Id = null; | ||
|
||
public void configure(Ejb3Configuration cfg) { | ||
cfg.addAnnotatedClass(OneToManyOwned.class); | ||
cfg.addAnnotatedClass(ManyToOneOwning.class); | ||
} | ||
|
||
@Test | ||
@Priority(10) | ||
public void initData() { | ||
EntityManager em = getEntityManager(); | ||
|
||
OneToManyOwned owned = new OneToManyOwned("data", null); | ||
Set<ManyToOneOwning> referencing = new HashSet<ManyToOneOwning>(); | ||
ManyToOneOwning owning1 = new ManyToOneOwning("data1", owned); | ||
referencing.add(owning1); | ||
ManyToOneOwning owning2 = new ManyToOneOwning("data2", owned); | ||
referencing.add(owning2); | ||
owned.setReferencing(referencing); | ||
|
||
// Revision 1 | ||
em.getTransaction().begin(); | ||
em.persist(owned); | ||
em.persist(owning1); | ||
em.persist(owning2); | ||
em.getTransaction().commit(); | ||
|
||
ownedId = owned.getId(); | ||
owning1Id = owning1.getId(); | ||
owning2Id = owning2.getId(); | ||
|
||
// Revision 2 | ||
em.getTransaction().begin(); | ||
owning1 = em.find(ManyToOneOwning.class, owning1.getId()); | ||
em.remove(owning1); | ||
em.getTransaction().commit(); | ||
|
||
// Revision 3 | ||
em.getTransaction().begin(); | ||
owning2 = em.find(ManyToOneOwning.class, owning2.getId()); | ||
owning2.setData("data2modified"); | ||
em.merge(owning2); | ||
em.getTransaction().commit(); | ||
} | ||
|
||
@Test | ||
public void testRevisionsCounts() { | ||
Assert.assertEquals(Arrays.asList(1, 2), getAuditReader().getRevisions(OneToManyOwned.class, ownedId)); | ||
Assert.assertEquals(Arrays.asList(1, 2), getAuditReader().getRevisions(ManyToOneOwning.class, owning1Id)); | ||
Assert.assertEquals(Arrays.asList(1, 3), getAuditReader().getRevisions(ManyToOneOwning.class, owning2Id)); | ||
} | ||
|
||
@Test | ||
public void testHistoryOfOwned() { | ||
OneToManyOwned owned = new OneToManyOwned("data", null, ownedId); | ||
Set<ManyToOneOwning> referencing = new HashSet<ManyToOneOwning>(); | ||
ManyToOneOwning owning1 = new ManyToOneOwning("data1", owned, owning1Id); | ||
referencing.add(owning1); | ||
ManyToOneOwning owning2 = new ManyToOneOwning("data2", owned, owning2Id); | ||
referencing.add(owning2); | ||
owned.setReferencing(referencing); | ||
|
||
OneToManyOwned ver1 = getAuditReader().find(OneToManyOwned.class, ownedId, 1); | ||
Assert.assertEquals(owned, ver1); | ||
Assert.assertEquals(TestTools.makeSet(owning1, owning2), ver1.getReferencing()); | ||
} | ||
|
||
@Test | ||
public void testHistoryOfOwning1() { | ||
ManyToOneOwning ver1 = new ManyToOneOwning("data1", null, owning1Id); | ||
Assert.assertEquals(ver1, getAuditReader().find(ManyToOneOwning.class, owning1Id, 1)); | ||
} | ||
|
||
@Test | ||
public void testHistoryOfOwning2() { | ||
ManyToOneOwning ver1 = new ManyToOneOwning("data2", null, owning2Id); | ||
ManyToOneOwning ver3 = new ManyToOneOwning("data2modified", null, owning2Id); | ||
|
||
Assert.assertEquals(ver1, getAuditReader().find(ManyToOneOwning.class, owning2Id, 1)); | ||
Assert.assertEquals(ver3, getAuditReader().find(ManyToOneOwning.class, owning2Id, 3)); | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
...x/java/org/hibernate/envers/test/integration/manytoone/bidirectional/ManyToOneOwning.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package org.hibernate.envers.test.integration.manytoone.bidirectional; | ||
|
||
import org.hibernate.envers.Audited; | ||
|
||
import javax.persistence.*; | ||
import java.io.Serializable; | ||
|
||
/** | ||
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com) | ||
*/ | ||
@Entity | ||
@Audited | ||
public class ManyToOneOwning implements Serializable { | ||
@Id | ||
@GeneratedValue | ||
private Long id; | ||
|
||
private String data; | ||
|
||
@ManyToOne | ||
@JoinTable(name = "many_to_one_join_table", joinColumns = @JoinColumn(name = "owning_id"), | ||
inverseJoinColumns = @JoinColumn(name = "owned_id")) | ||
private OneToManyOwned references; | ||
|
||
public ManyToOneOwning() { | ||
} | ||
|
||
public ManyToOneOwning(String data, OneToManyOwned references) { | ||
this.data = data; | ||
this.references = references; | ||
} | ||
|
||
public ManyToOneOwning(String data, OneToManyOwned references, Long id) { | ||
this.id = id; | ||
this.data = data; | ||
this.references = references; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (!(o instanceof ManyToOneOwning)) return false; | ||
|
||
ManyToOneOwning that = (ManyToOneOwning) o; | ||
|
||
if (data != null ? !data.equals(that.data) : that.data != 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 + (data != null ? data.hashCode() : 0); | ||
return result; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "ManyToOneOwning(id = " + id + ", data = " + data + ")"; | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public String getData() { | ||
return data; | ||
} | ||
|
||
public void setData(String data) { | ||
this.data = data; | ||
} | ||
|
||
public OneToManyOwned getReferences() { | ||
return references; | ||
} | ||
|
||
public void setReferences(OneToManyOwned references) { | ||
this.references = references; | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
...ix/java/org/hibernate/envers/test/integration/manytoone/bidirectional/OneToManyOwned.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package org.hibernate.envers.test.integration.manytoone.bidirectional; | ||
|
||
import org.hibernate.envers.Audited; | ||
|
||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.Id; | ||
import javax.persistence.OneToMany; | ||
import java.io.Serializable; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
/** | ||
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com) | ||
*/ | ||
@Entity | ||
@Audited | ||
public class OneToManyOwned implements Serializable { | ||
@Id | ||
@GeneratedValue | ||
private Long id; | ||
|
||
private String data; | ||
|
||
@OneToMany(mappedBy="references") | ||
private Set<ManyToOneOwning> referencing = new HashSet<ManyToOneOwning>(); | ||
|
||
public OneToManyOwned() { | ||
} | ||
|
||
public OneToManyOwned(String data, Set<ManyToOneOwning> referencing) { | ||
this.data = data; | ||
this.referencing = referencing; | ||
} | ||
|
||
public OneToManyOwned(String data, Set<ManyToOneOwning> referencing, Long id) { | ||
this.id = id; | ||
this.data = data; | ||
this.referencing = referencing; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (!(o instanceof OneToManyOwned)) return false; | ||
|
||
OneToManyOwned that = (OneToManyOwned) o; | ||
|
||
if (data != null ? !data.equals(that.data) : that.data != 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 + (data != null ? data.hashCode() : 0); | ||
return result; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "OneToManyOwned(id = " + id + ", data = " + data + ")"; | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public String getData() { | ||
return data; | ||
} | ||
|
||
public void setData(String data) { | ||
this.data = data; | ||
} | ||
|
||
public Set<ManyToOneOwning> getReferencing() { | ||
return referencing; | ||
} | ||
|
||
public void setReferencing(Set<ManyToOneOwning> referencing) { | ||
this.referencing = referencing; | ||
} | ||
} |