Author: max.andersen(a)jboss.com
Date: 2007-11-04 03:49:09 -0500 (Sun, 04 Nov 2007)
New Revision: 14185
Modified:
branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/tool/hbm2x/pojo/EntityPOJOClass.java
Log:
fixed @JoinColumn
Modified:
branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/tool/hbm2x/pojo/EntityPOJOClass.java
===================================================================
---
branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/tool/hbm2x/pojo/EntityPOJOClass.java 2007-11-04
08:48:14 UTC (rev 14184)
+++
branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/tool/hbm2x/pojo/EntityPOJOClass.java 2007-11-04
08:49:09 UTC (rev 14185)
@@ -329,12 +329,13 @@
return propertyValue != null && propertyValue.equals( defaultValue );
}
- public String generateJoinColumnsAnnotation(Property property) {
+ public String generateJoinColumnsAnnotation(Property property, Configuration cfg) {
boolean insertable = property.isInsertable();
boolean updatable = property.isUpdateable();
Value value = property.getValue();
int span;
Iterator columnIterator;
+ Iterator referencedColumnsIterator = null;
if (value != null && value instanceof Collection) {
Collection collection = (Collection) value;
span = collection.getKey().getColumnSpan();
@@ -344,35 +345,47 @@
span = property.getColumnSpan();
columnIterator = property.getColumnIterator();
}
+
+ if(property.getValue() instanceof ToOne) {
+ String referencedEntityName = ((ToOne)property.getValue()).getReferencedEntityName();
+ PersistentClass target = cfg.getClassMapping(referencedEntityName);
+ if(target!=null) {
+ referencedColumnsIterator = target.getKey().getColumnIterator();
+ }
+ }
StringBuffer annotations = new StringBuffer( " " );
if ( span == 1 ) {
Selectable selectable = (Selectable) columnIterator.next();
- buildJoinColumnAnnotation( selectable, annotations, insertable, updatable );
+ buildJoinColumnAnnotation( selectable, null, annotations, insertable, updatable );
}
else {
Iterator columns = columnIterator;
annotations.append("(a)").append(
importType("javax.persistence.JoinColumns") ).append("( { " );
- buildArrayOfJoinColumnAnnotation( columns, annotations, insertable, updatable );
+ buildArrayOfJoinColumnAnnotation( columns, referencedColumnsIterator, annotations,
insertable, updatable );
annotations.append( " } )" );
}
return annotations.toString();
}
private void buildArrayOfJoinColumnAnnotation(
- Iterator columns, StringBuffer annotations, boolean insertable,
+ Iterator columns, Iterator referencedColumnsIterator, StringBuffer annotations,
boolean insertable,
boolean updatable
) {
while ( columns.hasNext() ) {
Selectable selectable = (Selectable) columns.next();
-
+ Selectable referencedColumn = null;
+ if(referencedColumnsIterator!=null) {
+ referencedColumn = (Selectable) referencedColumnsIterator.next();
+ }
+
if ( selectable.isFormula() ) {
//TODO formula in multicolumns not supported by annotations
//annotations.append("/* TODO formula in multicolumns not supported by
annotations */");
}
else {
annotations.append( "\n " );
- buildJoinColumnAnnotation( selectable, annotations, insertable, updatable );
+ buildJoinColumnAnnotation( selectable, referencedColumn, annotations, insertable,
updatable );
annotations.append( ", " );
}
}
@@ -380,7 +393,7 @@
}
private void buildJoinColumnAnnotation(
- Selectable selectable, StringBuffer annotations, boolean insertable, boolean
updatable
+ Selectable selectable, Selectable referencedColumn, StringBuffer annotations, boolean
insertable, boolean updatable
) {
if ( selectable.isFormula() ) {
//TODO not supported by HA
@@ -390,7 +403,10 @@
annotations.append("(a)").append(
importType("javax.persistence.JoinColumn") )
.append("(name=\"" ).append( column.getName() ).append(
"\"" );
//TODO handle referenced column name, this is a hard one
- //.append(", referencedColumnName=")
+ if(referencedColumn!=null) {
+ annotations.append(", referencedColumnName=\"" ).append(
referencedColumn.getText() ).append( "\"" );
+ }
+
appendCommonColumnInfo(annotations, column, insertable, updatable);
//TODO support secondary table
annotations.append( ")" );
@@ -513,7 +529,7 @@
}
annotation.append( ab.getResult() );
- if (mappedBy == null) annotation.append("\n").append(
generateJoinColumnsAnnotation(property) );
+ if (mappedBy == null) annotation.append("\n").append(
generateJoinColumnsAnnotation(property, cfg) );
}
else {
//TODO do the @OneToMany @JoinTable
@@ -548,6 +564,7 @@
annotation.append( ", joinColumns = { ");
buildArrayOfJoinColumnAnnotation(
collection.getKey().getColumnIterator(),
+ null,
annotation,
property.isInsertable(),
property.isUpdateable()
@@ -556,6 +573,7 @@
annotation.append( ", inverseJoinColumns = { ");
buildArrayOfJoinColumnAnnotation(
collection.getElement().getColumnIterator(),
+ null,
annotation,
property.isInsertable(),
property.isUpdateable()