HHH-3573: fix with tests (a not-insertable column can now be a many-to-one relation)

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@15743 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Adam Warski 2008-12-29 08:52:31 +00:00
parent e1926b4ad9
commit 92a4af082b
8 changed files with 198 additions and 5 deletions

View File

@ -119,7 +119,7 @@ public final class AuditMetadataGenerator {
// only second pass
if (!firstPass) {
toOneRelationMetadataGenerator.addToOne(parent, persistentPropertyAuditingData, value, currentMapper,
entityName);
entityName, insertable);
}
} else if (type instanceof OneToOneType) {
// only second pass

View File

@ -212,7 +212,7 @@ public final class CollectionMetadataGenerator {
MetadataTools.ColumnNameIterator columnNameIterator,
IdMappingData relatedIdMapping) {
Element properties = (Element) relatedIdMapping.getXmlRelationMapping().clone();
MetadataTools.prefixNamesInPropertyElement(properties, prefix, columnNameIterator, true);
MetadataTools.prefixNamesInPropertyElement(properties, prefix, columnNameIterator, true, true);
for (Element idProperty : (java.util.List<Element>) properties.elements()) {
xmlMapping.add((Element) idProperty.clone());
}

View File

@ -163,7 +163,7 @@ public class MetadataTools {
@SuppressWarnings({"unchecked"})
public static void prefixNamesInPropertyElement(Element element, String prefix, ColumnNameIterator columnNameIterator,
boolean changeToKey) {
boolean changeToKey, boolean insertable) {
Iterator<Element> properties = element.elementIterator();
while (properties.hasNext()) {
Element property = properties.next();
@ -179,6 +179,9 @@ public class MetadataTools {
if (changeToKey) {
property.setName("key-property");
}
Attribute insert = property.attribute("insert");
insert.setText(Boolean.toString(insertable));
}
}
}

View File

@ -50,7 +50,7 @@ public final class ToOneRelationMetadataGenerator {
@SuppressWarnings({"unchecked"})
void addToOne(Element parent, PersistentPropertyAuditingData persistentPropertyAuditingData, Value value,
CompositeMapperBuilder mapper, String entityName) {
CompositeMapperBuilder mapper, String entityName, boolean insertable) {
String referencedEntityName = ((ToOne) value).getReferencedEntityName();
EntityConfiguration configuration = mainGenerator.getEntitiesConfigurations().get(referencedEntityName);
@ -74,7 +74,7 @@ public final class ToOneRelationMetadataGenerator {
properties.addAttribute("name", persistentPropertyAuditingData.getName());
MetadataTools.prefixNamesInPropertyElement(properties, lastPropertyPrefix,
MetadataTools.getColumnNameIterator(value.getColumnIterator()), false);
MetadataTools.getColumnNameIterator(value.getColumnIterator()), false, insertable);
parent.add(properties);
// Adding mapper for the id

View File

@ -0,0 +1,79 @@
package org.hibernate.envers.test.integration.notinsertable.manytoone;
import java.util.Arrays;
import javax.persistence.EntityManager;
import org.hibernate.envers.test.AbstractEntityTest;
import org.hibernate.ejb.Ejb3Configuration;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class ManyToOneNotInsertable extends AbstractEntityTest {
private Integer mto_id1;
private Integer type_id1;
private Integer type_id2;
public void configure(Ejb3Configuration cfg) {
cfg.addAnnotatedClass(ManyToOneNotInsertableEntity.class);
cfg.addAnnotatedClass(NotInsertableEntityType.class);
}
@BeforeClass(dependsOnMethods = "init")
public void initData() {
EntityManager em = getEntityManager();
mto_id1 = 1;
type_id1 = 2;
type_id2 = 3;
// Rev 1
// Preparing the types
em.getTransaction().begin();
NotInsertableEntityType type1 = new NotInsertableEntityType(type_id1, "type1");
NotInsertableEntityType type2 = new NotInsertableEntityType(type_id2, "type2");
em.persist(type1);
em.persist(type2);
em.getTransaction().commit();
// Rev 2
em.getTransaction().begin();
ManyToOneNotInsertableEntity master = new ManyToOneNotInsertableEntity(mto_id1, type_id1, type1);
em.persist(master);
em.getTransaction().commit();
// Rev 2
em.getTransaction().begin();
master = em.find(ManyToOneNotInsertableEntity.class, mto_id1);
master.setNumber(type_id2);
em.getTransaction().commit();
}
@Test
public void testRevisionCounts() {
assert Arrays.asList(1).equals(getAuditReader().getRevisions(NotInsertableEntityType.class, type_id1));
assert Arrays.asList(1).equals(getAuditReader().getRevisions(NotInsertableEntityType.class, type_id2));
assert Arrays.asList(2, 3).equals(getAuditReader().getRevisions(ManyToOneNotInsertableEntity.class, mto_id1));
}
@Test
public void testNotInsertableEntity() {
ManyToOneNotInsertableEntity ver1 = getAuditReader().find(ManyToOneNotInsertableEntity.class, mto_id1, 1);
ManyToOneNotInsertableEntity ver2 = getAuditReader().find(ManyToOneNotInsertableEntity.class, mto_id1, 2);
ManyToOneNotInsertableEntity ver3 = getAuditReader().find(ManyToOneNotInsertableEntity.class, mto_id1, 3);
NotInsertableEntityType type1 = getEntityManager().find(NotInsertableEntityType.class, type_id1);
NotInsertableEntityType type2 = getEntityManager().find(NotInsertableEntityType.class, type_id2);
assert ver1 == null;
assert ver2.getType().equals(type1);
assert ver3.getType().equals(type2);
}
}

View File

@ -0,0 +1,52 @@
package org.hibernate.envers.test.integration.notinsertable.manytoone;
import org.hibernate.envers.Audited;
import javax.persistence.*;
@Entity
@Audited
public class ManyToOneNotInsertableEntity {
@Id
private Integer id;
@Basic
@Column(name = "number")
private Integer number;
@ManyToOne
@JoinColumn(name = "number", insertable = false, updatable = false)
private NotInsertableEntityType type;
public ManyToOneNotInsertableEntity() { }
public ManyToOneNotInsertableEntity(Integer id, Integer number, NotInsertableEntityType type) {
this.id = id;
this.number = number;
this.type = type;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getNumber() {
return number;
}
public void setNumber(Integer number) {
this.number = number;
}
public NotInsertableEntityType getType() {
return type;
}
public void setType(NotInsertableEntityType type) {
this.type = type;
}
}

View File

@ -0,0 +1,58 @@
package org.hibernate.envers.test.integration.notinsertable.manytoone;
import org.hibernate.envers.Audited;
import javax.persistence.*;
@Entity
@Audited
public class NotInsertableEntityType {
public NotInsertableEntityType(Integer typeId, String type) {
this.typeId = typeId;
this.type = type;
}
public NotInsertableEntityType() { }
@Id
private Integer typeId;
@Basic
private String type;
public Integer getTypeId() {
return typeId;
}
public void setTypeId(Integer typeId) {
this.typeId = typeId;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
NotInsertableEntityType that = (NotInsertableEntityType) o;
if (type != null ? !type.equals(that.type) : that.type != null) return false;
if (typeId != null ? !typeId.equals(that.typeId) : that.typeId != null) return false;
return true;
}
@Override
public int hashCode() {
int result = typeId != null ? typeId.hashCode() : 0;
result = 31 * result + (type != null ? type.hashCode() : 0);
return result;
}
}

View File

@ -32,6 +32,7 @@
<package name="org.hibernate.envers.test.integration.naming" />
<package name="org.hibernate.envers.test.integration.naming.ids" />
<package name="org.hibernate.envers.test.integration.notinsertable" />
<package name="org.hibernate.envers.test.integration.notinsertable.manytoone" />
<package name="org.hibernate.envers.test.integration.onetomany" />
<package name="org.hibernate.envers.test.integration.onetomany.detached" />
<package name="org.hibernate.envers.test.integration.onetoone.bidirectional" />