Author: anthonyHib
Date: 2008-12-02 10:11:04 -0500 (Tue, 02 Dec 2008)
New Revision: 15641
Modified:
branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/tool/hbm2x/pojo/EntityPOJOClass.java
branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/test/jdbc2cfg/OneToOneTest.java
Log:
HBX-524 : jpa fix
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 2008-12-02
15:00:08 UTC (rev 15640)
+++
branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/tool/hbm2x/pojo/EntityPOJOClass.java 2008-12-02
15:11:04 UTC (rev 15641)
@@ -22,6 +22,7 @@
import org.hibernate.mapping.OneToMany;
import org.hibernate.mapping.OneToOne;
import org.hibernate.mapping.PersistentClass;
+import org.hibernate.mapping.PrimaryKey;
import org.hibernate.mapping.Property;
import org.hibernate.mapping.RootClass;
import org.hibernate.mapping.Selectable;
@@ -32,6 +33,7 @@
import org.hibernate.mapping.UniqueKey;
import org.hibernate.mapping.Value;
import org.hibernate.tool.hbm2x.Cfg2JavaTool;
+import org.hibernate.type.ForeignKeyDirection;
import org.hibernate.util.JoinedIterator;
import org.hibernate.util.StringHelper;
@@ -447,16 +449,43 @@
return buffer.toString();
}
+ public boolean isSharedPkBasedOneToOne(OneToOne oneToOne){
+ Iterator joinColumnsIt = oneToOne.getColumnIterator();
+ Set joinColumns = new HashSet();
+ while ( joinColumnsIt.hasNext() ) {
+ joinColumns.add( joinColumnsIt.next() );
+ }
+
+ if ( joinColumns.size() == 0 )
+ return false;
+
+ Iterator<Column> idColumnsIt = getIdentifierProperty().getColumnIterator();
+ Set idColumns = new HashSet();
+ while ( idColumnsIt.hasNext() ) {
+ if (!joinColumns.contains(idColumnsIt.next()) )
+ return false;
+ }
+
+ return true;
+ }
+
public String generateOneToOneAnnotation(Property property, Configuration cfg) {
+ OneToOne oneToOne = (OneToOne)property.getValue();
+
+ boolean pkIsAlsoFk = isSharedPkBasedOneToOne(oneToOne);
+
AnnotationBuilder ab = AnnotationBuilder.createAnnotation(
importType("javax.persistence.OneToOne") )
.addAttribute( "cascade", getCascadeTypes(property))
.addAttribute( "fetch", getFetchType(property));
- OneToOne oneToOne = (OneToOne)property.getValue();
- if (oneToOne.isConstrained())
+
+ if ( oneToOne.getForeignKeyType().equals(ForeignKeyDirection.FOREIGN_KEY_TO_PARENT) ){
ab.addQuotedAttribute("mappedBy", getOneToOneMappedBy(cfg, oneToOne));
+ }
+
StringBuffer buffer = new StringBuffer(ab.getResult());
buffer.append(getHibernateCascadeTypeAnnotation(property));
- if (!oneToOne.isConstrained()){
+
+ if ( pkIsAlsoFk &&
oneToOne.getForeignKeyType().equals(ForeignKeyDirection.FOREIGN_KEY_FROM_PARENT) ){
AnnotationBuilder ab1 = AnnotationBuilder.createAnnotation(
importType("javax.persistence.PrimaryKeyJoinColumn") );
buffer.append(ab1.getResult());
}
@@ -691,14 +720,20 @@
joinColumns.add( joinColumnsIt.next() );
}
PersistentClass pc = cfg.getClassMapping( oneToOne.getReferencedEntityName() );
+ String referencedPropertyName = oneToOne.getReferencedPropertyName();
+ if ( referencedPropertyName != null )
+ return referencedPropertyName;
+
Iterator properties = pc.getPropertyClosureIterator();
//TODO we should check the table too
boolean isOtherSide = false;
mappedBy = "unresolved";
+
+
while ( ! isOtherSide && properties.hasNext() ) {
Property oneProperty = (Property) properties.next();
Value manyValue = oneProperty.getValue();
- if ( manyValue != null && manyValue instanceof OneToOne ) {
+ if ( manyValue != null && ( manyValue instanceof OneToOne || manyValue
instanceof ManyToOne ) ) {
if ( joinColumns.size() == manyValue.getColumnSpan() ) {
isOtherSide = true;
Iterator it = manyValue.getColumnIterator();
Modified:
branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/test/jdbc2cfg/OneToOneTest.java
===================================================================
---
branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/test/jdbc2cfg/OneToOneTest.java 2008-12-02
15:00:08 UTC (rev 15640)
+++
branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/test/jdbc2cfg/OneToOneTest.java 2008-12-02
15:11:04 UTC (rev 15641)
@@ -246,7 +246,7 @@
TestHelper.compile(
getOutputDir(), getOutputDir(), TestHelper.visitAllFiles( getOutputDir(), list ),
"1.5",
TestHelper.buildClasspath( jars )
- );
+ );
URL[] urls = new URL[] { getOutputDir().toURL() };
ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
URLClassLoader ucl = new URLClassLoader(urls, oldLoader );
@@ -299,6 +299,7 @@
"create table ADDRESS_PERSON ( address_id integer not null, name varchar(50),
primary key (address_id), constraint address_person foreign key (address_id) references
PERSON)",
"create table MULTI_PERSON ( person_id integer not null, person_compid integer
not null, name varchar(50), primary key (person_id, person_compid) )",
"create table ADDRESS_MULTI_PERSON ( address_id integer not null, address_compid
integer not null, name varchar(50), primary key (address_id, address_compid), constraint
address_multi_person foreign key (address_id, address_compid) references
MULTI_PERSON)",
+
};
}
@@ -308,7 +309,6 @@
"drop table PERSON",
"drop table ADDRESS_MULTI_PERSON",
"drop table MULTI_PERSON",
-
};
}