HHH-5808 - Fix and test
This commit is contained in:
parent
f70f8d7101
commit
bcfa5ffed9
|
@ -29,6 +29,7 @@ import org.hibernate.envers.tools.Tools;
|
|||
|
||||
/**
|
||||
* @author Adam Warski (adam at warski dot org)
|
||||
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
|
||||
*/
|
||||
public abstract class AbstractCompositeIdMapper extends AbstractIdMapper implements SimpleIdMapperBuilder {
|
||||
protected Map<PropertyData, SingleIdMapper> ids;
|
||||
|
@ -45,6 +46,10 @@ public abstract class AbstractCompositeIdMapper extends AbstractIdMapper impleme
|
|||
}
|
||||
|
||||
public Object mapToIdFromMap(Map data) {
|
||||
if (data == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Object ret;
|
||||
try {
|
||||
ret = Thread.currentThread().getContextClassLoader().loadClass(compositeIdClass).newInstance();
|
||||
|
|
|
@ -0,0 +1,85 @@
|
|||
package org.hibernate.envers.test.integration.onetoone.unidirectional;
|
||||
|
||||
import org.hibernate.envers.Audited;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.IdClass;
|
||||
|
||||
/**
|
||||
* @author Andrei Zagorneanu
|
||||
*/
|
||||
@Entity
|
||||
@Audited
|
||||
@IdClass(AddressPK.class)
|
||||
public class AddressCompositePKEntity {
|
||||
private Integer id;
|
||||
private Integer ver;
|
||||
private String street;
|
||||
|
||||
public AddressCompositePKEntity() {
|
||||
}
|
||||
|
||||
public AddressCompositePKEntity(Integer id, Integer ver, String street) {
|
||||
this.setId(id);
|
||||
this.setVer(ver);
|
||||
this.setStreet(street);
|
||||
}
|
||||
|
||||
@Id
|
||||
@Column(name = "ID", nullable = false, updatable = false)
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@Id
|
||||
@Column(name = "VER", nullable = false, updatable = false)
|
||||
public Integer getVer() {
|
||||
return ver;
|
||||
}
|
||||
|
||||
public void setVer(Integer ver) {
|
||||
this.ver = ver;
|
||||
}
|
||||
|
||||
@Column(name = "STREET", nullable = false)
|
||||
public String getStreet() {
|
||||
return street;
|
||||
}
|
||||
|
||||
public void setStreet(String street) {
|
||||
this.street = street;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return "AddressCompositePKEntity[id = " + getId() + ", ver = " + getVer() + ", street = " + getStreet() + "]";
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = 1;
|
||||
result = 31 * result + ((id == null) ? 0 : id.hashCode());
|
||||
result = 31 * result + ((ver == null) ? 0 : ver.hashCode());
|
||||
result = 31 * result + ((street == null) ? 0 : street.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof AddressCompositePKEntity)) return false;
|
||||
|
||||
AddressCompositePKEntity that = (AddressCompositePKEntity) o;
|
||||
|
||||
if (ver != null ? !ver.equals(that.ver) : that.ver != null) return false;
|
||||
if (id != null ? !id.equals(that.id) : that.id != null) return false;
|
||||
if (street != null ? !street.equals(that.street) : that.street != null) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
package org.hibernate.envers.test.integration.onetoone.unidirectional;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* @author Andrei Zagorneanu
|
||||
*/
|
||||
public class AddressPK implements Serializable {
|
||||
private Integer id;
|
||||
private Integer ver;
|
||||
|
||||
public AddressPK() {
|
||||
}
|
||||
|
||||
public AddressPK(Integer id, Integer ver) {
|
||||
this.setId(id);
|
||||
this.setVer(ver);
|
||||
}
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Integer getVer() {
|
||||
return ver;
|
||||
}
|
||||
|
||||
public void setVer(Integer ver) {
|
||||
this.ver = ver;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return "AddressPK[id = " + getId() + ", ver = " + getVer() + "]";
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = 1;
|
||||
result = 31 * result + ((id == null) ? 0 : id.hashCode());
|
||||
result = 31 * result + ((ver == null) ? 0 : ver.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof AddressPK)) return false;
|
||||
|
||||
AddressPK that = (AddressPK) o;
|
||||
|
||||
if (ver != null ? !ver.equals(that.ver) : that.ver != null) return false;
|
||||
if (id != null ? !id.equals(that.id) : that.id != null) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,86 @@
|
|||
package org.hibernate.envers.test.integration.onetoone.unidirectional;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.JoinColumns;
|
||||
import javax.persistence.OneToOne;
|
||||
|
||||
import org.hibernate.envers.Audited;
|
||||
|
||||
/**
|
||||
* @author Andrei Zagorneanu
|
||||
*/
|
||||
@Entity
|
||||
@Audited
|
||||
public class PersonCompositeFKEntity {
|
||||
private Integer id;
|
||||
private String name;
|
||||
private AddressCompositePKEntity address;
|
||||
|
||||
public PersonCompositeFKEntity() {
|
||||
}
|
||||
|
||||
public PersonCompositeFKEntity(Integer id, String name, AddressCompositePKEntity address) {
|
||||
this.setId(id);
|
||||
this.setName(name);
|
||||
this.setAddress(address);
|
||||
}
|
||||
|
||||
@Id
|
||||
@Column(name = "ID", nullable = false, updatable = false)
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@Column(name = "NAME", nullable = false)
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@OneToOne(cascade = { CascadeType.ALL }, orphanRemoval = true)
|
||||
@JoinColumns({@JoinColumn(name = "ID_ADDRESS", referencedColumnName = "ID"),
|
||||
@JoinColumn(name = "VER_ADDRESS", referencedColumnName = "VER")})
|
||||
public AddressCompositePKEntity getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(AddressCompositePKEntity address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return "PersonCompositeFKEntity[id = " + getId() + ", name = " + getName() + "]";
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = 1;
|
||||
result = 31 * result + ((id == null) ? 0 : id.hashCode());
|
||||
result = 31 * result + ((name == null) ? 0 : name.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof PersonCompositeFKEntity)) return false;
|
||||
|
||||
PersonCompositeFKEntity that = (PersonCompositeFKEntity) o;
|
||||
|
||||
if (id != null ? !id.equals(that.id) : that.id != null) return false;
|
||||
if (name != null ? !name.equals(that.name) : that.name != null) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
package org.hibernate.envers.test.integration.onetoone.unidirectional;
|
||||
|
||||
import java.util.Arrays;
|
||||
import javax.persistence.EntityManager;
|
||||
|
||||
import org.hibernate.envers.test.AbstractEntityTest;
|
||||
import org.hibernate.envers.test.Priority;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.hibernate.ejb.Ejb3Configuration;
|
||||
|
||||
/**
|
||||
* @author Andrei Zagorneanu
|
||||
*/
|
||||
public class UnidirectionalCompositePKWithNulls extends AbstractEntityTest {
|
||||
public void configure(Ejb3Configuration cfg) {
|
||||
cfg.addAnnotatedClass(AddressCompositePKEntity.class);
|
||||
cfg.addAnnotatedClass(PersonCompositeFKEntity.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Priority(10)
|
||||
public void initData() {
|
||||
// address
|
||||
AddressCompositePKEntity address1 = new AddressCompositePKEntity(1, 1, "street1");
|
||||
|
||||
// person with an address
|
||||
PersonCompositeFKEntity person1 = new PersonCompositeFKEntity(1, "person1", address1);
|
||||
// person without an address
|
||||
PersonCompositeFKEntity person2 = new PersonCompositeFKEntity(2, "person2", null);
|
||||
|
||||
EntityManager em = getEntityManager();
|
||||
em.getTransaction().begin();
|
||||
|
||||
em.persist(person1);
|
||||
em.persist(person2);
|
||||
|
||||
em.getTransaction().commit();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRevisionsCounts() {
|
||||
assert Arrays.asList(1).equals(getAuditReader().getRevisions(PersonCompositeFKEntity.class, 1));
|
||||
assert Arrays.asList(1).equals(getAuditReader().getRevisions(PersonCompositeFKEntity.class, 2));
|
||||
assert Arrays.asList(1).equals(getAuditReader().getRevisions(AddressCompositePKEntity.class, new AddressPK(1, 1)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHistoryOfPerson1() {
|
||||
AddressCompositePKEntity address1 = getEntityManager().find(AddressCompositePKEntity.class, new AddressPK(1, 1));
|
||||
PersonCompositeFKEntity revPerson1 = getAuditReader().find(PersonCompositeFKEntity.class, 1, 1);
|
||||
assert address1 != null;
|
||||
assert revPerson1.getAddress().equals(address1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHistoryOfPerson2NotNull() {
|
||||
PersonCompositeFKEntity revPerson2 = getAuditReader().find(PersonCompositeFKEntity.class, 2, 1);
|
||||
assert revPerson2.getAddress() == null;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue