[hibernate-commits] Hibernate SVN: r10882 - in branches/Branch_3_2/HibernateExt/metadata/src: java/org/hibernate/cfg/annotations test/org/hibernate/test/annotations/query

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Wed Nov 29 22:37:39 EST 2006


Author: epbernard
Date: 2006-11-29 22:37:37 -0500 (Wed, 29 Nov 2006)
New Revision: 10882

Added:
   branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/test/annotations/query/CasimirParticle.java
   branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/test/annotations/query/Chaos.java
   branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/test/annotations/query/QueryAndSQLTest.java
Removed:
   branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/test/annotations/query/QueryTest.java
Modified:
   branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/cfg/annotations/CollectionBinder.java
   branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/cfg/annotations/EntityBinder.java
Log:
ANN-37 SQL Overriding support

Modified: branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/cfg/annotations/CollectionBinder.java
===================================================================
--- branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/cfg/annotations/CollectionBinder.java	2006-11-29 15:23:36 UTC (rev 10881)
+++ branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/cfg/annotations/CollectionBinder.java	2006-11-30 03:37:37 UTC (rev 10882)
@@ -22,6 +22,7 @@
 import org.hibernate.AssertionFailure;
 import org.hibernate.FetchMode;
 import org.hibernate.MappingException;
+import org.hibernate.engine.ExecuteUpdateResultCheckStyle;
 import org.hibernate.annotations.BatchSize;
 import org.hibernate.annotations.Cache;
 import org.hibernate.annotations.CollectionId;
@@ -39,6 +40,11 @@
 import org.hibernate.annotations.SortType;
 import org.hibernate.annotations.Where;
 import org.hibernate.annotations.WhereJoinTable;
+import org.hibernate.annotations.SQLInsert;
+import org.hibernate.annotations.SQLUpdate;
+import org.hibernate.annotations.SQLDelete;
+import org.hibernate.annotations.SQLDeleteAll;
+import org.hibernate.annotations.Loader;
 import org.hibernate.cfg.AnnotatedClassType;
 import org.hibernate.cfg.AnnotationBinder;
 import org.hibernate.cfg.BinderHelper;
@@ -335,6 +341,37 @@
 			collection.setCacheRegionName( cacheRegionName );
 		}
 
+		//SQL overriding
+		SQLInsert sqlInsert = property.getAnnotation( SQLInsert.class );
+		SQLUpdate sqlUpdate = property.getAnnotation( SQLUpdate.class );
+		SQLDelete sqlDelete = property.getAnnotation( SQLDelete.class );
+		SQLDeleteAll sqlDeleteAll = property.getAnnotation( SQLDeleteAll.class );
+		Loader loader = property.getAnnotation( Loader.class );
+		if ( sqlInsert != null ) {
+			collection.setCustomSQLInsert( sqlInsert.sql().trim(), sqlInsert.callable(),
+					ExecuteUpdateResultCheckStyle.parse( sqlInsert.check().toString().toLowerCase() )
+			);
+
+		}
+		if ( sqlUpdate != null ) {
+			collection.setCustomSQLUpdate( sqlUpdate.sql(), sqlUpdate.callable(),
+					ExecuteUpdateResultCheckStyle.parse( sqlUpdate.check().toString().toLowerCase() )
+			);
+		}
+		if ( sqlDelete != null ) {
+			collection.setCustomSQLDelete( sqlDelete.sql(), sqlDelete.callable(),
+					ExecuteUpdateResultCheckStyle.parse( sqlDelete.check().toString().toLowerCase() )
+			);
+		}
+		if ( sqlDeleteAll != null ) {
+			collection.setCustomSQLDelete( sqlDeleteAll.sql(), sqlDeleteAll.callable(),
+					ExecuteUpdateResultCheckStyle.parse( sqlDeleteAll.check().toString().toLowerCase() )
+			);
+		}
+		if ( loader != null ) {
+			collection.setLoaderName( loader.namedQuery() );
+		}
+
 		//work on association
 		boolean isMappedBy = ! BinderHelper.isDefault( mappedBy );
 		collection.setInverse( isMappedBy );

Modified: branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/cfg/annotations/EntityBinder.java
===================================================================
--- branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/cfg/annotations/EntityBinder.java	2006-11-29 15:23:36 UTC (rev 10881)
+++ branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/cfg/annotations/EntityBinder.java	2006-11-30 03:37:37 UTC (rev 10882)
@@ -23,20 +23,26 @@
 import org.hibernate.annotations.BatchSize;
 import org.hibernate.annotations.Cache;
 import org.hibernate.annotations.CacheConcurrencyStrategy;
+import org.hibernate.annotations.ForceDiscriminator;
+import org.hibernate.annotations.Loader;
 import org.hibernate.annotations.OptimisticLockType;
 import org.hibernate.annotations.PolymorphismType;
 import org.hibernate.annotations.Proxy;
+import org.hibernate.annotations.SQLDelete;
+import org.hibernate.annotations.SQLInsert;
+import org.hibernate.annotations.SQLUpdate;
 import org.hibernate.annotations.Tables;
 import org.hibernate.annotations.Where;
-import org.hibernate.annotations.ForceDiscriminator;
+import org.hibernate.annotations.SQLDeleteAll;
 import org.hibernate.cache.CacheFactory;
 import org.hibernate.cfg.AnnotationBinder;
+import org.hibernate.cfg.BinderHelper;
 import org.hibernate.cfg.Ejb3JoinColumn;
 import org.hibernate.cfg.ExtendedMappings;
 import org.hibernate.cfg.InheritanceState;
 import org.hibernate.cfg.PropertyHolder;
-import org.hibernate.cfg.BinderHelper;
 import org.hibernate.engine.Versioning;
+import org.hibernate.engine.ExecuteUpdateResultCheckStyle;
 import org.hibernate.mapping.DependantValue;
 import org.hibernate.mapping.Join;
 import org.hibernate.mapping.PersistentClass;
@@ -157,6 +163,7 @@
 		}
 		persistentClass.setDynamicInsert( dynamicInsert );
 		persistentClass.setDynamicUpdate( dynamicUpdate );
+
 		if ( persistentClass instanceof RootClass ) {
 			RootClass rootClass = (RootClass) persistentClass;
 			rootClass.setMutable( mutable );
@@ -170,14 +177,14 @@
 			rootClass.setForceDiscriminator( annotatedClass.isAnnotationPresent( ForceDiscriminator.class ) );
 		}
 		else {
-			if (explicitHibernateEntityAnnotation) {
-				log.warn("@org.hibernate.annotations.Entity used on a non root entity: ignored for "
+			if ( explicitHibernateEntityAnnotation ) {
+				log.warn( "@org.hibernate.annotations.Entity used on a non root entity: ignored for "
 						+ annotatedClass.getName() );
 			}
 		}
 		persistentClass.setOptimisticLockMode( getVersioning( optimisticLockType ) );
 		persistentClass.setSelectBeforeUpdate( selectBeforeUpdate );
-		if ( ! BinderHelper.isDefault( persister ) ) {
+		if ( !BinderHelper.isDefault( persister ) ) {
 			try {
 				persistentClass.setEntityPersisterClass( ReflectHelper.classForName( persister ) );
 			}
@@ -187,7 +194,38 @@
 		}
 		persistentClass.setBatchSize( batchSize );
 
-		if ( ! inheritanceState.hasParents ) {
+		//SQL overriding
+		SQLInsert sqlInsert = annotatedClass.getAnnotation( SQLInsert.class );
+		SQLUpdate sqlUpdate = annotatedClass.getAnnotation( SQLUpdate.class );
+		SQLDelete sqlDelete = annotatedClass.getAnnotation( SQLDelete.class );
+		SQLDeleteAll sqlDeleteAll = annotatedClass.getAnnotation( SQLDeleteAll.class );
+		Loader loader = annotatedClass.getAnnotation( Loader.class );
+		if ( sqlInsert != null ) {
+			persistentClass.setCustomSQLInsert( sqlInsert.sql().trim(), sqlInsert.callable(),
+					ExecuteUpdateResultCheckStyle.parse( sqlInsert.check().toString().toLowerCase() )
+			);
+
+		}
+		if ( sqlUpdate != null ) {
+			persistentClass.setCustomSQLUpdate( sqlUpdate.sql(), sqlUpdate.callable(),
+					ExecuteUpdateResultCheckStyle.parse( sqlUpdate.check().toString().toLowerCase() )
+			);
+		}
+		if ( sqlDelete != null ) {
+			persistentClass.setCustomSQLDelete( sqlDelete.sql(), sqlDelete.callable(),
+					ExecuteUpdateResultCheckStyle.parse( sqlDelete.check().toString().toLowerCase() )
+			);
+		}
+		if ( sqlDeleteAll != null ) {
+			persistentClass.setCustomSQLDelete( sqlDeleteAll.sql(), sqlDeleteAll.callable(),
+					ExecuteUpdateResultCheckStyle.parse( sqlDeleteAll.check().toString().toLowerCase() )
+			);
+		}
+		if ( loader != null ) {
+			persistentClass.setLoaderName( loader.namedQuery() );
+		}
+
+		if ( !inheritanceState.hasParents ) {
 			Iterator<Map.Entry<String, String>> iter = filters.entrySet().iterator();
 			while ( iter.hasNext() ) {
 				Map.Entry<String, String> filter = iter.next();
@@ -213,7 +251,7 @@
 		try {
 			mappings.addImport( persistentClass.getEntityName(), name );
 			String entityName = persistentClass.getEntityName();
-			if ( ! entityName.equals( name ) ) {
+			if ( !entityName.equals( name ) ) {
 				mappings.addImport( entityName, entityName );
 			}
 		}
@@ -247,7 +285,7 @@
 	}
 
 	int getVersioning(OptimisticLockType type) {
-		switch ( type ) {
+		switch (type) {
 			case VERSION:
 				return Versioning.OPTIMISTIC_LOCK_VERSION;
 			case NONE:
@@ -262,7 +300,7 @@
 	}
 
 	private boolean isExplicitPolymorphism(PolymorphismType type) {
-		switch ( type ) {
+		switch (type) {
 			case IMPLICIT:
 				return false;
 			case EXPLICIT:
@@ -383,7 +421,9 @@
 			);
 		}
 		else {
-			int nbrOfJoinColumns = pkColumnsAnn != null ? pkColumnsAnn.length : joinColumnsAnn.length;
+			int nbrOfJoinColumns = pkColumnsAnn != null ?
+					pkColumnsAnn.length :
+					joinColumnsAnn.length;
 			if ( nbrOfJoinColumns == 0 ) {
 				ejb3JoinColumns = new Ejb3JoinColumn[1];
 				ejb3JoinColumns[0] = Ejb3JoinColumn.buildJoinColumn(
@@ -397,7 +437,7 @@
 			else {
 				ejb3JoinColumns = new Ejb3JoinColumn[nbrOfJoinColumns];
 				if ( pkColumnsAnn != null ) {
-					for ( int colIndex = 0; colIndex < nbrOfJoinColumns ; colIndex++ ) {
+					for ( int colIndex = 0; colIndex < nbrOfJoinColumns; colIndex++ ) {
 						ejb3JoinColumns[colIndex] = Ejb3JoinColumn.buildJoinColumn(
 								pkColumnsAnn[colIndex],
 								null,
@@ -408,7 +448,7 @@
 					}
 				}
 				else {
-					for ( int colIndex = 0; colIndex < nbrOfJoinColumns ; colIndex++ ) {
+					for ( int colIndex = 0; colIndex < nbrOfJoinColumns; colIndex++ ) {
 						ejb3JoinColumns[colIndex] = Ejb3JoinColumn.buildJoinColumn(
 								null,
 								joinColumnsAnn[colIndex],
@@ -493,7 +533,9 @@
 		else {
 			throw new AssertionFailure( "Both JoinTable and SecondaryTable are null" );
 		}
-		List uniqueConstraints = new ArrayList( uniqueCosntraintsAnn == null ? 0 : uniqueCosntraintsAnn.length );
+		List uniqueConstraints = new ArrayList( uniqueCosntraintsAnn == null ?
+				0 :
+				uniqueCosntraintsAnn.length );
 		if ( uniqueCosntraintsAnn != null && uniqueCosntraintsAnn.length != 0 ) {
 			for ( UniqueConstraint uc : uniqueCosntraintsAnn ) {
 				uniqueConstraints.add( uc.columnNames() );
@@ -541,7 +583,9 @@
 
 	public void setCache(Cache cacheAnn) {
 		if ( cacheAnn != null ) {
-			cacheRegion = BinderHelper.isDefault( cacheAnn.region() ) ? null : cacheAnn.region();
+			cacheRegion = BinderHelper.isDefault( cacheAnn.region() ) ?
+					null :
+					cacheAnn.region();
 			cacheConcurrentStrategy = getCacheConcurrencyStrategy( cacheAnn.usage() );
 			if ( "all".equalsIgnoreCase( cacheAnn.include() ) ) {
 				cacheLazyProperty = true;
@@ -561,7 +605,7 @@
 	}
 
 	public static String getCacheConcurrencyStrategy(CacheConcurrencyStrategy strategy) {
-		switch ( strategy ) {
+		switch (strategy) {
 			case NONE:
 				return null;
 			case READ_ONLY:

Added: branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/test/annotations/query/CasimirParticle.java
===================================================================
--- branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/test/annotations/query/CasimirParticle.java	2006-11-29 15:23:36 UTC (rev 10881)
+++ branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/test/annotations/query/CasimirParticle.java	2006-11-30 03:37:37 UTC (rev 10882)
@@ -0,0 +1,25 @@
+//$Id: $
+package org.hibernate.test.annotations.query;
+
+import javax.persistence.Entity;
+import javax.persistence.Table;
+import javax.persistence.Id;
+
+/**
+ * @author Emmanuel Bernard
+ */
+ at Entity
+ at Table(name="CASIMIR_PARTICULE")
+public class CasimirParticle {
+	@Id
+	private Long id;
+
+
+	public Long getId() {
+		return id;
+	}
+
+	public void setId(Long id) {
+		this.id = id;
+	}
+}

Added: branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/test/annotations/query/Chaos.java
===================================================================
--- branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/test/annotations/query/Chaos.java	2006-11-29 15:23:36 UTC (rev 10881)
+++ branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/test/annotations/query/Chaos.java	2006-11-30 03:37:37 UTC (rev 10882)
@@ -0,0 +1,83 @@
+//$Id: $
+package org.hibernate.test.annotations.query;
+
+import java.util.Set;
+import java.util.HashSet;
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.Table;
+import javax.persistence.NamedQuery;
+import javax.persistence.NamedNativeQuery;
+import javax.persistence.OneToMany;
+import javax.persistence.JoinColumn;
+
+import org.hibernate.annotations.SQLInsert;
+import org.hibernate.annotations.SQLUpdate;
+import org.hibernate.annotations.SQLDelete;
+import org.hibernate.annotations.SQLDeleteAll;
+import org.hibernate.annotations.Loader;
+
+/**
+ * @author Emmanuel Bernard
+ */
+ at Entity
+ at Table(name="CHAOS")
+ at SQLInsert( sql="INSERT INTO CHAOS(size, name, nickname, id) VALUES(?,upper(?),?,?)")
+ at SQLUpdate( sql="UPDATE CHAOS SET size = ?, name = upper(?), nickname = ? WHERE id = ?")
+ at SQLDelete( sql="DELETE CHAOS WHERE id = ?")
+ at SQLDeleteAll( sql="DELETE CHAOS")
+ at Loader(namedQuery = "chaos")
+ at NamedNativeQuery(name="chaos", query="select id, size, name, lower( nickname ) as nickname from CHAOS where id= ?", resultClass = Chaos.class)
+public class Chaos {
+	@Id
+	private Long id;
+	private Long size;
+	private String name;
+	private String nickname;
+
+	@OneToMany
+	@JoinColumn(name="chaos_fk")
+	@SQLInsert( sql="UPDATE CASIMIR_PARTICULE SET chaos_fk = ? where id = ?")
+	@SQLDelete( sql="UPDATE CASIMIR_PARTICULE SET chaos_fk = null where id = ?")
+	private Set<CasimirParticle> particles = new HashSet<CasimirParticle>();
+
+	public Long getId() {
+		return id;
+	}
+
+	public void setId(Long id) {
+		this.id = id;
+	}
+
+	public Long getSize() {
+		return size;
+	}
+
+	public void setSize(Long size) {
+		this.size = size;
+	}
+
+	public String getName() {
+		return name;
+	}
+
+	public void setName(String name) {
+		this.name = name;
+	}
+
+	public String getNickname() {
+		return nickname;
+	}
+
+	public void setNickname(String nickname) {
+		this.nickname = nickname;
+	}
+
+	public Set<CasimirParticle> getParticles() {
+		return particles;
+	}
+
+	public void setParticles(Set<CasimirParticle> particles) {
+		this.particles = particles;
+	}
+}

Copied: branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/test/annotations/query/QueryAndSQLTest.java (from rev 10867, branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/test/annotations/query/QueryTest.java)
===================================================================
--- branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/test/annotations/query/QueryTest.java	2006-11-24 00:37:46 UTC (rev 10867)
+++ branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/test/annotations/query/QueryAndSQLTest.java	2006-11-30 03:37:37 UTC (rev 10882)
@@ -0,0 +1,380 @@
+//$Id$
+package org.hibernate.test.annotations.query;
+
+import java.util.Calendar;
+import java.util.Date;
+import java.util.GregorianCalendar;
+import java.util.List;
+
+import org.hibernate.Query;
+import org.hibernate.Session;
+import org.hibernate.Transaction;
+import org.hibernate.stat.Statistics;
+import org.hibernate.test.annotations.A320;
+import org.hibernate.test.annotations.A320b;
+import org.hibernate.test.annotations.Plane;
+import org.hibernate.test.annotations.TestCase;
+
+/**
+ * Test named queries
+ *
+ * @author Emmanuel Bernard
+ */
+public class QueryAndSQLTest extends TestCase {
+	public QueryAndSQLTest(String x) {
+		super( x );
+	}
+
+	public void testPackageQueries() throws Exception {
+		Session s = openSession();
+		Transaction tx = s.beginTransaction();
+		Plane p = new Plane();
+		s.persist( p );
+		Query q = s.getNamedQuery( "plane.getAll" );
+		assertEquals( 1, q.list().size() );
+		tx.commit();
+		s.close();
+	}
+
+	public void testClassQueries() throws Exception {
+		Session s = openSession();
+		Transaction tx = s.beginTransaction();
+		Night n = new Night();
+		Calendar c = new GregorianCalendar();
+		c.set( 2000, 2, 2 );
+		Date now = c.getTime();
+		c.add( Calendar.MONTH, -1 );
+		Date aMonthAgo = c.getTime();
+		c.add( Calendar.MONTH, 2 );
+		Date inAMonth = c.getTime();
+		n.setDate( now );
+		n.setDuration( 14 );
+		s.persist( n );
+		tx.commit();
+		s.close();
+		s = openSession();
+		tx = s.beginTransaction();
+		Query q = s.getNamedQuery( "night.moreRecentThan" );
+		q.setDate( "date", aMonthAgo );
+		assertEquals( 1, q.list().size() );
+		q = s.getNamedQuery( "night.moreRecentThan" );
+		q.setDate( "date", inAMonth );
+		assertEquals( 0, q.list().size() );
+		Statistics stats = getSessions().getStatistics();
+		stats.setStatisticsEnabled( true );
+		stats.clear();
+		q = s.getNamedQuery( "night.duration" );
+		q.setParameter( "duration", 14l );
+		assertEquals( 1, q.list().size() );
+		assertEquals( 1, stats.getQueryCachePutCount() );
+		q = s.getNamedQuery( "night.duration" );
+		q.setParameter( "duration", 14l );
+		s.delete( q.list().get( 0 ) );
+		assertEquals( 1, stats.getQueryCacheHitCount() );
+		tx.commit();
+		s.close();
+	}
+
+	public void testSQLQuery() {
+		Night n = new Night();
+		Calendar c = new GregorianCalendar();
+		c.set( 2000, 2, 2 );
+		Date now = c.getTime();
+		c.add( Calendar.MONTH, -1 );
+		Date aMonthAgo = c.getTime();
+		c.add( Calendar.MONTH, 2 );
+		Date inAMonth = c.getTime();
+		n.setDate( now );
+		n.setDuration( 9999 );
+		Area area = new Area();
+		area.setName( "Monceau" );
+
+		Session s = openSession();
+		Transaction tx = s.beginTransaction();
+		s.persist( n );
+		s.persist( area );
+		tx.commit();
+		s.clear();
+		tx = s.beginTransaction();
+		Query q = s.getNamedQuery( "night.getAll.bySQL" );
+		q.setParameter( 0, 9990 );
+		List result = q.list();
+		assertEquals( 1, result.size() );
+		Night n2 = (Night) result.get( 0 );
+		assertEquals( n2.getDuration(), n.getDuration() );
+		List areas = s.getNamedQuery( "getAreaByNative" ).list();
+		assertTrue( 1 == areas.size() );
+		assertEquals( area.getName(), ( (Area) areas.get( 0 ) ).getName() );
+		tx.commit();
+		s.close();
+	}
+
+	public void testSQLQueryWithManyToOne() {
+		Night n = new Night();
+		Calendar c = new GregorianCalendar();
+		c.set( 2000, 2, 2 );
+		Date now = c.getTime();
+		c.add( Calendar.MONTH, -1 );
+		Date aMonthAgo = c.getTime();
+		c.add( Calendar.MONTH, 2 );
+		Date inAMonth = c.getTime();
+		n.setDate( now );
+		n.setDuration( 9999 );
+		Area a = new Area();
+		a.setName( "Paris" );
+		n.setArea( a );
+		Session s = openSession();
+		Transaction tx = s.beginTransaction();
+		s.persist( a );
+		s.persist( n );
+		tx.commit();
+		s.close();
+		s = openSession();
+		tx = s.beginTransaction();
+		Statistics stats = getSessions().getStatistics();
+		stats.setStatisticsEnabled( true );
+		Query q = s.getNamedQuery( "night&areaCached" );
+		List result = q.list();
+		assertEquals( 1, result.size() );
+		assertEquals( 1, stats.getQueryCachePutCount() );
+		q.list();
+		assertEquals( 1, stats.getQueryCacheHitCount() );
+		Night n2 = (Night) ( (Object[]) result.get( 0 ) )[0];
+		assertEquals( n2.getDuration(), n.getDuration() );
+		tx.commit();
+		s.close();
+	}
+
+	public void testImplicitNativeQuery() throws Exception {
+		Session s;
+		Transaction tx;
+		s = openSession();
+		tx = s.beginTransaction();
+		SpaceShip ship = new SpaceShip();
+		ship.setModel( "X-Wing" );
+		ship.setName( "YuBlue" );
+		ship.setSpeed( 2000 );
+		ship.setDimensions( new Dimensions() );
+		s.persist( ship );
+		tx.commit();
+		s.clear();
+		tx = s.beginTransaction();
+		Query q = s.getNamedQuery( "implicitSample" );
+		List result = q.list();
+		assertEquals( 1, result.size() );
+		assertEquals( ship.getModel(), ( (SpaceShip) result.get( 0 ) ).getModel() );
+		s.delete( result.get( 0 ) );
+		tx.commit();
+		s.close();
+	}
+
+	public void testNativeQueryAndCompositePKAndComponents() throws Exception {
+		Session s;
+		Transaction tx;
+		s = openSession();
+		tx = s.beginTransaction();
+		SpaceShip ship = new SpaceShip();
+		ship.setModel( "X-Wing" );
+		ship.setName( "YuBlue" );
+		ship.setSpeed( 2000 );
+		ship.setDimensions( new Dimensions() );
+		ship.getDimensions().setLength( 10 );
+		ship.getDimensions().setWidth( 5 );
+		Captain captain = new Captain();
+		captain.setFirstname( "Luke" );
+		captain.setLastname( "Skywalker" );
+		ship.setCaptain( captain );
+		s.persist( captain );
+		s.persist( ship );
+		tx.commit();
+		s.clear();
+		tx = s.beginTransaction();
+		Query q = s.getNamedQuery( "compositekey" );
+		List result = q.list();
+		assertEquals( 1, result.size() );
+		Object[] row = (Object[]) result.get( 0 );
+		SpaceShip spaceShip = (SpaceShip) row[0];
+		assertEquals( ship.getModel(), spaceShip.getModel() );
+		assertNotNull( spaceShip.getDimensions() );
+		assertEquals( ship.getDimensions().getWidth(), spaceShip.getDimensions().getWidth() );
+		assertEquals( ship.getDimensions().getLength(), spaceShip.getDimensions().getLength() );
+		assertEquals( ship.getCaptain().getFirstname(), ship.getCaptain().getFirstname() );
+		assertEquals( ship.getCaptain().getLastname(), ship.getCaptain().getLastname() );
+		//FIXME vary depending on databases
+		assertTrue( row[1].toString().startsWith( "50" ) );
+		assertTrue( row[2].toString().startsWith( "500" ) );
+		s.delete( spaceShip.getCaptain() );
+		s.delete( spaceShip );
+		tx.commit();
+		s.close();
+	}
+
+	public void testDiscriminator() throws Exception {
+		Session s;
+		Transaction tx;
+		s = openSession();
+		tx = s.beginTransaction();
+		Dictionary dic = new Dictionary();
+		dic.setName( "Anglais-Francais" );
+		dic.setEditor( "Harrap's" );
+		SynonymousDictionary syn = new SynonymousDictionary();
+		syn.setName( "Synonymes de tous les temps" );
+		syn.setEditor( "Imagination edition" );
+		s.persist( dic );
+		s.persist( syn );
+		tx.commit();
+		s.clear();
+		tx = s.beginTransaction();
+		List results = s.getNamedQuery( "all.dictionaries" ).list();
+		assertEquals( 2, results.size() );
+		assertTrue(
+				results.get( 0 ) instanceof SynonymousDictionary
+						|| results.get( 1 ) instanceof SynonymousDictionary
+		);
+		tx.commit();
+		s.close();
+	}
+
+//	public void testScalarQuery() throws Exception {
+//        Session s = openSession();
+//		Transaction tx;
+//		tx = s.beginTransaction();
+//		Mark bad = new Mark();
+//		bad.value = 5;
+//		Mark good = new Mark();
+//		good.value = 15;
+//		s.persist(bad);
+//		s.persist(good);
+//		tx.commit();
+//		s.clear();
+//		tx = s.beginTransaction();
+//		List result = s.getNamedQuery("average").list();
+//		assertEquals( 1, result.size() );
+//		tx.commit();
+//		s.close();
+//
+//	}
+
+	public void testCache() throws Exception {
+		Session s;
+		Transaction tx;
+		s = openSession();
+		tx = s.beginTransaction();
+		Plane plane = new Plane();
+		plane.setNbrOfSeats( 5 );
+		s.persist( plane );
+		tx.commit();
+		s.close();
+		getSessions().getStatistics().clear();
+		getSessions().getStatistics().setStatisticsEnabled( true );
+		s = openSession();
+		tx = s.beginTransaction();
+		Query query = s.getNamedQuery( "plane.byId" ).setParameter( "id", plane.getId() );
+		plane = (Plane) query.uniqueResult();
+		assertEquals( 1, getSessions().getStatistics().getQueryCachePutCount() );
+		plane = (Plane) s.getNamedQuery( "plane.byId" ).setParameter( "id", plane.getId() ).uniqueResult();
+		assertEquals( 1, getSessions().getStatistics().getQueryCacheHitCount() );
+		tx.commit();
+		s.close();
+
+		s = openSession();
+		tx = s.beginTransaction();
+		s.delete( s.get( Plane.class, plane.getId() ) );
+		tx.commit();
+		s.close();
+	}
+
+	public void testEntitySQLOverriding() {
+		Session s;
+		Transaction tx;
+		s = openSession();
+		tx = s.beginTransaction();
+		Chaos chaos = new Chaos();
+		chaos.setSize( 123l );
+		chaos.setId( 1l );
+
+		String lowerName = "hello";
+		String upperName = lowerName.toUpperCase();
+		assertFalse( lowerName.equals( upperName ) );
+
+		chaos.setName( "hello" );
+		chaos.setNickname( "NickName" );
+		s.persist( chaos );
+		s.flush();
+		s.clear();
+		s.getSessionFactory().evict( Chaos.class );
+
+		Chaos resultChaos = (Chaos) s.load( Chaos.class, chaos.getId() );
+		assertEquals( upperName, resultChaos.getName() );
+		assertEquals( "nickname", resultChaos.getNickname() );
+
+		tx.rollback();
+		s.close();
+	}
+
+	public void testCollectionSQLOverriding() {
+		Session s;
+		Transaction tx;
+		s = openSession();
+		tx = s.beginTransaction();
+		Chaos chaos = new Chaos();
+		chaos.setSize( 123l );
+		chaos.setId( 1l );
+
+		chaos.setName( "hello" );
+		s.persist( chaos );
+		CasimirParticle p = new CasimirParticle();
+		p.setId( 1l );
+		s.persist( p );
+		chaos.getParticles().add(p);
+		p = new CasimirParticle();
+		p.setId( 2l );
+		s.persist( p );
+		chaos.getParticles().add(p);
+		s.flush();
+		s.clear();
+		s.getSessionFactory().evict( Chaos.class );
+
+		Chaos resultChaos = (Chaos) s.load( Chaos.class, chaos.getId() );
+		assertEquals( 2, resultChaos.getParticles().size() );
+		resultChaos.getParticles().remove( resultChaos.getParticles().iterator().next() );
+		resultChaos.getParticles().remove( resultChaos.getParticles().iterator().next() );
+		s.flush();
+
+		s.clear();
+		resultChaos = (Chaos) s.load( Chaos.class, chaos.getId() );
+		assertEquals( 0, resultChaos.getParticles().size() );
+
+		tx.rollback();
+		s.close();
+	}
+
+	protected Class[] getMappings() {
+		return new Class[]{
+				Plane.class,
+				A320.class,
+				A320b.class,
+				Night.class,
+				Area.class,
+				SpaceShip.class,
+				Dictionary.class,
+				SynonymousDictionary.class,
+				Captain.class,
+				Chaos.class,
+				CasimirParticle.class
+		};
+	}
+
+	protected String[] getAnnotatedPackages() {
+		return new String[]{
+				"org.hibernate.test.annotations.query"
+		};
+	}
+
+	@Override
+	protected String[] getXmlFiles() {
+		return new String[]{
+				"org/hibernate/test/annotations/query/orm.xml"
+		};
+	}
+}


Property changes on: branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/test/annotations/query/QueryAndSQLTest.java
___________________________________________________________________
Name: svn:keywords
   + Author Date Id Revision
Name: svn:eol-style
   + native

Deleted: branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/test/annotations/query/QueryTest.java
===================================================================
--- branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/test/annotations/query/QueryTest.java	2006-11-29 15:23:36 UTC (rev 10881)
+++ branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/test/annotations/query/QueryTest.java	2006-11-30 03:37:37 UTC (rev 10882)
@@ -1,313 +0,0 @@
-//$Id$
-package org.hibernate.test.annotations.query;
-
-import java.util.Calendar;
-import java.util.Date;
-import java.util.GregorianCalendar;
-import java.util.List;
-
-import org.hibernate.Query;
-import org.hibernate.Session;
-import org.hibernate.Transaction;
-import org.hibernate.stat.Statistics;
-import org.hibernate.test.annotations.A320;
-import org.hibernate.test.annotations.A320b;
-import org.hibernate.test.annotations.Plane;
-import org.hibernate.test.annotations.TestCase;
-
-/**
- * Test named queries
- *
- * @author Emmanuel Bernard
- */
-public class QueryTest extends TestCase {
-	public QueryTest(String x) {
-		super( x );
-	}
-
-	public void testPackageQueries() throws Exception {
-		Session s = openSession();
-		Transaction tx = s.beginTransaction();
-		Plane p = new Plane();
-		s.persist( p );
-		Query q = s.getNamedQuery( "plane.getAll" );
-		assertEquals( 1, q.list().size() );
-		tx.commit();
-		s.close();
-	}
-
-	public void testClassQueries() throws Exception {
-		Session s = openSession();
-		Transaction tx = s.beginTransaction();
-		Night n = new Night();
-		Calendar c = new GregorianCalendar();
-		c.set( 2000, 2, 2 );
-		Date now = c.getTime();
-		c.add( Calendar.MONTH, -1 );
-		Date aMonthAgo = c.getTime();
-		c.add( Calendar.MONTH, 2 );
-		Date inAMonth = c.getTime();
-		n.setDate( now );
-		n.setDuration( 14 );
-		s.persist( n );
-		tx.commit();
-		s.close();
-		s = openSession();
-		tx = s.beginTransaction();
-		Query q = s.getNamedQuery( "night.moreRecentThan" );
-		q.setDate( "date", aMonthAgo );
-		assertEquals( 1, q.list().size() );
-		q = s.getNamedQuery( "night.moreRecentThan" );
-		q.setDate( "date", inAMonth );
-		assertEquals( 0, q.list().size() );
-		Statistics stats = getSessions().getStatistics();
-		stats.setStatisticsEnabled( true );
-		stats.clear();
-		q = s.getNamedQuery( "night.duration" );
-		q.setParameter( "duration", 14l );
-		assertEquals( 1, q.list().size() );
-		assertEquals( 1, stats.getQueryCachePutCount() );
-		q = s.getNamedQuery( "night.duration" );
-		q.setParameter( "duration", 14l );
-		s.delete( q.list().get( 0 ) );
-		assertEquals( 1, stats.getQueryCacheHitCount() );
-		tx.commit();
-		s.close();
-	}
-
-	public void testSQLQuery() {
-		Night n = new Night();
-		Calendar c = new GregorianCalendar();
-		c.set( 2000, 2, 2 );
-		Date now = c.getTime();
-		c.add( Calendar.MONTH, -1 );
-		Date aMonthAgo = c.getTime();
-		c.add( Calendar.MONTH, 2 );
-		Date inAMonth = c.getTime();
-		n.setDate( now );
-		n.setDuration( 9999 );
-		Area area = new Area();
-		area.setName( "Monceau" );
-
-		Session s = openSession();
-		Transaction tx = s.beginTransaction();
-		s.persist( n );
-		s.persist( area );
-		tx.commit();
-		s.clear();
-		tx = s.beginTransaction();
-		Query q = s.getNamedQuery( "night.getAll.bySQL" );
-		q.setParameter( 0, 9990 );
-		List result = q.list();
-		assertEquals( 1, result.size() );
-		Night n2 = (Night) result.get( 0 );
-		assertEquals( n2.getDuration(), n.getDuration() );
-		List areas = s.getNamedQuery( "getAreaByNative" ).list();
-		assertTrue( 1 == areas.size() );
-		assertEquals( area.getName(), ( (Area) areas.get( 0 ) ).getName() );
-		tx.commit();
-		s.close();
-	}
-
-	public void testSQLQueryWithManyToOne() {
-		Night n = new Night();
-		Calendar c = new GregorianCalendar();
-		c.set( 2000, 2, 2 );
-		Date now = c.getTime();
-		c.add( Calendar.MONTH, -1 );
-		Date aMonthAgo = c.getTime();
-		c.add( Calendar.MONTH, 2 );
-		Date inAMonth = c.getTime();
-		n.setDate( now );
-		n.setDuration( 9999 );
-		Area a = new Area();
-		a.setName( "Paris" );
-		n.setArea( a );
-		Session s = openSession();
-		Transaction tx = s.beginTransaction();
-		s.persist( a );
-		s.persist( n );
-		tx.commit();
-		s.close();
-		s = openSession();
-		tx = s.beginTransaction();
-		Statistics stats = getSessions().getStatistics();
-		stats.setStatisticsEnabled( true );
-		Query q = s.getNamedQuery( "night&areaCached" );
-		List result = q.list();
-		assertEquals( 1, result.size() );
-		assertEquals( 1, stats.getQueryCachePutCount() );
-		q.list();
-		assertEquals( 1, stats.getQueryCacheHitCount() );
-		Night n2 = (Night) ( (Object[]) result.get( 0 ) )[0];
-		assertEquals( n2.getDuration(), n.getDuration() );
-		tx.commit();
-		s.close();
-	}
-
-	public void testImplicitNativeQuery() throws Exception {
-		Session s;
-		Transaction tx;
-		s = openSession();
-		tx = s.beginTransaction();
-		SpaceShip ship = new SpaceShip();
-		ship.setModel( "X-Wing" );
-		ship.setName( "YuBlue" );
-		ship.setSpeed( 2000 );
-		ship.setDimensions( new Dimensions() );
-		s.persist( ship );
-		tx.commit();
-		s.clear();
-		tx = s.beginTransaction();
-		Query q = s.getNamedQuery( "implicitSample" );
-		List result = q.list();
-		assertEquals( 1, result.size() );
-		assertEquals( ship.getModel(), ( (SpaceShip) result.get( 0 ) ).getModel() );
-		s.delete( result.get( 0 ) );
-		tx.commit();
-		s.close();
-	}
-
-	public void testNativeQueryAndCompositePKAndComponents() throws Exception {
-		Session s;
-		Transaction tx;
-		s = openSession();
-		tx = s.beginTransaction();
-		SpaceShip ship = new SpaceShip();
-		ship.setModel( "X-Wing" );
-		ship.setName( "YuBlue" );
-		ship.setSpeed( 2000 );
-		ship.setDimensions( new Dimensions() );
-		ship.getDimensions().setLength( 10 );
-		ship.getDimensions().setWidth( 5 );
-		Captain captain = new Captain();
-		captain.setFirstname( "Luke" );
-		captain.setLastname( "Skywalker" );
-		ship.setCaptain( captain );
-		s.persist( captain );
-		s.persist( ship );
-		tx.commit();
-		s.clear();
-		tx = s.beginTransaction();
-		Query q = s.getNamedQuery( "compositekey" );
-		List result = q.list();
-		assertEquals( 1, result.size() );
-		Object[] row = (Object[]) result.get( 0 );
-		SpaceShip spaceShip = (SpaceShip) row[0];
-		assertEquals( ship.getModel(), spaceShip.getModel() );
-		assertNotNull( spaceShip.getDimensions() );
-		assertEquals( ship.getDimensions().getWidth(), spaceShip.getDimensions().getWidth() );
-		assertEquals( ship.getDimensions().getLength(), spaceShip.getDimensions().getLength() );
-		assertEquals( ship.getCaptain().getFirstname(), ship.getCaptain().getFirstname() );
-		assertEquals( ship.getCaptain().getLastname(), ship.getCaptain().getLastname() );
-		//FIXME vary depending on databases
-		assertTrue( row[1].toString().startsWith( "50" ) );
-		assertTrue( row[2].toString().startsWith( "500" ) );
-		s.delete( spaceShip.getCaptain() );
-		s.delete( spaceShip );
-		tx.commit();
-		s.close();
-	}
-
-	public void testDiscriminator() throws Exception {
-		Session s;
-		Transaction tx;
-		s = openSession();
-		tx = s.beginTransaction();
-		Dictionary dic = new Dictionary();
-		dic.setName( "Anglais-Francais" );
-		dic.setEditor( "Harrap's" );
-		SynonymousDictionary syn = new SynonymousDictionary();
-		syn.setName( "Synonymes de tous les temps" );
-		syn.setEditor( "Imagination edition" );
-		s.persist( dic );
-		s.persist( syn );
-		tx.commit();
-		s.clear();
-		tx = s.beginTransaction();
-		List results = s.getNamedQuery( "all.dictionaries" ).list();
-		assertEquals( 2, results.size() );
-		assertTrue(
-				results.get( 0 ) instanceof SynonymousDictionary
-						|| results.get( 1 ) instanceof SynonymousDictionary
-		);
-		tx.commit();
-		s.close();
-	}
-
-//	public void testScalarQuery() throws Exception {
-//        Session s = openSession();
-//		Transaction tx;
-//		tx = s.beginTransaction();
-//		Mark bad = new Mark();
-//		bad.value = 5;
-//		Mark good = new Mark();
-//		good.value = 15;
-//		s.persist(bad);
-//		s.persist(good);
-//		tx.commit();
-//		s.clear();
-//		tx = s.beginTransaction();
-//		List result = s.getNamedQuery("average").list();
-//		assertEquals( 1, result.size() );
-//		tx.commit();
-//		s.close();
-//
-//	}
-
-	public void testCache() throws Exception {
-		Session s;
-		Transaction tx;
-		s = openSession();
-		tx = s.beginTransaction();
-		Plane plane = new Plane();
-		plane.setNbrOfSeats( 5 );
-		s.persist( plane );
-		tx.commit();
-		s.close();
-		getSessions().getStatistics().clear();
-		getSessions().getStatistics().setStatisticsEnabled( true );
-		s = openSession();
-		tx = s.beginTransaction();
-		Query query = s.getNamedQuery( "plane.byId" ).setParameter( "id", plane.getId() );
-		plane = (Plane) query.uniqueResult();
-		assertEquals( 1, getSessions().getStatistics().getQueryCachePutCount() );
-		plane = (Plane) s.getNamedQuery( "plane.byId" ).setParameter( "id", plane.getId() ).uniqueResult();
-		assertEquals( 1, getSessions().getStatistics().getQueryCacheHitCount() );
-		tx.commit();
-		s.close();
-
-		s = openSession();
-		tx = s.beginTransaction();
-		s.delete( s.get( Plane.class, plane.getId() ) );
-		tx.commit();
-		s.close();
-	}
-
-	protected Class[] getMappings() {
-		return new Class[]{
-				Plane.class,
-				A320.class,
-				A320b.class,
-				Night.class,
-				Area.class,
-				SpaceShip.class,
-				Dictionary.class,
-				SynonymousDictionary.class,
-				Captain.class
-		};
-	}
-
-	protected String[] getAnnotatedPackages() {
-		return new String[]{
-				"org.hibernate.test.annotations.query"
-		};
-	}
-
-	@Override
-	protected String[] getXmlFiles() {
-		return new String[] {
-				"org/hibernate/test/annotations/query/orm.xml"
-		};
-	}
-}




More information about the hibernate-commits mailing list