HHH-7046 - Polymorphic query by natural ID broken

This commit is contained in:
Steve Ebersole 2012-02-15 17:18:41 -06:00
parent a21ba713ab
commit 2107411faf
4 changed files with 188 additions and 5 deletions

View File

@ -37,6 +37,8 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import org.jboss.logging.Logger;
import org.hibernate.AssertionFailure;
import org.hibernate.EntityMode;
import org.hibernate.FetchMode;
@ -121,7 +123,6 @@ import org.hibernate.type.EntityType;
import org.hibernate.type.Type;
import org.hibernate.type.TypeHelper;
import org.hibernate.type.VersionType;
import org.jboss.logging.Logger;
/**
* Basic functionality for persisting an entity via JDBC
@ -3597,16 +3598,15 @@ public abstract class AbstractEntityPersister
else {
sqlIdentityInsertString = null;
}
if (hasNaturalIdentifier()) {
sqlEntityIdByNaturalIdString = generateEntityIdByNaturalIdSql();
}
logStaticSQL();
}
public void postInstantiate() throws MappingException {
if ( hasNaturalIdentifier() ) {
sqlEntityIdByNaturalIdString = generateEntityIdByNaturalIdSql();
}
createLoaders();
createUniqueKeyLoaders();
@ -4571,6 +4571,13 @@ public abstract class AbstractEntityPersister
}
private String generateEntityIdByNaturalIdSql() {
EntityPersister rootPersister = getFactory().getEntityPersister( getRootEntityName() );
if ( rootPersister != this ) {
if ( rootPersister instanceof AbstractEntityPersister ) {
return ( (AbstractEntityPersister) rootPersister ).sqlEntityIdByNaturalIdString;
}
}
Select select = new Select( getFactory().getDialect() );
if ( getFactory().getSettings().isCommentsEnabled() ) {
select.setComment( "get current natural-id->entity-id state " + getEntityName() );

View File

@ -0,0 +1,61 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2012, Red Hat Inc. 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 Inc.
*
* 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.naturalid.inheritance;
import org.hibernate.Session;
import org.junit.Test;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
/**
* @author Steve Ebersole
*/
public class InheritedNaturalIdTest extends BaseCoreFunctionalTestCase {
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[] { Principal.class, User.class };
}
@Test
public void testIt() {
Session s = openSession();
s.beginTransaction();
s.save( new User( "steve" ) );
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
s.bySimpleNaturalId( Principal.class ).load( "steve" );
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
s.bySimpleNaturalId( User.class ).load( "steve" );
s.getTransaction().commit();
s.close();
}
}

View File

@ -0,0 +1,74 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2012, Red Hat Inc. 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 Inc.
*
* 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.naturalid.inheritance;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.MappedSuperclass;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.NaturalId;
/**
* @author Emanuel Kupcik
* @author Steve Ebersole
*/
@Entity
@Inheritance( strategy = InheritanceType.JOINED )
@Table( name = "GK_PRINCIPAL" )
public abstract class Principal {
private Long id;
private String uid;
protected Principal() {
}
protected Principal(String uid) {
this.uid = uid;
}
@Id
@GeneratedValue( generator = "increment" )
@GenericGenerator( name = "increment", strategy = "increment" )
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@NaturalId
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
}

View File

@ -0,0 +1,41 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2012, Red Hat Inc. 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 Inc.
*
* 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.naturalid.inheritance;
import javax.persistence.Entity;
import javax.persistence.Table;
/**
* @author Steve Ebersole
*/
@Entity
@Table( name = "GK_USER" )
public class User extends Principal {
public User() {
}
public User(String uid) {
super( uid );
}
}