[hibernate-commits] Hibernate SVN: r12665 - in trunk/HibernateExt/annotations/src: java/org/hibernate/cfg and 3 other directories.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Tue Jul 3 11:20:03 EDT 2007


Author: epbernard
Date: 2007-07-03 11:20:03 -0400 (Tue, 03 Jul 2007)
New Revision: 12665

Added:
   trunk/HibernateExt/annotations/src/java/org/hibernate/annotations/NaturalId.java
   trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/IndexOrUniqueKeySecondPass.java
   trunk/HibernateExt/annotations/src/test/org/hibernate/test/annotations/naturalid/
   trunk/HibernateExt/annotations/src/test/org/hibernate/test/annotations/naturalid/Citizen.java
   trunk/HibernateExt/annotations/src/test/org/hibernate/test/annotations/naturalid/NaturalIdTest.java
   trunk/HibernateExt/annotations/src/test/org/hibernate/test/annotations/naturalid/State.java
Removed:
   trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/IndexSecondPass.java
Modified:
   trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/AnnotationBinder.java
   trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/Ejb3Column.java
   trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/annotations/PropertyBinder.java
   trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/annotations/TableBinder.java
   trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/annotations/Version.java
Log:
ANN-122 add @NaturalId annotation (Nicol?\195?\161s Lichtmaier)

Added: trunk/HibernateExt/annotations/src/java/org/hibernate/annotations/NaturalId.java
===================================================================
--- trunk/HibernateExt/annotations/src/java/org/hibernate/annotations/NaturalId.java	                        (rev 0)
+++ trunk/HibernateExt/annotations/src/java/org/hibernate/annotations/NaturalId.java	2007-07-03 15:20:03 UTC (rev 12665)
@@ -0,0 +1,22 @@
+package org.hibernate.annotations;
+
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.METHOD;
+import java.lang.annotation.Retention;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+import java.lang.annotation.Target;
+
+
+/**
+ * This specifies that a property is part of the natural id of the entity.
+ *
+ * @author Nicol‡s Lichtmaier
+ */
+ at Target( { METHOD, FIELD } )
+ at Retention( RUNTIME )
+public @interface NaturalId {
+	/**
+	 * If this natural id component is mutable or not.
+	 */
+	boolean mutable() default false;
+}

Modified: trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/AnnotationBinder.java
===================================================================
--- trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/AnnotationBinder.java	2007-07-03 13:24:52 UTC (rev 12664)
+++ trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/AnnotationBinder.java	2007-07-03 15:20:03 UTC (rev 12665)
@@ -76,6 +76,7 @@
 import org.hibernate.annotations.LazyToOne;
 import org.hibernate.annotations.LazyToOneOption;
 import org.hibernate.annotations.MapKeyManyToMany;
+import org.hibernate.annotations.NaturalId;
 import org.hibernate.annotations.NotFound;
 import org.hibernate.annotations.NotFoundAction;
 import org.hibernate.annotations.OnDelete;
@@ -1597,6 +1598,20 @@
 				}
 			}
 		}
+
+		NaturalId naturalIdAnn = property.getAnnotation( NaturalId.class );
+		if(naturalIdAnn != null)
+		{
+			if(joinColumns != null) {
+				for ( Ejb3Column column : joinColumns ) {
+					column.addUniqueKey("_UniqueKey", inSecondPass);
+				}
+			} else {
+				for ( Ejb3Column column : columns ) {
+					column.addUniqueKey("_UniqueKey", inSecondPass);
+				}
+			}
+		}
 	}
 
 	//TODO move that to collection binder?
@@ -1880,7 +1895,7 @@
 		//binder.setCascade(cascadeStrategy);
 		if ( isIdentifierMapper ) {
 			binder.setInsertable( false );
-			binder.setInsertable( false );
+			binder.setUpdatable( false );
 		}
 		else {
 			binder.setInsertable( columns[0].isInsertable() );

Modified: trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/Ejb3Column.java
===================================================================
--- trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/Ejb3Column.java	2007-07-03 13:24:52 UTC (rev 12664)
+++ trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/Ejb3Column.java	2007-07-03 15:20:03 UTC (rev 12665)
@@ -431,7 +431,12 @@
 
 	public void addIndex(Index index, boolean inSecondPass) {
 		if ( index == null ) return;
-		IndexSecondPass secondPass = new IndexSecondPass( index.name(), this, mappings );
+		String indexName = index.name();
+		addIndex(indexName, inSecondPass);
+	}
+
+	void addIndex(String indexName, boolean inSecondPass) {
+		IndexOrUniqueKeySecondPass secondPass = new IndexOrUniqueKeySecondPass( indexName, this, mappings, false );
 		if (inSecondPass) {
 			secondPass.doSecondPass( mappings.getClasses() );
 		}
@@ -441,4 +446,16 @@
 			);
 		}
 	}
+
+	void addUniqueKey(String uniqueKeyName, boolean inSecondPass) {
+		IndexOrUniqueKeySecondPass secondPass = new IndexOrUniqueKeySecondPass( uniqueKeyName, this, mappings, true );
+		if (inSecondPass) {
+			secondPass.doSecondPass( mappings.getClasses() );
+		}
+		else {
+			mappings.addSecondPass(
+					secondPass
+			);
+		}
+	}
 }
\ No newline at end of file

Copied: trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/IndexOrUniqueKeySecondPass.java (from rev 11648, trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/IndexSecondPass.java)
===================================================================
--- trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/IndexOrUniqueKeySecondPass.java	                        (rev 0)
+++ trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/IndexOrUniqueKeySecondPass.java	2007-07-03 15:20:03 UTC (rev 12665)
@@ -0,0 +1,81 @@
+//$Id$
+package org.hibernate.cfg;
+
+import java.util.Map;
+
+import org.hibernate.AnnotationException;
+import org.hibernate.MappingException;
+import org.hibernate.mapping.Column;
+import org.hibernate.mapping.Table;
+
+/**
+ * @author Emmanuel Bernard
+ */
+public class IndexOrUniqueKeySecondPass implements SecondPass {
+	private Table table;
+	private final String indexName;
+	private final String[] columns;
+	private final ExtendedMappings mappings;
+	private final Ejb3Column column;
+	private final boolean unique;
+
+	/**
+	 * Build an index
+	 */
+	public IndexOrUniqueKeySecondPass(Table table, String indexName, String[] columns, ExtendedMappings mappings) {
+		this.table = table;
+		this.indexName = indexName;
+		this.columns = columns;
+		this.mappings = mappings;
+		this.column = null;
+		this.unique = false;
+	}
+
+	/**
+	 * Build an index
+	 */
+	public IndexOrUniqueKeySecondPass(String indexName, Ejb3Column column, ExtendedMappings mappings) {
+		this(indexName, column, mappings, false);
+	}
+
+	/**
+	 * Build an index if unique is false or a Unique Key if unique is true
+	 */
+	public IndexOrUniqueKeySecondPass(String indexName, Ejb3Column column,
+			ExtendedMappings mappings, boolean unique) {
+		this.indexName = indexName;
+		this.column = column;
+		this.columns = null;
+		this.mappings = mappings;
+		this.unique = unique;
+	}
+
+	public void doSecondPass(Map persistentClasses) throws MappingException {
+		if ( columns != null ) {
+			for ( String columnName : columns ) {
+				addConstraintToColumn( columnName );
+			}
+		}
+		if ( column != null ) {
+			this.table = column.getTable();
+			addConstraintToColumn( mappings.getLogicalColumnName( column.getName(), table ) );
+		}
+	}
+
+	private void addConstraintToColumn(String columnName) {
+		Column column = table.getColumn(
+				new Column(
+						mappings.getPhysicalColumnName( columnName, table )
+				)
+		);
+		if ( column == null ) {
+			throw new AnnotationException(
+					"@Index references a unknown column: " + columnName
+			);
+		}
+		if(unique)
+			table.getOrCreateUniqueKey( indexName ).addColumn( column );
+		else
+			table.getOrCreateIndex( indexName ).addColumn( column );
+	}
+}


Property changes on: trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/IndexOrUniqueKeySecondPass.java
___________________________________________________________________
Name: svn:keywords
   + Author Date Id Revision
Name: svn:eol-style
   + native

Deleted: trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/IndexSecondPass.java
===================================================================
--- trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/IndexSecondPass.java	2007-07-03 13:24:52 UTC (rev 12664)
+++ trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/IndexSecondPass.java	2007-07-03 15:20:03 UTC (rev 12665)
@@ -1,61 +0,0 @@
-//$Id$
-package org.hibernate.cfg;
-
-import java.util.Map;
-
-import org.hibernate.AnnotationException;
-import org.hibernate.MappingException;
-import org.hibernate.mapping.Column;
-import org.hibernate.mapping.Table;
-
-/**
- * @author Emmanuel Bernard
- */
-public class IndexSecondPass implements SecondPass {
-	private Table table;
-	private final String indexName;
-	private final String[] columns;
-	private final ExtendedMappings mappings;
-	private final Ejb3Column column;
-
-	public IndexSecondPass(Table table, String indexName, String[] columns, ExtendedMappings mappings) {
-		this.table = table;
-		this.indexName = indexName;
-		this.columns = columns;
-		this.mappings = mappings;
-		this.column = null;
-	}
-
-	public IndexSecondPass(String indexName, Ejb3Column column, ExtendedMappings mappings) {
-		this.indexName = indexName;
-		this.column = column;
-		this.columns = null;
-		this.mappings = mappings;
-	}
-
-	public void doSecondPass(Map persistentClasses) throws MappingException {
-		if ( columns != null ) {
-			for ( String columnName : columns ) {
-				addIndexToColumn( columnName );
-			}
-		}
-		if ( column != null ) {
-			this.table = column.getTable();
-			addIndexToColumn( mappings.getLogicalColumnName( column.getName(), table ) );
-		}
-	}
-
-	private void addIndexToColumn(String columnName) {
-		Column column = table.getColumn(
-				new Column(
-						mappings.getPhysicalColumnName( columnName, table )
-				)
-		);
-		if ( column == null ) {
-			throw new AnnotationException(
-					"@Index references a unknown column: " + columnName
-			);
-		}
-		table.getOrCreateIndex( indexName ).addColumn( column );
-	}
-}

Modified: trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/annotations/PropertyBinder.java
===================================================================
--- trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/annotations/PropertyBinder.java	2007-07-03 13:24:52 UTC (rev 12664)
+++ trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/annotations/PropertyBinder.java	2007-07-03 15:20:03 UTC (rev 12665)
@@ -10,6 +10,7 @@
 import org.hibernate.annotations.Generated;
 import org.hibernate.annotations.GenerationTime;
 import org.hibernate.annotations.OptimisticLock;
+import org.hibernate.annotations.NaturalId;
 import org.hibernate.cfg.Ejb3Column;
 import org.hibernate.cfg.ExtendedMappings;
 import org.hibernate.cfg.PropertyHolder;
@@ -153,6 +154,15 @@
 				prop.setGeneration( PropertyGeneration.parse( generated.toString().toLowerCase() ) );
 			}
 		}
+		NaturalId naturalId = property != null ?
+				property.getAnnotation( NaturalId.class ) :
+				null;
+		if ( naturalId != null ) {
+			if ( !naturalId.mutable() ) {
+				updatable = false;
+			}
+			prop.setNaturalIdentifier( true );
+		}
 		prop.setInsertable( insertable );
 		prop.setUpdateable( updatable );
 		OptimisticLock lockAnn = property != null ?

Modified: trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/annotations/TableBinder.java
===================================================================
--- trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/annotations/TableBinder.java	2007-07-03 13:24:52 UTC (rev 12664)
+++ trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/annotations/TableBinder.java	2007-07-03 15:20:03 UTC (rev 12665)
@@ -15,7 +15,7 @@
 import org.hibernate.cfg.BinderHelper;
 import org.hibernate.cfg.Ejb3JoinColumn;
 import org.hibernate.cfg.ExtendedMappings;
-import org.hibernate.cfg.IndexSecondPass;
+import org.hibernate.cfg.IndexOrUniqueKeySecondPass;
 import org.hibernate.mapping.Collection;
 import org.hibernate.mapping.Column;
 import org.hibernate.mapping.DependantValue;
@@ -360,7 +360,7 @@
 		for ( Index index : indexes ) {
 			//no need to handle inSecondPass here since it is only called from EntityBinder
 			mappings.addSecondPass(
-					new IndexSecondPass( hibTable, index.name(), index.columnNames(), mappings )
+					new IndexOrUniqueKeySecondPass( hibTable, index.name(), index.columnNames(), mappings )
 			);
 		}
 	}

Modified: trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/annotations/Version.java
===================================================================
--- trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/annotations/Version.java	2007-07-03 13:24:52 UTC (rev 12664)
+++ trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/annotations/Version.java	2007-07-03 15:20:03 UTC (rev 12665)
@@ -8,7 +8,7 @@
  * @author Emmanuel Bernard
  */
 public class Version {
-	public static final String VERSION = "3.3.0.GA";
+	public static final String VERSION = "3.3.1.GA";
 	private static Log log = LogFactory.getLog( Version.class );
 
 	static {

Added: trunk/HibernateExt/annotations/src/test/org/hibernate/test/annotations/naturalid/Citizen.java
===================================================================
--- trunk/HibernateExt/annotations/src/test/org/hibernate/test/annotations/naturalid/Citizen.java	                        (rev 0)
+++ trunk/HibernateExt/annotations/src/test/org/hibernate/test/annotations/naturalid/Citizen.java	2007-07-03 15:20:03 UTC (rev 12665)
@@ -0,0 +1,67 @@
+//$Id$
+package org.hibernate.test.annotations.naturalid;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.ManyToOne;
+
+import org.hibernate.annotations.NaturalId;
+
+/**
+ * @author Emmanuel Bernard
+ */
+ at Entity
+public class Citizen {
+	@Id
+	@GeneratedValue
+	private Integer id;
+	private String firstname;
+	private String lastname;
+	@NaturalId
+	@ManyToOne
+	private State state;
+	@NaturalId
+	private String ssn;
+
+
+	public Integer getId() {
+		return id;
+	}
+
+	public void setId(Integer id) {
+		this.id = id;
+	}
+
+	public String getFirstname() {
+		return firstname;
+	}
+
+	public void setFirstname(String firstname) {
+		this.firstname = firstname;
+	}
+
+	public String getLastname() {
+		return lastname;
+	}
+
+	public void setLastname(String lastname) {
+		this.lastname = lastname;
+	}
+
+	public State getState() {
+		return state;
+	}
+
+	public void setState(State state) {
+		this.state = state;
+	}
+
+	public String getSsn() {
+		return ssn;
+	}
+
+	public void setSsn(String ssn) {
+		this.ssn = ssn;
+	}
+}

Added: trunk/HibernateExt/annotations/src/test/org/hibernate/test/annotations/naturalid/NaturalIdTest.java
===================================================================
--- trunk/HibernateExt/annotations/src/test/org/hibernate/test/annotations/naturalid/NaturalIdTest.java	                        (rev 0)
+++ trunk/HibernateExt/annotations/src/test/org/hibernate/test/annotations/naturalid/NaturalIdTest.java	2007-07-03 15:20:03 UTC (rev 12665)
@@ -0,0 +1,45 @@
+//$Id$
+package org.hibernate.test.annotations.naturalid;
+
+import java.util.List;
+
+import org.hibernate.Session;
+import org.hibernate.Transaction;
+import org.hibernate.criterion.Restrictions;
+import org.hibernate.test.annotations.TestCase;
+
+/**
+ * @author Emmanuel Bernard
+ */
+public class NaturalIdTest extends TestCase {
+
+	public void testNaturalId() {
+		Citizen c = new Citizen();
+		c.setFirstname( "Emmanuel" );
+		c.setLastname( "Bernard" );
+		c.setSsn( "1234" );
+		State ste = new State();
+		ste.setName( "Ile de France");
+		c.setState( ste);
+		Session s = openSession();
+		Transaction tx = s.beginTransaction();
+		s.persist( ste );
+		s.persist( c );
+		s.flush();
+		s.clear();
+		List results = s.createCriteria( Citizen.class )
+				.add( Restrictions.naturalId().set( "ssn", "1234" ).set( "state", ste ) )
+				.list();
+		assertEquals( 1, results.size() );
+
+		tx.rollback();
+		s.close();
+	}
+
+	protected Class[] getMappings() {
+		return new Class[] {
+        		Citizen.class,
+				State.class
+		};
+	}
+}

Added: trunk/HibernateExt/annotations/src/test/org/hibernate/test/annotations/naturalid/State.java
===================================================================
--- trunk/HibernateExt/annotations/src/test/org/hibernate/test/annotations/naturalid/State.java	                        (rev 0)
+++ trunk/HibernateExt/annotations/src/test/org/hibernate/test/annotations/naturalid/State.java	2007-07-03 15:20:03 UTC (rev 12665)
@@ -0,0 +1,33 @@
+//$Id$
+package org.hibernate.test.annotations.naturalid;
+
+import javax.persistence.Id;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Entity;
+
+/**
+ * @author Emmanuel Bernard
+ */
+ at Entity
+public class State {
+	@Id
+	@GeneratedValue
+	private Integer id;
+	private String name;
+
+	public Integer getId() {
+		return id;
+	}
+
+	public void setId(Integer id) {
+		this.id = id;
+	}
+
+	public String getName() {
+		return name;
+	}
+
+	public void setName(String name) {
+		this.name = name;
+	}
+}




More information about the hibernate-commits mailing list