HHH-3741: fix and testcase; properly computing the referencing property name in a unidirectional collection

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@15912 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Adam Warski 2009-02-06 23:10:40 +00:00
parent dafb54a60f
commit 38e6babc34
8 changed files with 313 additions and 3 deletions

View File

@ -57,6 +57,7 @@ import org.hibernate.event.PreCollectionRemoveEventListener;
import org.hibernate.event.PreCollectionUpdateEvent;
import org.hibernate.event.PreCollectionUpdateEventListener;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.persister.collection.AbstractCollectionPersister;
import org.hibernate.proxy.HibernateProxy;
/**
@ -206,7 +207,7 @@ public class AuditEventListener implements PostInsertEventListener, PostUpdateEv
AuditSync verSync = verCfg.getSyncManager().get(event.getSession());
PersistentCollectionChangeWorkUnit workUnit = new PersistentCollectionChangeWorkUnit(entityName, verCfg,
newColl, collectionEntry.getRole(), oldColl, event.getAffectedOwnerIdOrNull());
newColl, collectionEntry, oldColl, event.getAffectedOwnerIdOrNull());
verSync.addWorkUnit(workUnit);
if (workUnit.containsWork()) {

View File

@ -32,6 +32,8 @@ import org.hibernate.envers.configuration.AuditEntitiesConfiguration;
import org.hibernate.envers.entities.mapper.PersistentCollectionChangeData;
import org.hibernate.Session;
import org.hibernate.persister.collection.AbstractCollectionPersister;
import org.hibernate.engine.CollectionEntry;
import org.hibernate.collection.PersistentCollection;
/**
@ -42,11 +44,12 @@ public class PersistentCollectionChangeWorkUnit extends AbstractAuditWorkUnit im
private final String referencingPropertyName;
public PersistentCollectionChangeWorkUnit(String entityName, AuditConfiguration verCfg,
PersistentCollection collection, String role,
PersistentCollection collection, CollectionEntry collectionEntry,
Serializable snapshot, Serializable id) {
super(entityName, verCfg, null);
referencingPropertyName = role.substring(entityName.length() + 1);
String ownerEntityName = ((AbstractCollectionPersister) collectionEntry.getLoadedPersister()).getOwnerEntityName();
referencingPropertyName = collectionEntry.getRole().substring(ownerEntityName.length() + 1);
collectionChanges = verCfg.getEntCfg().get(getEntityName()).getPropertyMapper()
.mapCollectionChanges(referencingPropertyName, collection, snapshot, id);

View File

@ -0,0 +1,73 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Middleware LLC.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.envers.test.integration.inheritance.joined.relation.unidirectional;
import javax.persistence.Entity;
import javax.persistence.Inheritance;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.InheritanceType;
import org.hibernate.envers.Audited;
/**
* @author Adam Warski (adam at warski dot org)
*/
@Entity
@Audited
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class AbstractContainedEntity {
@Id
@GeneratedValue
private Long id;
public AbstractContainedEntity() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof AbstractContainedEntity)) return false;
AbstractContainedEntity that = (AbstractContainedEntity) 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;
}
}

View File

@ -0,0 +1,71 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Middleware LLC.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.envers.test.integration.inheritance.joined.relation.unidirectional;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.GeneratedValue;
import javax.persistence.OneToMany;
import org.hibernate.envers.Audited;
import java.util.Set;
import java.util.HashSet;
/**
* @author Adam Warski (adam at warski dot org)
*/
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Audited
public abstract class AbstractSetEntity {
@Id
@GeneratedValue
private Integer id;
@OneToMany
private Set<AbstractContainedEntity> entities = new HashSet<AbstractContainedEntity>();
public AbstractSetEntity() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Set<AbstractContainedEntity> getEntities() {
return entities;
}
public void setEntities(Set<AbstractContainedEntity> entities) {
this.entities = entities;
}
}

View File

@ -0,0 +1,37 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Middleware LLC.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.envers.test.integration.inheritance.joined.relation.unidirectional;
import javax.persistence.Entity;
import org.hibernate.envers.Audited;
/**
* @author Adam Warski (adam at warski dot org)
*/
@Entity
@Audited
public class ContainedEntity extends AbstractContainedEntity {
}

View File

@ -0,0 +1,37 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Middleware LLC.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.envers.test.integration.inheritance.joined.relation.unidirectional;
import javax.persistence.Entity;
import org.hibernate.envers.Audited;
/**
* @author Adam Warski (adam at warski dot org)
*/
@Entity
@Audited
public class SetEntity extends AbstractSetEntity {
}

View File

@ -0,0 +1,87 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Middleware LLC.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.envers.test.integration.inheritance.joined.relation.unidirectional;
import javax.persistence.EntityManager;
import org.hibernate.envers.test.AbstractEntityTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.hibernate.ejb.Ejb3Configuration;
import java.util.Arrays;
import java.util.Set;
/**
* @author Adam Warski (adam at warski dot org)
*/
public class UnidirectionalDoubleAbstract extends AbstractEntityTest {
private Long cce1_id;
private Integer cse1_id;
public void configure(Ejb3Configuration cfg) {
cfg.addAnnotatedClass(AbstractContainedEntity.class);
cfg.addAnnotatedClass(AbstractSetEntity.class);
cfg.addAnnotatedClass(ContainedEntity.class);
cfg.addAnnotatedClass(SetEntity.class);
}
@BeforeClass(dependsOnMethods = "init")
public void initData() {
EntityManager em = getEntityManager();
// Rev 1
em.getTransaction().begin();
ContainedEntity cce1 = new ContainedEntity();
em.persist(cce1);
SetEntity cse1 = new SetEntity();
cse1.getEntities().add(cce1);
em.persist(cse1);
em.getTransaction().commit();
cce1_id = cce1.getId();
cse1_id = cse1.getId();
}
@Test
public void testRevisionsCounts() {
assert Arrays.asList(1).equals(getAuditReader().getRevisions(ContainedEntity.class, cce1_id));
assert Arrays.asList(1).equals(getAuditReader().getRevisions(SetEntity.class, cse1_id));
}
@Test
public void testHistoryOfReferencedCollection() {
ContainedEntity cce1 = getEntityManager().find(ContainedEntity.class, cce1_id);
Set<AbstractContainedEntity> entities = getAuditReader().find(SetEntity.class, cse1_id, 1).getEntities();
assert entities.size() == 1;
assert entities.iterator().next() instanceof ContainedEntity;
assert entities.contains(cce1);
}
}

View File

@ -19,6 +19,7 @@
<package name="org.hibernate.envers.test.integration.inheritance.joined.emptychild" />
<package name="org.hibernate.envers.test.integration.inheritance.joined.notownedrelation" />
<package name="org.hibernate.envers.test.integration.inheritance.joined.relation" />
<package name="org.hibernate.envers.test.integration.inheritance.joined.relation.unidirectional" />
<package name="org.hibernate.envers.test.integration.inheritance.single" />
<package name="org.hibernate.envers.test.integration.inheritance.single.childrelation" />
<package name="org.hibernate.envers.test.integration.inheritance.single.notownedrelation" />