HHH-4928:
- using unsupported constructs in ids of non-audited entities doesn't cause an exception git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@18827 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
parent
32cd3b4359
commit
6c44b281b5
|
@ -345,7 +345,7 @@ public final class AuditMetadataGenerator {
|
|||
|
||||
if (!isAudited) {
|
||||
String entityName = pc.getEntityName();
|
||||
IdMappingData idMapper = idMetadataGenerator.addId(pc);
|
||||
IdMappingData idMapper = idMetadataGenerator.addId(pc, false);
|
||||
|
||||
if (idMapper == null) {
|
||||
// Unsupported id mapping, e.g. key-many-to-one. If the entity is used in auditing, an exception
|
||||
|
@ -375,7 +375,7 @@ public final class AuditMetadataGenerator {
|
|||
AuditTableData auditTableData = new AuditTableData(auditEntityName, auditTableName, schema, catalog);
|
||||
|
||||
// Generating a mapping for the id
|
||||
IdMappingData idMapper = idMetadataGenerator.addId(pc);
|
||||
IdMappingData idMapper = idMetadataGenerator.addId(pc, true);
|
||||
|
||||
InheritanceType inheritanceType = InheritanceType.get(pc);
|
||||
|
||||
|
|
|
@ -56,7 +56,8 @@ public final class IdMetadataGenerator {
|
|||
}
|
||||
|
||||
@SuppressWarnings({"unchecked"})
|
||||
private void addIdProperties(Element parent, Iterator<Property> properties, SimpleMapperBuilder mapper, boolean key) {
|
||||
private boolean addIdProperties(Element parent, Iterator<Property> properties, SimpleMapperBuilder mapper, boolean key,
|
||||
boolean audited) {
|
||||
while (properties.hasNext()) {
|
||||
Property property = properties.next();
|
||||
Type propertyType = property.getType();
|
||||
|
@ -67,14 +68,23 @@ public final class IdMetadataGenerator {
|
|||
property.getValue(), mapper, true, key);
|
||||
|
||||
if (!added) {
|
||||
throw new MappingException("Type not supported: " + propertyType.getClass().getName());
|
||||
// If the entity is audited, and a non-supported id component is used, throwing an exception.
|
||||
// If the entity is not audited, then we simply don't support this entity, even in
|
||||
// target relation mode not audited.
|
||||
if (audited) {
|
||||
throw new MappingException("Type not supported: " + propertyType.getClass().getName());
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@SuppressWarnings({"unchecked"})
|
||||
IdMappingData addId(PersistentClass pc) {
|
||||
IdMappingData addId(PersistentClass pc, boolean audited) {
|
||||
// Xml mapping which will be used for relations
|
||||
Element rel_id_mapping = new DefaultElement("properties");
|
||||
// Xml mapping which will be used for the primary key of the versions table
|
||||
|
@ -93,20 +103,28 @@ public final class IdMetadataGenerator {
|
|||
// Multiple id
|
||||
|
||||
mapper = new MultipleIdMapper(((Component) pc.getIdentifier()).getComponentClassName());
|
||||
addIdProperties(rel_id_mapping, (Iterator<Property>) id_mapper.getPropertyIterator(), mapper, false);
|
||||
if (!addIdProperties(rel_id_mapping, (Iterator<Property>) id_mapper.getPropertyIterator(), mapper, false, audited)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// null mapper - the mapping where already added the first time, now we only want to generate the xml
|
||||
addIdProperties(orig_id_mapping, (Iterator<Property>) id_mapper.getPropertyIterator(), null, true);
|
||||
if (!addIdProperties(orig_id_mapping, (Iterator<Property>) id_mapper.getPropertyIterator(), null, true, audited)) {
|
||||
return null;
|
||||
}
|
||||
} else if (id_prop.isComposite()) {
|
||||
// Embedded id
|
||||
|
||||
Component id_component = (Component) id_prop.getValue();
|
||||
|
||||
mapper = new EmbeddedIdMapper(getIdPropertyData(id_prop), id_component.getComponentClassName());
|
||||
addIdProperties(rel_id_mapping, (Iterator<Property>) id_component.getPropertyIterator(), mapper, false);
|
||||
if (!addIdProperties(rel_id_mapping, (Iterator<Property>) id_component.getPropertyIterator(), mapper, false, audited)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// null mapper - the mapping where already added the first time, now we only want to generate the xml
|
||||
addIdProperties(orig_id_mapping, (Iterator<Property>) id_component.getPropertyIterator(), null, true);
|
||||
if (!addIdProperties(orig_id_mapping, (Iterator<Property>) id_component.getPropertyIterator(), null, true, audited)) {
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
// Single id
|
||||
|
||||
|
|
|
@ -0,0 +1,55 @@
|
|||
package org.hibernate.envers.test.entities.ids;
|
||||
|
||||
import javax.persistence.EmbeddedId;
|
||||
import javax.persistence.Entity;
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* @author Adam Warski (adam at warski dot org)
|
||||
*/
|
||||
@Entity
|
||||
public class ManyToOneIdNotAuditedTestEntity implements Serializable {
|
||||
@EmbeddedId
|
||||
private ManyToOneNotAuditedEmbId id;
|
||||
|
||||
private String data;
|
||||
|
||||
public ManyToOneIdNotAuditedTestEntity() {
|
||||
}
|
||||
|
||||
public ManyToOneNotAuditedEmbId getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(ManyToOneNotAuditedEmbId id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public void setData(String data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
ManyToOneIdNotAuditedTestEntity that = (ManyToOneIdNotAuditedTestEntity) 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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
package org.hibernate.envers.test.entities.ids;
|
||||
|
||||
import org.hibernate.envers.test.entities.UnversionedStrTestEntity;
|
||||
|
||||
import javax.persistence.Embeddable;
|
||||
import javax.persistence.ManyToOne;
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* @author Adam Warski (adam at warski dot org)
|
||||
*/
|
||||
@Embeddable
|
||||
public class ManyToOneNotAuditedEmbId implements Serializable {
|
||||
@ManyToOne
|
||||
private UnversionedStrTestEntity id;
|
||||
|
||||
public ManyToOneNotAuditedEmbId() {
|
||||
}
|
||||
|
||||
public ManyToOneNotAuditedEmbId(UnversionedStrTestEntity id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public UnversionedStrTestEntity getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(UnversionedStrTestEntity id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
ManyToOneNotAuditedEmbId that = (ManyToOneNotAuditedEmbId) o;
|
||||
|
||||
if (id != null ? !id.equals(that.id) : that.id != null) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return id != null ? id.hashCode() : 0;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
package org.hibernate.envers.test.integration.ids;
|
||||
|
||||
import org.hibernate.ejb.Ejb3Configuration;
|
||||
import org.hibernate.envers.test.AbstractEntityTest;
|
||||
import org.hibernate.envers.test.entities.StrTestEntity;
|
||||
import org.hibernate.envers.test.entities.UnversionedStrTestEntity;
|
||||
import org.hibernate.envers.test.entities.ids.ManyToOneIdNotAuditedTestEntity;
|
||||
import org.hibernate.envers.test.entities.ids.ManyToOneNotAuditedEmbId;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
|
||||
/**
|
||||
* A test checking that when using Envers it is possible to have non-audited entities that use unsupported
|
||||
* components in their ids, e.g. a many-to-one join to another entity.
|
||||
* @author Adam Warski (adam at warski dot org)
|
||||
*/
|
||||
public class ManyToOneIdNotAudited extends AbstractEntityTest {
|
||||
private ManyToOneNotAuditedEmbId id1;
|
||||
|
||||
public void configure(Ejb3Configuration cfg) {
|
||||
cfg.addAnnotatedClass(ManyToOneIdNotAuditedTestEntity.class);
|
||||
cfg.addAnnotatedClass(UnversionedStrTestEntity.class);
|
||||
cfg.addAnnotatedClass(StrTestEntity.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void initData() {
|
||||
// Revision 1
|
||||
EntityManager em = getEntityManager();
|
||||
em.getTransaction().begin();
|
||||
|
||||
UnversionedStrTestEntity uste = new UnversionedStrTestEntity();
|
||||
uste.setStr("test1");
|
||||
em.persist(uste);
|
||||
|
||||
id1 = new ManyToOneNotAuditedEmbId(uste);
|
||||
|
||||
em.getTransaction().commit();
|
||||
|
||||
// Revision 2
|
||||
em = getEntityManager();
|
||||
em.getTransaction().begin();
|
||||
|
||||
ManyToOneIdNotAuditedTestEntity mtoinate = new ManyToOneIdNotAuditedTestEntity();
|
||||
mtoinate.setData("data1");
|
||||
mtoinate.setId(id1);
|
||||
em.persist(mtoinate);
|
||||
|
||||
em.getTransaction().commit();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue