[hibernate-commits] Hibernate SVN: r21114 - annotations/patches/annotations-3.4.0.GA_CP05_JBPAPP-10735/src/test/java/org/hibernate/test/annotations/querycache and 1 other directories.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Fri May 3 14:10:35 EDT 2013


Author: brmeyer
Date: 2013-05-03 14:10:35 -0400 (Fri, 03 May 2013)
New Revision: 21114

Added:
   annotations/patches/annotations-3.4.0.GA_CP05_JBPAPP-10735/src/test/java/org/hibernate/test/annotations/querycache/
   annotations/patches/annotations-3.4.0.GA_CP05_JBPAPP-10735/src/test/java/org/hibernate/test/annotations/querycache/CompositeKey.java
   annotations/patches/annotations-3.4.0.GA_CP05_JBPAPP-10735/src/test/java/org/hibernate/test/annotations/querycache/EntityWithCompositeKey.java
   annotations/patches/annotations-3.4.0.GA_CP05_JBPAPP-10735/src/test/java/org/hibernate/test/annotations/querycache/EntityWithStringCompositeKey.java
   annotations/patches/annotations-3.4.0.GA_CP05_JBPAPP-10735/src/test/java/org/hibernate/test/annotations/querycache/QueryCacheTest.java
   annotations/patches/annotations-3.4.0.GA_CP05_JBPAPP-10735/src/test/java/org/hibernate/test/annotations/querycache/StringCompositeKey.java
Modified:
   core/patches/hibernate-3.3.2.GA_CP05_JBPAPP-10735/core/src/main/java/org/hibernate/type/ComponentType.java
Log:
HHH-4457 Query with Composite Primary Key parameter crashes when query cache is on

Added: annotations/patches/annotations-3.4.0.GA_CP05_JBPAPP-10735/src/test/java/org/hibernate/test/annotations/querycache/CompositeKey.java
===================================================================
--- annotations/patches/annotations-3.4.0.GA_CP05_JBPAPP-10735/src/test/java/org/hibernate/test/annotations/querycache/CompositeKey.java	                        (rev 0)
+++ annotations/patches/annotations-3.4.0.GA_CP05_JBPAPP-10735/src/test/java/org/hibernate/test/annotations/querycache/CompositeKey.java	2013-05-03 18:10:35 UTC (rev 21114)
@@ -0,0 +1,49 @@
+package org.hibernate.test.annotations.querycache;
+
+import java.io.Serializable;
+
+import javax.persistence.Embeddable;
+
+ at Embeddable
+public class CompositeKey implements Serializable {
+
+	private static final long serialVersionUID = 7950910288405475131L;
+
+	public int a;
+
+	public int b;
+
+	public CompositeKey() {
+	}
+	
+	public CompositeKey(int a, int b) {
+		this.a = a;
+		this.b = b;
+	}
+
+	@Override
+	public int hashCode() {
+		final int prime = 31;
+		int result = 1;
+		result = prime * result + a;
+		result = prime * result + b;
+		return result;
+	}
+
+	@Override
+	public boolean equals(Object obj) {
+		if (this == obj)
+			return true;
+		if (obj == null)
+			return false;
+		if (getClass() != obj.getClass())
+			return false;
+		CompositeKey other = (CompositeKey) obj;
+		if (a != other.a)
+			return false;
+		if (b != other.b)
+			return false;
+		return true;
+	}
+
+}
\ No newline at end of file


Property changes on: annotations/patches/annotations-3.4.0.GA_CP05_JBPAPP-10735/src/test/java/org/hibernate/test/annotations/querycache/CompositeKey.java
___________________________________________________________________
Added: svn:mime-type
   + text/plain

Added: annotations/patches/annotations-3.4.0.GA_CP05_JBPAPP-10735/src/test/java/org/hibernate/test/annotations/querycache/EntityWithCompositeKey.java
===================================================================
--- annotations/patches/annotations-3.4.0.GA_CP05_JBPAPP-10735/src/test/java/org/hibernate/test/annotations/querycache/EntityWithCompositeKey.java	                        (rev 0)
+++ annotations/patches/annotations-3.4.0.GA_CP05_JBPAPP-10735/src/test/java/org/hibernate/test/annotations/querycache/EntityWithCompositeKey.java	2013-05-03 18:10:35 UTC (rev 21114)
@@ -0,0 +1,19 @@
+package org.hibernate.test.annotations.querycache;
+
+import javax.persistence.EmbeddedId;
+import javax.persistence.Entity;
+
+ at Entity
+public class EntityWithCompositeKey {
+
+	@EmbeddedId
+	public CompositeKey pk;
+
+	public EntityWithCompositeKey() {
+	}
+
+	public EntityWithCompositeKey(CompositeKey pk) {
+		this.pk = pk;
+	}
+
+}
\ No newline at end of file


Property changes on: annotations/patches/annotations-3.4.0.GA_CP05_JBPAPP-10735/src/test/java/org/hibernate/test/annotations/querycache/EntityWithCompositeKey.java
___________________________________________________________________
Added: svn:mime-type
   + text/plain

Added: annotations/patches/annotations-3.4.0.GA_CP05_JBPAPP-10735/src/test/java/org/hibernate/test/annotations/querycache/EntityWithStringCompositeKey.java
===================================================================
--- annotations/patches/annotations-3.4.0.GA_CP05_JBPAPP-10735/src/test/java/org/hibernate/test/annotations/querycache/EntityWithStringCompositeKey.java	                        (rev 0)
+++ annotations/patches/annotations-3.4.0.GA_CP05_JBPAPP-10735/src/test/java/org/hibernate/test/annotations/querycache/EntityWithStringCompositeKey.java	2013-05-03 18:10:35 UTC (rev 21114)
@@ -0,0 +1,23 @@
+package org.hibernate.test.annotations.querycache;
+
+import javax.persistence.EmbeddedId;
+import javax.persistence.Entity;
+
+import org.hibernate.annotations.Cache;
+import org.hibernate.annotations.CacheConcurrencyStrategy;
+
+ at Entity
+ at Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
+public class EntityWithStringCompositeKey {
+
+	private StringCompositeKey pk;
+
+	@EmbeddedId
+	public StringCompositeKey getPk() {
+		return pk;
+	}
+
+	public void setPk(StringCompositeKey pk) {
+		this.pk = pk;
+	}
+}


Property changes on: annotations/patches/annotations-3.4.0.GA_CP05_JBPAPP-10735/src/test/java/org/hibernate/test/annotations/querycache/EntityWithStringCompositeKey.java
___________________________________________________________________
Added: svn:mime-type
   + text/plain

Added: annotations/patches/annotations-3.4.0.GA_CP05_JBPAPP-10735/src/test/java/org/hibernate/test/annotations/querycache/QueryCacheTest.java
===================================================================
--- annotations/patches/annotations-3.4.0.GA_CP05_JBPAPP-10735/src/test/java/org/hibernate/test/annotations/querycache/QueryCacheTest.java	                        (rev 0)
+++ annotations/patches/annotations-3.4.0.GA_CP05_JBPAPP-10735/src/test/java/org/hibernate/test/annotations/querycache/QueryCacheTest.java	2013-05-03 18:10:35 UTC (rev 21114)
@@ -0,0 +1,69 @@
+package org.hibernate.test.annotations.querycache;
+
+import org.hibernate.Criteria;
+import org.hibernate.Query;
+import org.hibernate.Session;
+import org.hibernate.cfg.Configuration;
+import org.hibernate.cfg.Environment;
+import org.hibernate.criterion.Restrictions;
+import org.hibernate.test.annotations.TestCase;
+
+public class QueryCacheTest extends TestCase {
+
+	private static final CompositeKey PK = new CompositeKey(1, 2); 
+	
+	@Override
+	protected Class[] getAnnotatedClasses() {
+		return new Class[] { 
+				CompositeKey.class,
+				EntityWithCompositeKey.class,
+				StringCompositeKey.class,
+				EntityWithStringCompositeKey.class				
+		};
+	}
+
+	@Override
+	public void configure(Configuration cfg) {
+		super.configure( cfg );
+		cfg.setProperty( Environment.USE_QUERY_CACHE, "true" );
+		cfg.setProperty( Environment.CACHE_REGION_PREFIX, "foo" );
+		cfg.setProperty( Environment.USE_SECOND_LEVEL_CACHE, "true" );
+		cfg.setProperty( Environment.GENERATE_STATISTICS, "true" );
+	}
+
+//	@Override
+//	protected String getCacheConcurrencyStrategy() {
+//		return "nonstrict-read-write";
+//	}
+	
+//	@TestForIssue( jiraKey = "HHH-4459" )
+	public void testGetByCompositeId() {
+		Session s = openSession();
+		s.beginTransaction();
+		s.persist( new EntityWithCompositeKey( PK ) );
+		Query query = s.createQuery( "FROM EntityWithCompositeKey e WHERE e.pk = :pk" );
+		query.setCacheable( true );
+		query.setParameter( "pk", PK );
+		assertEquals(1, query.list().size( ));
+		s.getTransaction().rollback();
+		s.close();
+		
+		s = openSession();
+		s.beginTransaction();
+		EntityWithStringCompositeKey entity = new EntityWithStringCompositeKey();
+		StringCompositeKey key = new StringCompositeKey();
+		key.setAnalog( "foo1" );
+		key.setDevice( "foo2" );
+		key.setDeviceType( "foo3" );
+		key.setSubstation( "foo4" );
+		entity.setPk( key );
+		s.persist( entity );
+		Criteria c = s.createCriteria(
+				EntityWithStringCompositeKey.class ).add( Restrictions.eq( 
+						"pk", key ) );
+		c.setCacheable( true );
+		assertEquals( 1, c.list().size() );
+		s.getTransaction().rollback();
+		s.close();
+	}
+}


Property changes on: annotations/patches/annotations-3.4.0.GA_CP05_JBPAPP-10735/src/test/java/org/hibernate/test/annotations/querycache/QueryCacheTest.java
___________________________________________________________________
Added: svn:mime-type
   + text/plain

Added: annotations/patches/annotations-3.4.0.GA_CP05_JBPAPP-10735/src/test/java/org/hibernate/test/annotations/querycache/StringCompositeKey.java
===================================================================
--- annotations/patches/annotations-3.4.0.GA_CP05_JBPAPP-10735/src/test/java/org/hibernate/test/annotations/querycache/StringCompositeKey.java	                        (rev 0)
+++ annotations/patches/annotations-3.4.0.GA_CP05_JBPAPP-10735/src/test/java/org/hibernate/test/annotations/querycache/StringCompositeKey.java	2013-05-03 18:10:35 UTC (rev 21114)
@@ -0,0 +1,51 @@
+package org.hibernate.test.annotations.querycache;
+
+import java.io.Serializable;
+
+import javax.persistence.Embeddable;
+
+ at Embeddable
+public class StringCompositeKey implements Serializable {
+	
+    private static final long serialVersionUID = 1L;
+
+	private String substation;
+    
+    private String deviceType;
+    
+    private String device;
+    
+    public String getSubstation() {
+		return substation;
+	}
+
+	public void setSubstation(String substation) {
+		this.substation = substation;
+	}
+
+	public String getDeviceType() {
+		return deviceType;
+	}
+
+	public void setDeviceType(String deviceType) {
+		this.deviceType = deviceType;
+	}
+
+	public String getDevice() {
+		return device;
+	}
+
+	public void setDevice(String device) {
+		this.device = device;
+	}
+
+	public String getAnalog() {
+		return analog;
+	}
+
+	public void setAnalog(String analog) {
+		this.analog = analog;
+	}
+
+	private String analog;
+}


Property changes on: annotations/patches/annotations-3.4.0.GA_CP05_JBPAPP-10735/src/test/java/org/hibernate/test/annotations/querycache/StringCompositeKey.java
___________________________________________________________________
Added: svn:mime-type
   + text/plain

Modified: core/patches/hibernate-3.3.2.GA_CP05_JBPAPP-10735/core/src/main/java/org/hibernate/type/ComponentType.java
===================================================================
--- core/patches/hibernate-3.3.2.GA_CP05_JBPAPP-10735/core/src/main/java/org/hibernate/type/ComponentType.java	2013-05-03 17:54:43 UTC (rev 21113)
+++ core/patches/hibernate-3.3.2.GA_CP05_JBPAPP-10735/core/src/main/java/org/hibernate/type/ComponentType.java	2013-05-03 18:10:35 UTC (rev 21114)
@@ -373,7 +373,16 @@
 
 	public Object[] getPropertyValues(Object component, EntityMode entityMode)
 			throws HibernateException {
-		return tuplizerMapping.getTuplizer( entityMode ).getPropertyValues( component );
+		if (component instanceof Object[]) {
+			// A few calls to hashCode pass the property values already in an
+			// Object[] (ex: QueryKey hash codes for cached queries).
+			// It's easiest to just check for the condition here prior to
+			// trying reflection.
+			return (Object[]) component;
+		} else {
+			return tuplizerMapping.getTuplizer(entityMode).getPropertyValues(
+					component);
+		}
 	}
 
 	public void setPropertyValues(Object component, Object[] values, EntityMode entityMode)



More information about the hibernate-commits mailing list