[hibernate-commits] Hibernate SVN: r11202 - in branches/Branch_3_2/HibernateExt/annotations/src: java/org/hibernate/cfg/annotations and 1 other directories.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Tue Feb 13 19:15:38 EST 2007


Author: epbernard
Date: 2007-02-13 19:15:37 -0500 (Tue, 13 Feb 2007)
New Revision: 11202

Modified:
   branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/annotations/Table.java
   branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/cfg/annotations/EntityBinder.java
   branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/join/Cat.java
   branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/join/JoinTest.java
Log:
ANN-104 support for custom SQL on secondary tables

Modified: branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/annotations/Table.java
===================================================================
--- branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/annotations/Table.java	2007-02-13 22:48:55 UTC (rev 11201)
+++ branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/annotations/Table.java	2007-02-14 00:15:37 UTC (rev 11202)
@@ -43,17 +43,44 @@
 	 * sequential select for a secondary table defined on a subclass, which will be issued only if a row
 	 * turns out to represent an instance of the subclass. Inner joins will still be used to retrieve a
 	 * secondary defined by the class and its superclasses.
+	 *
+	 * <b>Only applies to secondary tables</b>
 	 */
 	FetchMode fetch() default FetchMode.JOIN;
 
 	/**
 	 * If enabled, Hibernate will not try to insert or update the properties defined by this join.
+	 *
+	 * <b>Only applies to secondary tables</b>
 	 */
 	boolean inverse() default false;
 
 	/**
 	 * If enabled, Hibernate will insert a row only if the properties defined by this join are non-null
 	 * and will always use an outer join to retrieve the properties.
+	 *
+	 * <b>Only applies to secondary tables</b>
 	 */
 	boolean optional() default true;
+
+	/**
+	 * Defines a custom SQL insert statement
+	 *
+	 * <b>Only applies to secondary tables</b>
+	 */
+	SQLInsert sqlInsert() default @SQLInsert(sql="");
+
+	/**
+	 * Defines a custom SQL update statement
+	 *
+	 * <b>Only applies to secondary tables</b>
+	 */
+	SQLUpdate sqlUpdate() default @SQLUpdate(sql="");
+
+	/**
+	 * Defines a custom SQL delete statement
+	 *
+	 * <b>Only applies to secondary tables</b>
+	 */
+	SQLDelete sqlDelete() default @SQLDelete(sql="");
 }

Modified: branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/cfg/annotations/EntityBinder.java
===================================================================
--- branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/cfg/annotations/EntityBinder.java	2007-02-13 22:48:55 UTC (rev 11201)
+++ branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/cfg/annotations/EntityBinder.java	2007-02-14 00:15:37 UTC (rev 11202)
@@ -625,6 +625,24 @@
 			join.setSequentialSelect( FetchMode.JOIN != matchingTable.fetch() );
 			join.setInverse( matchingTable.inverse() );
 			join.setOptional( matchingTable.optional() );
+			if ( ! BinderHelper.isDefault( matchingTable.sqlInsert().sql() ) ) {
+				join.setCustomSQLInsert( matchingTable.sqlInsert().sql().trim(),
+						matchingTable.sqlInsert().callable(),
+						ExecuteUpdateResultCheckStyle.parse( matchingTable.sqlInsert().check().toString().toLowerCase() )
+				);
+			}
+			if ( ! BinderHelper.isDefault( matchingTable.sqlUpdate().sql() ) ) {
+				join.setCustomSQLUpdate( matchingTable.sqlUpdate().sql().trim(), 
+						matchingTable.sqlUpdate().callable(),
+						ExecuteUpdateResultCheckStyle.parse( matchingTable.sqlUpdate().check().toString().toLowerCase() )
+				);
+			}
+			if ( ! BinderHelper.isDefault( matchingTable.sqlDelete().sql() ) ) {
+				join.setCustomSQLDelete( matchingTable.sqlDelete().sql().trim(),
+						matchingTable.sqlDelete().callable(),
+						ExecuteUpdateResultCheckStyle.parse( matchingTable.sqlDelete().check().toString().toLowerCase() )
+				);
+			}
 		}
 		else {
 			//default

Modified: branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/join/Cat.java
===================================================================
--- branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/join/Cat.java	2007-02-13 22:48:55 UTC (rev 11201)
+++ branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/join/Cat.java	2007-02-14 00:15:37 UTC (rev 11202)
@@ -15,6 +15,7 @@
 import org.hibernate.annotations.ForeignKey;
 import org.hibernate.annotations.Tables;
 import org.hibernate.annotations.FetchMode;
+import org.hibernate.annotations.SQLInsert;
 
 /**
  * @author Emmanuel Bernard
@@ -27,7 +28,8 @@
 @Tables( {
 	@Table(appliesTo = "Cat", indexes = @Index(name = "secondname",
 			columnNames = "secondName"), comment = "My cat table" ),
-	@Table(appliesTo = "Cat2", foreignKey = @ForeignKey(name="FK_CAT2_CAT"), fetch = FetchMode.SELECT)
+	@Table(appliesTo = "Cat2", foreignKey = @ForeignKey(name="FK_CAT2_CAT"), fetch = FetchMode.SELECT,
+			sqlInsert=@SQLInsert(sql="insert into Cat2(storyPart2, id) values(upper(?), ?)") )
 			} )
 public class Cat implements Serializable {
 

Modified: branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/join/JoinTest.java
===================================================================
--- branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/join/JoinTest.java	2007-02-13 22:48:55 UTC (rev 11201)
+++ branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/join/JoinTest.java	2007-02-14 00:15:37 UTC (rev 11202)
@@ -157,6 +157,24 @@
 		s.close();
 	}
 
+	public void testCustomSQL() throws Exception {
+		Cat cat = new Cat();
+		String storyPart2 = "My long story";
+		cat.setStoryPart2( storyPart2 );
+		Session s = openSession();
+		Transaction tx = s.beginTransaction();
+
+		s.persist( cat );
+		s.flush();
+		s.clear();
+
+		Cat c = (Cat) s.get( Cat.class, cat.getId() );
+		assertEquals( storyPart2.toUpperCase(), c.getStoryPart2() );
+
+		tx.rollback();
+		s.close();
+	}
+
 	/**
 	 * @see org.hibernate.test.annotations.TestCase#getMappings()
 	 */




More information about the hibernate-commits mailing list