[hibernate-commits] Hibernate SVN: r16258 - in annotations/branches/v3_3_1_GA_CP/src: test/org/hibernate/test/annotations/onetoone and 1 other directories.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Fri Apr 3 17:12:47 EDT 2009


Author: gbadner
Date: 2009-04-03 17:12:47 -0400 (Fri, 03 Apr 2009)
New Revision: 16258

Added:
   annotations/branches/v3_3_1_GA_CP/src/test/org/hibernate/test/annotations/onetoone/primarykey/
   annotations/branches/v3_3_1_GA_CP/src/test/org/hibernate/test/annotations/onetoone/primarykey/Address.java
   annotations/branches/v3_3_1_GA_CP/src/test/org/hibernate/test/annotations/onetoone/primarykey/NullablePrimaryKeyTest.java
   annotations/branches/v3_3_1_GA_CP/src/test/org/hibernate/test/annotations/onetoone/primarykey/Person.java
Modified:
   annotations/branches/v3_3_1_GA_CP/src/java/org/hibernate/cfg/OneToOneSecondPass.java
Log:
JBPAPP-1859 ANN-742 : Primary key should not be set on nullable column


Modified: annotations/branches/v3_3_1_GA_CP/src/java/org/hibernate/cfg/OneToOneSecondPass.java
===================================================================
--- annotations/branches/v3_3_1_GA_CP/src/java/org/hibernate/cfg/OneToOneSecondPass.java	2009-04-03 15:45:11 UTC (rev 16257)
+++ annotations/branches/v3_3_1_GA_CP/src/java/org/hibernate/cfg/OneToOneSecondPass.java	2009-04-03 21:12:47 UTC (rev 16258)
@@ -1,4 +1,27 @@
 //$Id$
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors.  All third-party contributions are
+ * distributed under license by Red Hat Middleware LLC.
+ *
+ * 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, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY 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
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA  02110-1301  USA
+ */
 package org.hibernate.cfg;
 
 import java.util.Iterator;
@@ -73,7 +96,7 @@
 		else {
 			referencedEntityName = targetEntity.getName();
 		}
-		value.setReferencedEntityName( referencedEntityName );
+		value.setReferencedEntityName( referencedEntityName );  
 		AnnotationBinder.defineFetchingStrategy( value, inferredData.getProperty() );
 		//value.setFetchMode( fetchMode );
 		value.setCascadeDeleteEnabled( cascadeOnDelete );
@@ -153,7 +176,7 @@
 				}
 				if ( otherSideJoin != null ) {
 					//@OneToOne @JoinTable
-					Join mappedByJoin = buildJoin(
+					Join mappedByJoin = buildJoinFromMappedBySide(
 							(PersistentClass) persistentClasses.get( ownerEntity ), otherSideProperty, otherSideJoin
 					);
 					ManyToOne manyToOne = new ManyToOne( mappedByJoin.getTable() );
@@ -215,8 +238,17 @@
 		if ( !BinderHelper.isDefault( fkName ) ) value.setForeignKeyName( fkName );
 	}
 
-	//dirty dupe of EntityBinder.bindSecondaryTable
-	private Join buildJoin(PersistentClass persistentClass, Property otherSideProperty, Join originalJoin) {
+	/**
+	 * Builds the <code>Join</code> instance for the mapped by side of a <i>OneToOne</i> association using 
+	 * a join tables.
+	 * <p>
+	 * Note:<br/>
+	 * <ul>
+	 * <li>From the mappedBy side we should not create the PK nor the FK, this is handled from the other side.</li>
+	 * <li>This method is a dirty dupe of EntityBinder.bindSecondaryTable</i>.
+	 * </p>
+	 */
+	private Join buildJoinFromMappedBySide(PersistentClass persistentClass, Property otherSideProperty, Join originalJoin) {
 		Join join = new Join();
 		join.setPersistentClass( persistentClass );
 
@@ -247,8 +279,6 @@
 			copy.setDefaultValue( column.getDefaultValue() );
 			key.addColumn( copy );
 		}
-		join.createPrimaryKey();
-		join.createForeignKey();
 		persistentClass.addJoin( join );
 		return join;
 	}

Added: annotations/branches/v3_3_1_GA_CP/src/test/org/hibernate/test/annotations/onetoone/primarykey/Address.java
===================================================================
--- annotations/branches/v3_3_1_GA_CP/src/test/org/hibernate/test/annotations/onetoone/primarykey/Address.java	                        (rev 0)
+++ annotations/branches/v3_3_1_GA_CP/src/test/org/hibernate/test/annotations/onetoone/primarykey/Address.java	2009-04-03 21:12:47 UTC (rev 16258)
@@ -0,0 +1,33 @@
+//$Id: A320.java 14736 2008-06-04 14:23:42Z hardy.ferentschik $
+package org.hibernate.test.annotations.onetoone.primarykey;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.OneToOne;
+
+ at Entity
+public class Address {
+
+	@Id
+	private long id;
+
+	@OneToOne(mappedBy = "address")
+	private Person person;
+
+	public long getId() {
+		return id;
+	}
+
+	public void setId(long id) {
+		this.id = id;
+	}
+
+	public Person getPerson() {
+		return person;
+	}
+
+	public void setPerson(Person person) {
+		this.person = person;
+	}
+
+}

Added: annotations/branches/v3_3_1_GA_CP/src/test/org/hibernate/test/annotations/onetoone/primarykey/NullablePrimaryKeyTest.java
===================================================================
--- annotations/branches/v3_3_1_GA_CP/src/test/org/hibernate/test/annotations/onetoone/primarykey/NullablePrimaryKeyTest.java	                        (rev 0)
+++ annotations/branches/v3_3_1_GA_CP/src/test/org/hibernate/test/annotations/onetoone/primarykey/NullablePrimaryKeyTest.java	2009-04-03 21:12:47 UTC (rev 16258)
@@ -0,0 +1,40 @@
+//$Id: A320.java 14736 2008-06-04 14:23:42Z hardy.ferentschik $
+package org.hibernate.test.annotations.onetoone.primarykey;
+
+import junit.framework.TestCase;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import org.hibernate.cfg.AnnotationConfiguration;
+import org.hibernate.dialect.SQLServerDialect;
+
+/**
+ * Test harness for ANN-742.
+ * 
+ * @author Hardy Ferentschik
+ * 
+ */
+public class NullablePrimaryKeyTest extends TestCase {
+
+	private static Log log = LogFactory.getLog( NullablePrimaryKeyTest.class );
+
+	public void testGeneratedSql() {
+		try {
+			AnnotationConfiguration config = new AnnotationConfiguration();
+			config.addAnnotatedClass(Address.class);
+			config.addAnnotatedClass(Person.class);
+			config.buildSessionFactory();
+			String[] schema = config
+					.generateSchemaCreationScript(new SQLServerDialect());
+			for (String s : schema) {
+				log.debug(s);
+			}
+			String expectedMappingTableSql = "create table personAddress (address_id numeric(19,0) null, " +
+					"person_id numeric(19,0) not null, primary key (person_id))";
+			assertEquals("Wrong SQL", expectedMappingTableSql, schema[2]);
+		} catch (Exception e) {
+			fail(e.getMessage());
+		}
+	}
+}

Added: annotations/branches/v3_3_1_GA_CP/src/test/org/hibernate/test/annotations/onetoone/primarykey/Person.java
===================================================================
--- annotations/branches/v3_3_1_GA_CP/src/test/org/hibernate/test/annotations/onetoone/primarykey/Person.java	                        (rev 0)
+++ annotations/branches/v3_3_1_GA_CP/src/test/org/hibernate/test/annotations/onetoone/primarykey/Person.java	2009-04-03 21:12:47 UTC (rev 16258)
@@ -0,0 +1,39 @@
+//$Id: A320.java 14736 2008-06-04 14:23:42Z hardy.ferentschik $
+package org.hibernate.test.annotations.onetoone.primarykey;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.JoinColumn;
+import javax.persistence.JoinTable;
+import javax.persistence.OneToOne;
+
+ at Entity
+public class Person {
+
+	@Id
+	private long id;
+
+	@OneToOne
+	@JoinTable(
+			name = "personAddress",
+			joinColumns = @JoinColumn(name = "person_id"),
+			inverseJoinColumns = @JoinColumn(name = "address_id")
+			)
+	private Address address;
+
+	public long getId() {
+		return id;
+	}
+
+	public void setId(long id) {
+		this.id = id;
+	}
+
+	public Address getAddress() {
+		return address;
+	}
+
+	public void setAddress(Address address) {
+		this.address = address;
+	}
+}




More information about the hibernate-commits mailing list