HHH-3351 : dynamic map entity-persister and subclassing
git-svn-id: https://svn.jboss.org/repos/hibernate/core/branches/Branch_3_3@15408 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
parent
ba60017ec3
commit
569a84d415
|
@ -3642,7 +3642,14 @@ public abstract class AbstractEntityPersister
|
|||
else {
|
||||
final String concreteEntityName = getTuplizer( entityMode )
|
||||
.determineConcreteSubclassEntityName( instance, factory );
|
||||
return factory.getEntityPersister( concreteEntityName );
|
||||
if ( concreteEntityName == null || getEntityName().equals( concreteEntityName ) ) {
|
||||
// the contract of EntityTuplizer.determineConcreteSubclassEntityName says that returning null
|
||||
// is an indication that the specified entity-name (this.getEntityName) should be used.
|
||||
return this;
|
||||
}
|
||||
else {
|
||||
return factory.getEntityPersister( concreteEntityName );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -20,7 +20,6 @@
|
|||
* Free Software Foundation, Inc.
|
||||
* 51 Franklin Street, Fifth Floor
|
||||
* Boston, MA 02110-1301 USA
|
||||
*
|
||||
*/
|
||||
package org.hibernate.tuple.entity;
|
||||
|
||||
|
@ -57,6 +56,9 @@ public class DynamicMapEntityTuplizer extends AbstractEntityTuplizer {
|
|||
super(entityMetamodel, mappedEntity);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public EntityMode getEntityMode() {
|
||||
return EntityMode.MAP;
|
||||
}
|
||||
|
@ -70,18 +72,30 @@ public class DynamicMapEntityTuplizer extends AbstractEntityTuplizer {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
protected Getter buildPropertyGetter(Property mappedProperty, PersistentClass mappedEntity) {
|
||||
return buildPropertyAccessor(mappedProperty).getGetter( null, mappedProperty.getName() );
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
protected Setter buildPropertySetter(Property mappedProperty, PersistentClass mappedEntity) {
|
||||
return buildPropertyAccessor(mappedProperty).getSetter( null, mappedProperty.getName() );
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
protected Instantiator buildInstantiator(PersistentClass mappingInfo) {
|
||||
return new DynamicMapInstantiator( mappingInfo );
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
protected ProxyFactory buildProxyFactory(PersistentClass mappingInfo, Getter idGetter, Setter idSetter) {
|
||||
|
||||
ProxyFactory pf = new MapProxyFactory();
|
||||
|
@ -103,24 +117,38 @@ public class DynamicMapEntityTuplizer extends AbstractEntityTuplizer {
|
|||
return pf;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public Class getMappedClass() {
|
||||
return Map.class;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public Class getConcreteProxyClass() {
|
||||
return Map.class;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public boolean isInstrumented() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public EntityNameResolver[] getEntityNameResolvers() {
|
||||
return new EntityNameResolver[] { BasicEntityNameResolver.INSTANCE };
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public String determineConcreteSubclassEntityName(Object entityInstance, SessionFactoryImplementor factory) {
|
||||
// TODO : do we need an explicit isInstance check here, or is that asserted prior to here?
|
||||
return extractEmbeddedEntityName( ( Map ) entityInstance );
|
||||
}
|
||||
|
||||
|
@ -131,6 +159,9 @@ public class DynamicMapEntityTuplizer extends AbstractEntityTuplizer {
|
|||
public static class BasicEntityNameResolver implements EntityNameResolver {
|
||||
public static final BasicEntityNameResolver INSTANCE = new BasicEntityNameResolver();
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public String resolveEntityName(Object entity) {
|
||||
final String entityName = extractEmbeddedEntityName( ( Map ) entity );
|
||||
if ( entityName == null ) {
|
||||
|
@ -139,10 +170,16 @@ public class DynamicMapEntityTuplizer extends AbstractEntityTuplizer {
|
|||
return entityName;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public boolean equals(Object obj) {
|
||||
return getClass().equals( obj.getClass() );
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public int hashCode() {
|
||||
return getClass().hashCode();
|
||||
}
|
||||
|
|
|
@ -0,0 +1,42 @@
|
|||
<?xml version="1.0"?>
|
||||
|
||||
<!--
|
||||
~ 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
|
||||
-->
|
||||
<!DOCTYPE hibernate-mapping PUBLIC
|
||||
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
|
||||
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
|
||||
|
||||
<hibernate-mapping>
|
||||
|
||||
<class entity-name="Superclass" discriminator-value="A">
|
||||
<id name="id" type="integer"><generator class="native"/></id>
|
||||
<discriminator column="DISC" type="string" />
|
||||
<property name="dataA" type="string"/>
|
||||
|
||||
<subclass entity-name="Subclass" discriminator-value="B">
|
||||
<property name="dataB" type="string"/>
|
||||
</subclass>
|
||||
</class>
|
||||
|
||||
</hibernate-mapping>
|
|
@ -0,0 +1,58 @@
|
|||
/*
|
||||
* 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.test.entitymode.map.subclass;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
import org.hibernate.junit.functional.FunctionalTestCase;
|
||||
import org.hibernate.Session;
|
||||
|
||||
/**
|
||||
* TODO : javadoc
|
||||
*
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
public class SubclassDynamicMapTest extends FunctionalTestCase {
|
||||
public SubclassDynamicMapTest(String string) {
|
||||
super( string );
|
||||
}
|
||||
|
||||
public String[] getMappings() {
|
||||
return new String[] { "entitymode/map/subclass/Mappings.hbm.xml" };
|
||||
}
|
||||
|
||||
public void testConcreateSubclassDeterminationOnEmptyDynamicMap() {
|
||||
Session s = openSession();
|
||||
s.beginTransaction();
|
||||
s.persist( "Superclass", new HashMap() );
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
|
||||
s = openSession();
|
||||
s.beginTransaction();
|
||||
s.createQuery( "delete Superclass" ).executeUpdate();
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue