HHH-1574 : AbstractEntityPersister.getNaturalIdentifierSnapshot doesn't work with many-to-one ids (Alex Burgel)
git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@19472 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
parent
ef24b0f556
commit
ad8e4340de
|
@ -4084,8 +4084,13 @@ public abstract class AbstractEntityPersister
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
final EntityKey key = new EntityKey( id, this, session.getEntityMode() );
|
||||||
|
Object owner = session.getPersistenceContext().getEntity( key );
|
||||||
for ( int i = 0; i < naturalIdPropertyCount; i++ ) {
|
for ( int i = 0; i < naturalIdPropertyCount; i++ ) {
|
||||||
snapshot[i] = extractionTypes[i].hydrate( rs, getPropertyAliases( "", naturalIdPropertyIndexes[i] ), session, null );
|
snapshot[i] = extractionTypes[i].hydrate( rs, getPropertyAliases( "", naturalIdPropertyIndexes[i] ), session, null );
|
||||||
|
if (extractionTypes[i].isEntityType()) {
|
||||||
|
snapshot[i] = extractionTypes[i].resolve(snapshot[i], session, owner);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return snapshot;
|
return snapshot;
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,43 @@
|
||||||
|
/*
|
||||||
|
* Hibernate, Relational Persistence for Idiomatic Java
|
||||||
|
*
|
||||||
|
* Copyright (c) 2010, 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.naturalid.immutable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Alex Burgel
|
||||||
|
*/
|
||||||
|
public class Child {
|
||||||
|
|
||||||
|
private Long id;
|
||||||
|
private Parent parent;
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
Child() {}
|
||||||
|
|
||||||
|
public Child(String name, Parent parent) {
|
||||||
|
this.name = name;
|
||||||
|
this.parent = parent;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,144 @@
|
||||||
|
/*
|
||||||
|
* Hibernate, Relational Persistence for Idiomatic Java
|
||||||
|
*
|
||||||
|
* Copyright (c) 2010, 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.naturalid.immutable;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
|
||||||
|
import junit.framework.Test;
|
||||||
|
import junit.framework.TestSuite;
|
||||||
|
|
||||||
|
import org.hibernate.FetchMode;
|
||||||
|
import org.hibernate.HibernateException;
|
||||||
|
import org.hibernate.Session;
|
||||||
|
import org.hibernate.Transaction;
|
||||||
|
import org.hibernate.cfg.Configuration;
|
||||||
|
import org.hibernate.cfg.Environment;
|
||||||
|
import org.hibernate.criterion.Restrictions;
|
||||||
|
import org.hibernate.junit.functional.FunctionalTestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Alex Burgel
|
||||||
|
*/
|
||||||
|
public class ImmutableEntityNaturalIdTest extends FunctionalTestCase {
|
||||||
|
|
||||||
|
public ImmutableEntityNaturalIdTest(String str) {
|
||||||
|
super(str);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testNaturalIdCheck() throws Exception {
|
||||||
|
Session s = openSession();
|
||||||
|
Transaction t = s.beginTransaction();
|
||||||
|
Parent p = new Parent("alex");
|
||||||
|
Child c = new Child("billy", p);
|
||||||
|
s.persist(p);
|
||||||
|
s.persist(c);
|
||||||
|
t.commit();
|
||||||
|
s.close();
|
||||||
|
|
||||||
|
Field name = c.getClass().getDeclaredField("name");
|
||||||
|
name.setAccessible(true);
|
||||||
|
name.set(c, "phil");
|
||||||
|
|
||||||
|
s = openSession();
|
||||||
|
t = s.beginTransaction();
|
||||||
|
try {
|
||||||
|
s.saveOrUpdate( c );
|
||||||
|
s.flush();
|
||||||
|
fail( "should have failed because immutable natural ID was altered");
|
||||||
|
}
|
||||||
|
catch (HibernateException he) {
|
||||||
|
// expected
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
t.rollback();
|
||||||
|
s.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
name.set(c, "billy");
|
||||||
|
|
||||||
|
s = openSession();
|
||||||
|
t = s.beginTransaction();
|
||||||
|
s.delete(c);
|
||||||
|
s.delete(p);
|
||||||
|
t.commit();
|
||||||
|
s.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testSaveParentWithDetachedChildren() throws Exception {
|
||||||
|
Session s = openSession();
|
||||||
|
Transaction t = s.beginTransaction();
|
||||||
|
|
||||||
|
Parent p = new Parent("alex");
|
||||||
|
Child c = new Child("billy", p);
|
||||||
|
|
||||||
|
s.persist(p);
|
||||||
|
s.persist(c);
|
||||||
|
t.commit();
|
||||||
|
s.close();
|
||||||
|
|
||||||
|
s = openSession();
|
||||||
|
t = s.beginTransaction();
|
||||||
|
|
||||||
|
p = (Parent) s.createCriteria(Parent.class)
|
||||||
|
.add( Restrictions.eq("name", "alex") )
|
||||||
|
.setFetchMode("children", FetchMode.JOIN)
|
||||||
|
.setCacheable(true)
|
||||||
|
.uniqueResult();
|
||||||
|
|
||||||
|
t.commit();
|
||||||
|
s.close();
|
||||||
|
|
||||||
|
s = openSession();
|
||||||
|
t = s.beginTransaction();
|
||||||
|
|
||||||
|
Child c2 = new Child("joey", p);
|
||||||
|
p.getChildren().add(c2);
|
||||||
|
|
||||||
|
s.update(p);
|
||||||
|
|
||||||
|
// this fails if AbstractEntityPersister returns identifiers instead of entities from
|
||||||
|
// AbstractEntityPersister.getNaturalIdSnapshot()
|
||||||
|
s.flush();
|
||||||
|
|
||||||
|
s.delete(p);
|
||||||
|
t.commit();
|
||||||
|
s.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void configure(Configuration cfg) {
|
||||||
|
cfg.setProperty(Environment.USE_SECOND_LEVEL_CACHE, "true");
|
||||||
|
cfg.setProperty(Environment.USE_QUERY_CACHE, "true");
|
||||||
|
cfg.setProperty(Environment.GENERATE_STATISTICS, "true");
|
||||||
|
}
|
||||||
|
|
||||||
|
public String[] getMappings() {
|
||||||
|
return new String[] { "naturalid/immutable/ParentChildWithManyToOne.hbm.xml" };
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Test suite() {
|
||||||
|
return new TestSuite( ImmutableEntityNaturalIdTest.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,58 @@
|
||||||
|
/*
|
||||||
|
* Hibernate, Relational Persistence for Idiomatic Java
|
||||||
|
*
|
||||||
|
* Copyright (c) 2010, 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.naturalid.immutable;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Alex Burgel
|
||||||
|
*/
|
||||||
|
public class Parent {
|
||||||
|
|
||||||
|
private Long id;
|
||||||
|
private String name;
|
||||||
|
private List children = new ArrayList();
|
||||||
|
|
||||||
|
Parent() {}
|
||||||
|
|
||||||
|
public Parent(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List getChildren() {
|
||||||
|
return children;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setChildren(List children) {
|
||||||
|
this.children = children;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,41 @@
|
||||||
|
<?xml version="1.0"?>
|
||||||
|
<!DOCTYPE hibernate-mapping PUBLIC
|
||||||
|
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
|
||||||
|
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
|
||||||
|
|
||||||
|
<!--
|
||||||
|
|
||||||
|
This mapping illustrates use of <natural-id> with a many-to-one.
|
||||||
|
|
||||||
|
-->
|
||||||
|
|
||||||
|
<hibernate-mapping
|
||||||
|
package="org.hibernate.test.naturalid.immutable"
|
||||||
|
default-access="field">
|
||||||
|
|
||||||
|
<class name="Parent" table="Parent">
|
||||||
|
<id name="id">
|
||||||
|
<generator class="increment"/>
|
||||||
|
</id>
|
||||||
|
|
||||||
|
<property name="name"/>
|
||||||
|
|
||||||
|
<bag name="children" inverse="true" cascade="all">
|
||||||
|
<key column="parent" />
|
||||||
|
<one-to-many class="Child" />
|
||||||
|
</bag>
|
||||||
|
|
||||||
|
</class>
|
||||||
|
|
||||||
|
<class name="Child" table="Child">
|
||||||
|
<id name="id">
|
||||||
|
<generator class="increment"/>
|
||||||
|
</id>
|
||||||
|
|
||||||
|
<natural-id mutable="false">
|
||||||
|
<many-to-one name="parent" class="Parent" />
|
||||||
|
<property name="name"/>
|
||||||
|
</natural-id>
|
||||||
|
</class>
|
||||||
|
|
||||||
|
</hibernate-mapping>
|
Loading…
Reference in New Issue