Author: hardy.ferentschik
Date: 2008-06-11 09:51:06 -0400 (Wed, 11 Jun 2008)
New Revision: 14761
Added:
annotations/trunk/src/test/org/hibernate/test/annotations/id/JoinColumnOverrideTest.java
annotations/trunk/src/test/org/hibernate/test/annotations/id/entities/TwinkleToes.java
Modified:
annotations/trunk/src/java/org/hibernate/cfg/AnnotationBinder.java
annotations/trunk/src/java/org/hibernate/cfg/Ejb3JoinColumn.java
annotations/trunk/src/java/org/hibernate/cfg/annotations/TableBinder.java
annotations/trunk/src/test/org/hibernate/test/annotations/id/IdTest.java
annotations/trunk/src/test/org/hibernate/test/annotations/id/entities/Bunny.java
annotations/trunk/src/test/org/hibernate/test/annotations/id/entities/PointyTooth.java
Log:
ANN-748
- Added new entity (TwinkleToe) in order to test one ManyToOne with JoinColumn and one
without
- Split test into seperate class for easier debugging
- changed overrideSqlTypeIfNecessary() to overrideFromReferencedColumnIfNecessary() in
Ejb3JoinColumn and made sure that not only sqlType gets taken care of, but also length,
precision and scale. Updated TableBinder accordingly.
Modified: annotations/trunk/src/java/org/hibernate/cfg/AnnotationBinder.java
===================================================================
--- annotations/trunk/src/java/org/hibernate/cfg/AnnotationBinder.java 2008-06-11 07:33:15
UTC (rev 14760)
+++ annotations/trunk/src/java/org/hibernate/cfg/AnnotationBinder.java 2008-06-11 13:51:06
UTC (rev 14761)
@@ -145,6 +145,7 @@
*
* @author Emmanuel Bernard
*/
+@SuppressWarnings("unchecked")
public final class AnnotationBinder {
/*
Modified: annotations/trunk/src/java/org/hibernate/cfg/Ejb3JoinColumn.java
===================================================================
--- annotations/trunk/src/java/org/hibernate/cfg/Ejb3JoinColumn.java 2008-06-11 07:33:15
UTC (rev 14760)
+++ annotations/trunk/src/java/org/hibernate/cfg/Ejb3JoinColumn.java 2008-06-11 13:51:06
UTC (rev 14761)
@@ -24,6 +24,7 @@
*
* @author Emmanuel Bernard
*/
+@SuppressWarnings("unchecked")
public class Ejb3JoinColumn extends Ejb3Column {
/**
* property name repated to this column
@@ -36,7 +37,7 @@
private String mappedByTableName;
private String mappedByEntityName;
- //FIXME hacky solution to get the information at proeprty ref resolution
+ //FIXME hacky solution to get the information at property ref resolution
public String getManyToManyOwnerSideEntityName() {
return manyToManyOwnerSideEntityName;
}
@@ -444,11 +445,24 @@
}
}
- public void overrideSqlTypeIfNecessary(org.hibernate.mapping.Column column) {
+ /**
+ * Called to apply column definitions from the referenced FK column to this column.
+ *
+ * @param column the referenced column.
+ */
+ public void overrideFromReferencedColumnIfNecessary(org.hibernate.mapping.Column column)
{
+
+ // columnDefinition can also be specified using @JoinColumn, hence we have to check
+ // whether it is set or not
if ( StringHelper.isEmpty( sqlType ) ) {
sqlType = column.getSqlType();
if ( getMappingColumn() != null ) getMappingColumn().setSqlType( sqlType );
}
+
+ // these properties can only be applied on the referenced column - we can just take
them over
+ getMappingColumn().setLength(column.getLength());
+ getMappingColumn().setPrecision(column.getPrecision());
+ getMappingColumn().setScale(column.getScale());
}
@Override
Modified: annotations/trunk/src/java/org/hibernate/cfg/annotations/TableBinder.java
===================================================================
--- annotations/trunk/src/java/org/hibernate/cfg/annotations/TableBinder.java 2008-06-11
07:33:15 UTC (rev 14760)
+++ annotations/trunk/src/java/org/hibernate/cfg/annotations/TableBinder.java 2008-06-11
13:51:06 UTC (rev 14761)
@@ -32,6 +32,7 @@
*
* @author Emmanuel Bernard
*/
+@SuppressWarnings("unchecked")
public class TableBinder {
//TODO move it to a getter/setter strategy
private static Logger log = LoggerFactory.getLogger( TableBinder.class );
@@ -201,7 +202,7 @@
}
while ( mappedByColumns.hasNext() ) {
Column column = (Column) mappedByColumns.next();
- columns[0].overrideSqlTypeIfNecessary( column );
+ columns[0].overrideFromReferencedColumnIfNecessary( column );
columns[0].linkValueUsingAColumnCopy( column, value );
}
}
@@ -219,7 +220,7 @@
}
while ( idColumns.hasNext() ) {
Column column = (Column) idColumns.next();
- columns[0].overrideSqlTypeIfNecessary( column );
+ columns[0].overrideFromReferencedColumnIfNecessary( column );
columns[0].linkValueUsingDefaultColumnNaming( column, referencedEntity, value );
}
}
@@ -306,7 +307,7 @@
else {
joinCol.linkWithValue( value );
}
- joinCol.overrideSqlTypeIfNecessary( col );
+ joinCol.overrideFromReferencedColumnIfNecessary( col );
match = true;
break;
}
@@ -329,17 +330,17 @@
private static void linkJoinColumnWithValueOverridingNameIfImplicit(
PersistentClass referencedEntity, Iterator columnIterator, Ejb3JoinColumn[] columns,
SimpleValue value
- ) {
+ ) {
for (Ejb3JoinColumn joinCol : columns) {
- Column synthCol = (Column) columnIterator.next();
+ Column synthCol = (Column) columnIterator.next();
if ( joinCol.isNameDeferred() ) {
//this has to be the default value
joinCol.linkValueUsingDefaultColumnNaming( synthCol, referencedEntity, value );
}
else {
joinCol.linkWithValue( value );
+ joinCol.overrideFromReferencedColumnIfNecessary( synthCol );
}
- joinCol.overrideSqlTypeIfNecessary( synthCol );
}
}
Modified: annotations/trunk/src/test/org/hibernate/test/annotations/id/IdTest.java
===================================================================
--- annotations/trunk/src/test/org/hibernate/test/annotations/id/IdTest.java 2008-06-11
07:33:15 UTC (rev 14760)
+++ annotations/trunk/src/test/org/hibernate/test/annotations/id/IdTest.java 2008-06-11
13:51:06 UTC (rev 14761)
@@ -3,13 +3,10 @@
import org.hibernate.Session;
import org.hibernate.Transaction;
-import org.hibernate.cfg.AnnotationConfiguration;
-import org.hibernate.dialect.SQLServerDialect;
import org.hibernate.mapping.Column;
import org.hibernate.test.annotations.TestCase;
import org.hibernate.test.annotations.id.entities.Ball;
import org.hibernate.test.annotations.id.entities.BreakDance;
-import org.hibernate.test.annotations.id.entities.Bunny;
import org.hibernate.test.annotations.id.entities.Computer;
import org.hibernate.test.annotations.id.entities.Department;
import org.hibernate.test.annotations.id.entities.Dog;
@@ -21,13 +18,10 @@
import org.hibernate.test.annotations.id.entities.Home;
import org.hibernate.test.annotations.id.entities.Monkey;
import org.hibernate.test.annotations.id.entities.Phone;
-import org.hibernate.test.annotations.id.entities.PointyTooth;
import org.hibernate.test.annotations.id.entities.Shoe;
import org.hibernate.test.annotations.id.entities.SoundSystem;
import org.hibernate.test.annotations.id.entities.Store;
import org.hibernate.test.annotations.id.entities.Tree;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
/**
* @author Emmanuel Bernard
@@ -36,8 +30,6 @@
public class IdTest extends TestCase {
// FIXME split Sequence and Id tests to explicit the run failure on Oracle etc
- private Logger log = LoggerFactory.getLogger(IdTest.class);
-
public IdTest(String x) {
super(x);
}
@@ -289,43 +281,6 @@
}
/**
- * See JIRA bug ANN-748.
- */
- public void testBlownPrecision() throws Exception {
-
- try {
- AnnotationConfiguration config = new AnnotationConfiguration();
- config.addAnnotatedClass(Bunny.class);
- config.addAnnotatedClass(PointyTooth.class);
- config.buildSessionFactory();
- String[] schema = config
- .generateSchemaCreationScript(new SQLServerDialect());
- for (String s : schema) {
- log.debug(s);
- }
- String expectedMappingTableSql = "create table PointyTooth (id numeric(128,0) not
null, " +
- "bunny_id numeric(128,0) null, primary key (id))";
- assertEquals("Wrong SQL", expectedMappingTableSql, schema[1]);
- } catch (Exception e) {
- fail(e.getMessage());
- }
-
-
-// Session s = openSession();
-// Transaction tx = s.beginTransaction();
-// Bunny bunny = new Bunny();
-// PointyTooth tooth = new PointyTooth();
-// Set<PointyTooth> teeth = new HashSet<PointyTooth>();
-// teeth.add(tooth);
-// bunny.setTeeth(teeth);
-// tooth.setBunny(bunny);
-// s.persist(bunny);
-// s.flush();
-// tx.rollback();
-// s.close();
- }
-
- /**
* @see org.hibernate.test.annotations.TestCase#getMappings()
*/
protected Class[] getMappings() {
@@ -333,7 +288,7 @@
Department.class, Dog.class, Computer.class, Home.class,
Phone.class, Tree.class, FirTree.class, Footballer.class,
SoundSystem.class, Furniture.class, GoalKeeper.class,
- BreakDance.class, Monkey.class, Bunny.class, PointyTooth.class };
+ BreakDance.class, Monkey.class};
}
/**
Added:
annotations/trunk/src/test/org/hibernate/test/annotations/id/JoinColumnOverrideTest.java
===================================================================
---
annotations/trunk/src/test/org/hibernate/test/annotations/id/JoinColumnOverrideTest.java
(rev 0)
+++
annotations/trunk/src/test/org/hibernate/test/annotations/id/JoinColumnOverrideTest.java 2008-06-11
13:51:06 UTC (rev 14761)
@@ -0,0 +1,64 @@
+//$Id$
+package org.hibernate.test.annotations.id;
+
+import java.io.PrintWriter;
+import java.io.StringWriter;
+
+import org.hibernate.cfg.AnnotationConfiguration;
+import org.hibernate.dialect.SQLServerDialect;
+import org.hibernate.test.annotations.TestCase;
+import org.hibernate.test.annotations.id.entities.Bunny;
+import org.hibernate.test.annotations.id.entities.PointyTooth;
+import org.hibernate.test.annotations.id.entities.TwinkleToes;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Tests for JIRA issue ANN-748.
+ *
+ * @author Hardy Ferentschik
+ */
+@SuppressWarnings("unchecked")
+public class JoinColumnOverrideTest extends TestCase {
+
+ private Logger log = LoggerFactory.getLogger(JoinColumnOverrideTest.class);
+
+ public JoinColumnOverrideTest(String x) {
+ super(x);
+ }
+
+ public void testBlownPrecision() throws Exception {
+
+ try {
+ AnnotationConfiguration config = new AnnotationConfiguration();
+ config.addAnnotatedClass(Bunny.class);
+ config.addAnnotatedClass(PointyTooth.class);
+ config.addAnnotatedClass(TwinkleToes.class);
+ config.buildSessionFactory();
+ String[] schema = config
+ .generateSchemaCreationScript(new SQLServerDialect());
+ for (String s : schema) {
+ log.debug(s);
+ }
+ String expectedSqlPointyTooth = "create table PointyTooth (id numeric(128,0) not
null, " +
+ "bunny_id numeric(128,0) null, primary key (id))";
+ assertEquals("Wrong SQL", expectedSqlPointyTooth, schema[1]);
+
+ String expectedSqlTwinkleToes = "create table TwinkleToes (id numeric(128,0) not
null, " +
+ "bunny_id numeric(128,0) null, primary key (id))";
+ assertEquals("Wrong SQL", expectedSqlTwinkleToes, schema[2]);
+ } catch (Exception e) {
+ StringWriter writer = new StringWriter();
+ e.printStackTrace(new PrintWriter(writer));
+ log.debug(writer.toString());
+ fail(e.getMessage());
+ }
+ }
+
+ /**
+ * @see org.hibernate.test.annotations.TestCase#getMappings()
+ */
+ protected Class[] getMappings() {
+ return new Class[] {};
+ }
+}
Property changes on:
annotations/trunk/src/test/org/hibernate/test/annotations/id/JoinColumnOverrideTest.java
___________________________________________________________________
Name: svn:keywords
+ Id
Modified:
annotations/trunk/src/test/org/hibernate/test/annotations/id/entities/Bunny.java
===================================================================
---
annotations/trunk/src/test/org/hibernate/test/annotations/id/entities/Bunny.java 2008-06-11
07:33:15 UTC (rev 14760)
+++
annotations/trunk/src/test/org/hibernate/test/annotations/id/entities/Bunny.java 2008-06-11
13:51:06 UTC (rev 14761)
@@ -32,8 +32,15 @@
@OneToMany(mappedBy = "bunny", cascade = { CascadeType.PERSIST })
Set<PointyTooth> teeth;
+
+ @OneToMany(mappedBy = "bunny", cascade = { CascadeType.PERSIST })
+ Set<TwinkleToes> toes;
public void setTeeth(Set<PointyTooth> teeth) {
this.teeth = teeth;
}
+
+ public BigInteger getId() {
+ return id;
+ }
}
Modified:
annotations/trunk/src/test/org/hibernate/test/annotations/id/entities/PointyTooth.java
===================================================================
---
annotations/trunk/src/test/org/hibernate/test/annotations/id/entities/PointyTooth.java 2008-06-11
07:33:15 UTC (rev 14760)
+++
annotations/trunk/src/test/org/hibernate/test/annotations/id/entities/PointyTooth.java 2008-06-11
13:51:06 UTC (rev 14761)
@@ -31,11 +31,14 @@
private BigInteger id;
@ManyToOne
- // comment out the below line and the test will pass
- //@JoinColumn(name = "bugs_bunny_id")
+ @JoinColumn(name = "bunny_id")
Bunny bunny;
public void setBunny(Bunny bunny) {
this.bunny = bunny;
}
+
+ public BigInteger getId() {
+ return id;
+ }
}
Added:
annotations/trunk/src/test/org/hibernate/test/annotations/id/entities/TwinkleToes.java
===================================================================
---
annotations/trunk/src/test/org/hibernate/test/annotations/id/entities/TwinkleToes.java
(rev 0)
+++
annotations/trunk/src/test/org/hibernate/test/annotations/id/entities/TwinkleToes.java 2008-06-11
13:51:06 UTC (rev 14761)
@@ -0,0 +1,42 @@
+//$Id$
+package org.hibernate.test.annotations.id.entities;
+
+import java.io.Serializable;
+import java.math.BigInteger;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.ManyToOne;
+
+import org.hibernate.annotations.GenericGenerator;
+
+/**
+ * Blown precision on related entity when @JoinColumn is used.
+ * Does not cause an issue on HyperSonic, but replicates nicely on PGSQL.
+ *
+ * @see ANN-748
+ * @author Andrew C. Oliver andyspam(a)osintegrators.com
+ */
+@Entity
+@SuppressWarnings("serial")
+public class TwinkleToes implements Serializable {
+ @Id
+ @GeneratedValue(strategy = GenerationType.IDENTITY, generator = "java5_uuid")
+ @GenericGenerator(name = "java5_uuid", strategy =
"org.hibernate.test.annotations.id.UUIDGenerator")
+ @Column(name = "id", precision = 128, scale = 0)
+ private BigInteger id;
+
+ @ManyToOne
+ Bunny bunny;
+
+ public void setBunny(Bunny bunny) {
+ this.bunny = bunny;
+ }
+
+ public BigInteger getId() {
+ return id;
+ }
+}
Property changes on:
annotations/trunk/src/test/org/hibernate/test/annotations/id/entities/TwinkleToes.java
___________________________________________________________________
Name: svn:keywords
+ Id