[hibernate-commits] Hibernate SVN: r14084 - in core/branches/Branch_3_2: src/org/hibernate/tuple/entity and 2 other directories.
hibernate-commits at lists.jboss.org
hibernate-commits at lists.jboss.org
Tue Oct 16 17:04:41 EDT 2007
Author: steve.ebersole at jboss.com
Date: 2007-10-16 17:04:41 -0400 (Tue, 16 Oct 2007)
New Revision: 14084
Modified:
core/branches/Branch_3_2/src/org/hibernate/loader/Loader.java
core/branches/Branch_3_2/src/org/hibernate/tuple/entity/EntityMetamodel.java
core/branches/Branch_3_2/test/org/hibernate/test/naturalid/immutable/ImmutableNaturalIdTest.java
core/branches/Branch_3_2/test/org/hibernate/test/naturalid/immutable/User.hbm.xml
core/branches/Branch_3_2/test/org/hibernate/test/naturalid/immutable/User.java
core/branches/Branch_3_2/test/org/hibernate/test/naturalid/mutable/MutableNaturalIdTest.java
core/branches/Branch_3_2/test/org/hibernate/test/naturalid/mutable/User.hbm.xml
Log:
HHH-2892 : natural id caching
Modified: core/branches/Branch_3_2/src/org/hibernate/loader/Loader.java
===================================================================
--- core/branches/Branch_3_2/src/org/hibernate/loader/Loader.java 2007-10-16 21:04:09 UTC (rev 14083)
+++ core/branches/Branch_3_2/src/org/hibernate/loader/Loader.java 2007-10-16 21:04:41 UTC (rev 14084)
@@ -2156,18 +2156,13 @@
final QueryCache queryCache,
final QueryKey key) {
List result = null;
-
- if ( session.getCacheMode().isGetEnabled() ) {
- result = queryCache.get(
- key,
- resultTypes,
- queryParameters.isNaturalKeyLookup(),
- querySpaces,
- session
- );
+ if ( session.getCacheMode().isGetEnabled() ) {
+ boolean isImmutableNaturalKeyLookup = queryParameters.isNaturalKeyLookup()
+ && getEntityPersisters()[0].getEntityMetamodel().hasImmutableNaturalId();
+ result = queryCache.get( key, resultTypes, isImmutableNaturalKeyLookup, querySpaces, session );
if ( factory.getStatistics().isStatisticsEnabled() ) {
- if (result==null) {
+ if ( result == null ) {
factory.getStatisticsImplementor()
.queryCacheMiss( getQueryIdentifier(), queryCache.getRegionName() );
}
@@ -2176,9 +2171,8 @@
.queryCacheHit( getQueryIdentifier(), queryCache.getRegionName() );
}
}
-
}
-
+
return result;
}
@@ -2189,13 +2183,12 @@
final QueryCache queryCache,
final QueryKey key,
final List result) {
-
if ( session.getCacheMode().isPutEnabled() ) {
boolean put = queryCache.put(
key,
resultTypes,
result,
- queryParameters.isNaturalKeyLookup(),
+ queryParameters.isNaturalKeyLookup(),
session
);
if ( put && factory.getStatistics().isStatisticsEnabled() ) {
Modified: core/branches/Branch_3_2/src/org/hibernate/tuple/entity/EntityMetamodel.java
===================================================================
--- core/branches/Branch_3_2/src/org/hibernate/tuple/entity/EntityMetamodel.java 2007-10-16 21:04:09 UTC (rev 14083)
+++ core/branches/Branch_3_2/src/org/hibernate/tuple/entity/EntityMetamodel.java 2007-10-16 21:04:41 UTC (rev 14084)
@@ -82,6 +82,7 @@
private final boolean hasNonIdentifierPropertyNamedId;
private final int[] naturalIdPropertyNumbers;
+ private final boolean hasImmutableNaturalId;
private boolean lazy; //not final because proxy factory creation can fail
private final boolean hasCascades;
@@ -159,6 +160,7 @@
boolean foundNonIdentifierPropertyNamedId = false;
boolean foundInsertGeneratedValue = false;
boolean foundUpdateGeneratedValue = false;
+ boolean foundUpdateableNaturalIdProperty = false;
while ( iter.hasNext() ) {
Property prop = ( Property ) iter.next();
@@ -173,6 +175,9 @@
if ( prop.isNaturalIdentifier() ) {
naturalIdNumbers.add( new Integer(i) );
+ if ( prop.isUpdateable() ) {
+ foundUpdateableNaturalIdProperty = true;
+ }
}
if ( "id".equals( prop.getName() ) ) {
@@ -229,9 +234,11 @@
if (naturalIdNumbers.size()==0) {
naturalIdPropertyNumbers = null;
+ hasImmutableNaturalId = false;
}
else {
naturalIdPropertyNumbers = ArrayHelper.toIntArray(naturalIdNumbers);
+ hasImmutableNaturalId = !foundUpdateableNaturalIdProperty;
}
hasInsertGeneratedValues = foundInsertGeneratedValue;
@@ -373,6 +380,10 @@
return naturalIdPropertyNumbers!=null;
}
+ public boolean hasImmutableNaturalId() {
+ return hasImmutableNaturalId;
+ }
+
public Set getSubclassEntityNames() {
return subclassEntityNames;
}
Modified: core/branches/Branch_3_2/test/org/hibernate/test/naturalid/immutable/ImmutableNaturalIdTest.java
===================================================================
--- core/branches/Branch_3_2/test/org/hibernate/test/naturalid/immutable/ImmutableNaturalIdTest.java 2007-10-16 21:04:09 UTC (rev 14083)
+++ core/branches/Branch_3_2/test/org/hibernate/test/naturalid/immutable/ImmutableNaturalIdTest.java 2007-10-16 21:04:41 UTC (rev 14084)
@@ -1,8 +1,30 @@
+/*
+ * Copyright (c) 2007, Red Hat Middleware, LLC. All rights reserved.
+ *
+ * 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, v. 2.1. This program is distributed in the
+ * hope that it will be useful, but WITHOUT A 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, v.2.1 along with this
+ * distribution; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * Red Hat Author(s): Steve Ebersole
+ */
package org.hibernate.test.naturalid.immutable;
+import java.lang.reflect.Field;
+
import junit.framework.Test;
import org.hibernate.Session;
+import org.hibernate.Transaction;
+import org.hibernate.HibernateException;
+import org.hibernate.cfg.Configuration;
+import org.hibernate.cfg.Environment;
+import org.hibernate.criterion.Restrictions;
import org.hibernate.junit.functional.FunctionalTestCase;
import org.hibernate.junit.functional.FunctionalTestClassTestSuite;
@@ -24,20 +46,23 @@
return new FunctionalTestClassTestSuite( ImmutableNaturalIdTest.class );
}
+ 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 void testUpdate() {
// prepare some test data...
Session session = openSession();
- session.beginTransaction();
- User user = new User();
- user.setUserName( "steve" );
- user.setEmail( "steve at hibernate.org" );
- user.setFirstName( "Steve" );
- user.setInitial( null);
- user.setLastName( "Ebersole" );
- user.setPassword( "brewhaha" );
+ session.beginTransaction();
+ User user = new User();
+ user.setUserName( "steve" );
+ user.setEmail( "steve at hibernate.org" );
+ user.setPassword( "brewhaha" );
session.save( user );
- session.getTransaction().commit();
- session.close();
+ session.getTransaction().commit();
+ session.close();
// 'user' is now a detached entity, so lets change a property and reattch...
user.setPassword( "homebrew" );
@@ -54,4 +79,82 @@
session.getTransaction().commit();
session.close();
}
+
+ public void testNaturalIdCheck() throws Exception {
+ Session s = openSession();
+ Transaction t = s.beginTransaction();
+
+ User u = new User( "steve", "superSecret" );
+ s.persist( u );
+ u.setUserName( "Steve" );
+ try {
+ s.flush();
+ fail();
+ }
+ catch ( HibernateException he ) {
+ }
+ u.setUserName( "steve" );
+ s.delete( u );
+ t.commit();
+ s.close();
+ }
+
+ public void testNaturalIdCache() {
+ Session s = openSession();
+ s.beginTransaction();
+ User u = new User( "steve", "superSecret" );
+ s.persist( u );
+ s.getTransaction().commit();
+ s.close();
+
+ getSessions().getStatistics().clear();
+
+ s = openSession();
+ s.beginTransaction();
+ u = ( User ) s.createCriteria( User.class )
+ .add( Restrictions.naturalId().set( "userName", "steve" ) )
+ .setCacheable( true )
+ .uniqueResult();
+ assertNotNull( u );
+ s.getTransaction().commit();
+ s.close();
+
+ assertEquals( getSessions().getStatistics().getQueryExecutionCount(), 1 );
+ assertEquals( getSessions().getStatistics().getQueryCacheHitCount(), 0 );
+ assertEquals( getSessions().getStatistics().getQueryCachePutCount(), 1 );
+
+ s = openSession();
+ s.beginTransaction();
+ User v = new User( "gavin", "supsup" );
+ s.persist( v );
+ s.getTransaction().commit();
+ s.close();
+
+ getSessions().getStatistics().clear();
+
+ s = openSession();
+ s.beginTransaction();
+ u = ( User ) s.createCriteria( User.class )
+ .add( Restrictions.naturalId().set( "userName", "steve" ) )
+ .setCacheable( true )
+ .uniqueResult();
+ assertNotNull( u );
+ assertEquals( getSessions().getStatistics().getQueryExecutionCount(), 0 );
+ assertEquals( getSessions().getStatistics().getQueryCacheHitCount(), 1 );
+ u = ( User ) s.createCriteria( User.class )
+ .add( Restrictions.naturalId().set( "userName", "steve" ) )
+ .setCacheable( true )
+ .uniqueResult();
+ assertNotNull( u );
+ assertEquals( getSessions().getStatistics().getQueryExecutionCount(), 0 );
+ assertEquals( getSessions().getStatistics().getQueryCacheHitCount(), 2 );
+ s.getTransaction().commit();
+ s.close();
+
+ s = openSession();
+ s.beginTransaction();
+ s.createQuery( "delete User" ).executeUpdate();
+ s.getTransaction().commit();
+ s.close();
+ }
}
Modified: core/branches/Branch_3_2/test/org/hibernate/test/naturalid/immutable/User.hbm.xml
===================================================================
--- core/branches/Branch_3_2/test/org/hibernate/test/naturalid/immutable/User.hbm.xml 2007-10-16 21:04:09 UTC (rev 14083)
+++ core/branches/Branch_3_2/test/org/hibernate/test/naturalid/immutable/User.hbm.xml 2007-10-16 21:04:41 UTC (rev 14084)
@@ -1,11 +1,32 @@
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
- "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
+ "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
+ "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
+<!--
+ ~ Copyright (c) 2007, Red Hat Middleware, LLC. All rights reserved.
+ ~
+ ~ 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, v. 2.1. This program is distributed in the
+ ~ hope that it will be useful, but WITHOUT A 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, v.2.1 along with this
+ ~ distribution; if not, write to the Free Software Foundation, Inc.,
+ ~ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ ~
+ ~ Red Hat Author(s): Steve Ebersole
+ -->
+
+<!--
+
+ This mapping illustrates use of <natural-id mutable="false"/>
+
+-->
<hibernate-mapping package="org.hibernate.test.naturalid.immutable">
- <class name="User" table="IMM_NAT_ID_USER" lazy="true">
+ <class name="User" table="IMM_NAT_ID_USER" lazy="true">
<comment>Users may bid for or sell auction items.</comment>
<id name="myUserId" type="java.lang.Integer">
<generator class="increment"/>
@@ -16,9 +37,6 @@
<version name="version"/>
<property name="password" not-null="true" length="15" column="`password`"/>
<property name="email"/>
- <property name="firstName" length="50" not-null="true"/>
- <property name="initial" column="`initial`"/>
- <property name="lastName" length="50" not-null="true"/>
</class>
</hibernate-mapping>
\ No newline at end of file
Modified: core/branches/Branch_3_2/test/org/hibernate/test/naturalid/immutable/User.java
===================================================================
--- core/branches/Branch_3_2/test/org/hibernate/test/naturalid/immutable/User.java 2007-10-16 21:04:09 UTC (rev 14083)
+++ core/branches/Branch_3_2/test/org/hibernate/test/naturalid/immutable/User.java 2007-10-16 21:04:41 UTC (rev 14084)
@@ -12,13 +12,15 @@
private String userName;
private String password;
private String email;
- private String firstName;
- private Character initial;
- private String lastName;
public User() {
}
+ public User(String userName, String password) {
+ this.userName = userName;
+ this.password = password;
+ }
+
public Integer getMyUserId() {
return this.myUserId;
}
@@ -51,30 +53,6 @@
this.email = email;
}
- public String getFirstName() {
- return this.firstName;
- }
-
- public void setFirstName(String firstName) {
- this.firstName = firstName;
- }
-
- public Character getInitial() {
- return this.initial;
- }
-
- public void setInitial(Character initial) {
- this.initial = initial;
- }
-
- public String getLastName() {
- return this.lastName;
- }
-
- public void setLastName(String lastName) {
- this.lastName = lastName;
- }
-
public Integer getVersion() {
return this.version;
}
Modified: core/branches/Branch_3_2/test/org/hibernate/test/naturalid/mutable/MutableNaturalIdTest.java
===================================================================
--- core/branches/Branch_3_2/test/org/hibernate/test/naturalid/mutable/MutableNaturalIdTest.java 2007-10-16 21:04:09 UTC (rev 14083)
+++ core/branches/Branch_3_2/test/org/hibernate/test/naturalid/mutable/MutableNaturalIdTest.java 2007-10-16 21:04:41 UTC (rev 14084)
@@ -76,26 +76,6 @@
s.close();
}
- public void testNaturalIdCheck() throws Exception {
- Session s = openSession();
- Transaction t = s.beginTransaction();
-
- User u = new User("gavin", "hb", "secret");
- s.persist(u);
- Field name = u.getClass().getDeclaredField("name");
- name.setAccessible(true);
- name.set(u, "Gavin");
- try {
- s.flush();
- fail();
- }
- catch (HibernateException he) {}
- name.set(u, "gavin");
- s.delete(u);
- t.commit();
- s.close();
- }
-
public void testNonexistentNaturalIdCache() {
getSessions().getStatistics().clear();
@@ -248,12 +228,22 @@
.uniqueResult();
assertNotNull(u);
+ assertEquals( getSessions().getStatistics().getQueryExecutionCount(), 1 );
+ assertEquals( getSessions().getStatistics().getQueryCacheHitCount(), 0 );
+
+ u = (User) s.createCriteria( User.class)
+ .add( Restrictions.naturalId()
+ .set("name", "gavin")
+ .set("org", "hb")
+ ).setCacheable(true)
+ .uniqueResult();
+
+ assertNotNull(u);
+ assertEquals( getSessions().getStatistics().getQueryExecutionCount(), 1 );
+ assertEquals( getSessions().getStatistics().getQueryCacheHitCount(), 1 );
t.commit();
s.close();
-
- assertEquals( getSessions().getStatistics().getQueryExecutionCount(), 0 );
- assertEquals( getSessions().getStatistics().getQueryCacheHitCount(), 1 );
s = openSession();
t = s.beginTransaction();
Modified: core/branches/Branch_3_2/test/org/hibernate/test/naturalid/mutable/User.hbm.xml
===================================================================
--- core/branches/Branch_3_2/test/org/hibernate/test/naturalid/mutable/User.hbm.xml 2007-10-16 21:04:09 UTC (rev 14083)
+++ core/branches/Branch_3_2/test/org/hibernate/test/naturalid/mutable/User.hbm.xml 2007-10-16 21:04:41 UTC (rev 14084)
@@ -5,7 +5,7 @@
<!--
- This mapping illustrates use of <natural-id>.
+ This mapping illustrates use of <natural-id mutable="true"/>
-->
More information about the hibernate-commits
mailing list