Skip to content

Commit

Permalink
HHH-4962 - Prefix and test case
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasz-antoniak committed Jan 24, 2012
1 parent 81ee788 commit 12464d7
Show file tree
Hide file tree
Showing 4 changed files with 288 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
import org.hibernate.envers.tools.Tools;
import org.hibernate.mapping.Collection;
import org.hibernate.mapping.IndexedCollection;
import org.hibernate.mapping.ManyToOne;
import org.hibernate.mapping.OneToMany;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.Property;
Expand Down Expand Up @@ -639,6 +640,12 @@ private String searchMappedBy(PersistentClass referencedClass, Table collectionT
if (((Collection) property.getValue()).getCollectionTable() == collectionTable) {
return property.getName();
}
} else if (property.getValue() instanceof ManyToOne) {
// The equality is intentional. We want to find a collection property with the same collection table.
//noinspection ObjectEquality
if (property.getValue().getTable() == collectionTable) {
return property.getName();
}
}
}
return null;
Expand Down
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));
}
}
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;
}
}
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;
}
}

0 comments on commit 12464d7

Please sign in to comment.