Java Mailing List Archive

http://www.gg3721.com/

Home » Hibernate Commits List »

[hibernate-commits] Hibernate SVN: r15015 - in
 branches/Branch_3_2/HibernateExt/tools:
 src/java/org/hibernate/cfg and 6 other directories.

hibernate-commits

2008-08-03


Author LoginPost Reply
Author: max.andersen@(protected)
Date: 2008-08-03 12:04:47 -0400 (Sun, 03 Aug 2008)
New Revision: 15015

Added:
 branches/Branch_3_2/HibernateExt/tools/src/templates/hbm/one-to-one.hbm.ftl
 branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/hbm2x/hbm2hbmxml/Address.java
 branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/hbm2x/hbm2hbmxml/OneToOneTest.java
 branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/hbm2x/hbm2hbmxml/Person.java
 branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/hbm2x/hbm2hbmxml/PersonAddressOneToOnePrimaryKey.hbm.xml
 branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/test/jdbc2cfg/OneToOneTest.java
Modified:
 branches/Branch_3_2/HibernateExt/tools/
 branches/Branch_3_2/HibernateExt/tools/build.xml
 branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/cfg/JDBCBinder.java
 branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/cfg/reveng/DefaultReverseEngineeringStrategy.java
 branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/cfg/reveng/DelegatingReverseEngineeringStrategy.java
 branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/cfg/reveng/ReverseEngineeringSettings.java
 branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/cfg/reveng/ReverseEngineeringStrategy.java
 branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/tool/Version.java
 branches/Branch_3_2/HibernateExt/tools/src/testsupport/NoopReverseEngineeringStrategy.java
Log:
HBX-524 one-to-one reveng
(primary-key constrained bidirectional, no JPA support nor reveng.xml support yet)


Property changes on: branches/Branch_3_2/HibernateExt/tools
___________________________________________________________________
Name: svn:ignore
 - build
target
hbm2xoutput
eclipsebuild
cfg2hbmoutput
reverseoutput
test_output
.settings
.classpath
.project
test.lck
test.log
test.properties
test.script
cloverout
clover_coverage.db*
toolstestoutput
 + build
target
hbm2xoutput
eclipsebuild
cfg2hbmoutput
reverseoutput
test_output
.settings
.classpath
.project
test.lck
test.log
test.properties
test.script
cloverout
clover_coverage.db*
toolstestoutput
testdb


Modified: branches/Branch_3_2/HibernateExt/tools/build.xml
===================================================================
--- branches/Branch_3_2/HibernateExt/tools/build.xml  2008-08-01 17:10:49 UTC (rev 15014)
+++ branches/Branch_3_2/HibernateExt/tools/build.xml  2008-08-03 16:04:47 UTC (rev 15015)
@@(protected) @@
  <!-- Name of project and version, used to create filenames -->
  <property name="Name" value="Hibernate Tools"/>
  <property name="name" value="hibernate-tools"/>
-   <property name="version" value="3.2.2.CR1"/>
+   <property name="version" value="3.2.2.GA"/>

 <property name="javadoc.packagenames" value="org.hibernate.tool"/>  


Modified: branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/cfg/JDBCBinder.java
===================================================================
--- branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/cfg/JDBCBinder.java  2008-08-01 17:10:49 UTC (rev 15014)
+++ branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/cfg/JDBCBinder.java  2008-08-03 16:04:47 UTC (rev 15015)
@@(protected) @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
+import java.util.Properties;
import java.util.Set;

import org.apache.commons.logging.Log;
@@(protected) @@
import org.hibernate.mapping.ForeignKey;
import org.hibernate.mapping.KeyValue;
import org.hibernate.mapping.ManyToOne;
+import org.hibernate.mapping.OneToOne;
import org.hibernate.mapping.MetaAttribute;
import org.hibernate.mapping.OneToMany;
import org.hibernate.mapping.PersistentClass;
@@(protected) @@
import org.hibernate.mapping.SimpleValue;
import org.hibernate.mapping.Table;
import org.hibernate.mapping.Value;
+import org.hibernate.type.ForeignKeyDirection;
import org.hibernate.type.Type;
import org.hibernate.type.TypeFactory;
import org.hibernate.util.JoinedIterator;
@@(protected) @@
     
     Set processed = new HashSet();
       
-      bindPrimaryKeyToProperties(table, rc, processed, mapping, collector);
+      
+      PrimaryKeyInfo pki = bindPrimaryKeyToProperties(table, rc, processed, mapping, collector);
     bindColumnsToVersioning(table, rc, processed, mapping);
     bindOutgoingForeignKeys(table, rc, processed);
     bindColumnsToProperties(table, rc, processed, mapping);
     List incomingForeignKeys = (List) manyToOneCandidates.get( rc.getEntityName() );
     bindIncomingForeignKeys(rc, processed, incomingForeignKeys, mapping);
+      updatePrimaryKey(rc, pki);
+      
   }
   
 }
+
+  private void updatePrimaryKey(RootClass rc, PrimaryKeyInfo pki) {
+    SimpleValue idValue = (SimpleValue) rc.getIdentifierProperty().getValue();
   
+    Properties defaultStrategyProperties = new Properties();
+    Property constrainedOneToOne = getConstrainedOneToOne(rc);
+    if(constrainedOneToOne!=null) {
+      if(pki.suggestedStrategy==null) {
+        idValue.setIdentifierGeneratorStrategy("foreign");        
+      }
+      
+      if(pki.suggestedProperties==null) {
+        defaultStrategyProperties.setProperty("property", constrainedOneToOne.getName());
+        idValue.setIdentifierGeneratorProperties(defaultStrategyProperties);
+      }
+    }
+    
+    
+    
+  }
+
+  private Property getConstrainedOneToOne(RootClass rc) {
+    Iterator propertyClosureIterator = rc.getPropertyClosureIterator();
+    while (propertyClosureIterator.hasNext()) {
+      Property property = (Property) propertyClosureIterator.next();
+      if(property.getValue() instanceof OneToOne) {
+        OneToOne oto = (OneToOne) property.getValue();
+        if(oto.isConstrained()) {
+          return property;
+        }
+      }
+    }
+    return null;
+  }
+    
 private Map safeMeta(Map map) {
   if(map==null) {
     return new HashMap();
@@(protected) @@
           TableIdentifier.create(foreignKey.getReferencedTable() ),
           foreignKey.getReferencedColumns())) {
         log.debug("Rev.eng excluded one-to-many for foreignkey " + foreignKey.getName());
-        } else {
-                    
+        } else if (revengStrategy.isOneToOne(foreignKey)){
+          Property property = bindOneToOne(rc, foreignKey.getTable(), foreignKey, processed, false);
+          rc.addProperty(property);
+        } else {                    
         Property property = bindOneToMany(rc, foreignKey, processed, mapping);
         rc.addProperty(property);
       }
@@(protected) @@
   }
 }

+  
+   private Property bindOneToOne(PersistentClass rc, Table targetTable,
+        ForeignKey fk, Set processedColumns, boolean constrained) {
+
+    
+     OneToOne value = new OneToOne(targetTable, rc);
+     value.setReferencedEntityName(revengStrategy
+           .tableToClassName(TableIdentifier.create(targetTable)));
+
+     boolean isUnique = isUniqueReference(fk);
+     String propertyName =
+           revengStrategy.foreignKeyToEntityName(fk.getName(),
+                TableIdentifier.create(fk.getReferencedTable()), fk
+                     .getReferencedColumns(), TableIdentifier
+                     .create(targetTable), fk.getColumns(), isUnique);
+
+     Iterator columns = fk.getColumnIterator();
+     while (columns.hasNext()) {
+        Column fkcolumn = (Column) columns.next();
+        checkColumn(fkcolumn);
+        value.addColumn(fkcolumn);
+        processedColumns.add(fkcolumn);
+     }
+
+     value.setFetchMode(FetchMode.SELECT);
+        
+     value.setConstrained(constrained);
+     value.setForeignKeyType( constrained ?
+        ForeignKeyDirection.FOREIGN_KEY_FROM_PARENT :
+        ForeignKeyDirection.FOREIGN_KEY_TO_PARENT );
+
+     return makeProperty(TableIdentifier.create(targetTable), propertyName, value,
+           true, true, value.getFetchMode() != FetchMode.JOIN, null, null);
+   }
+
  /**
   * @param table
   * @param fk
@@(protected) @@
   return true;
 }

-  private void bindPrimaryKeyToProperties(Table table, RootClass rc, Set processed, Mapping mapping, DatabaseCollector collector) {
+   static private class PrimaryKeyInfo {
+    
+    String suggestedStrategy = null;
+    Properties suggestedProperties = null;  
+   }
+  
+  
+  private PrimaryKeyInfo bindPrimaryKeyToProperties(Table table, RootClass rc, Set processed, Mapping mapping, DatabaseCollector collector) {
   SimpleValue id = null;
   String idPropertyname = null;
+  
+    PrimaryKeyInfo pki = new PrimaryKeyInfo();
   
   List keyColumns = null;
   if (table.getPrimaryKey()!=null) {
@@(protected) @@

   final TableIdentifier tableIdentifier = TableIdentifier.create(table);
   
-    String tableIdentifierStrategyName;
+    String tableIdentifierStrategyName = "assigned";
   
   boolean naturalId;
   
   if (keyColumns.size()>1) {
     log.debug("id strategy for " + rc.getEntityName() + " since it has a multiple column primary key");
-      tableIdentifierStrategyName = "assigned";
     naturalId = true;
     
     id = handleCompositeKey(rc, processed, keyColumns, mapping);
@@(protected) @@
     }
   }
   else {
-      tableIdentifierStrategyName = "assigned";
-      String suggestedStrategy = revengStrategy.getTableIdentifierStrategyName(tableIdentifier);
+      pki.suggestedStrategy = revengStrategy.getTableIdentifierStrategyName(tableIdentifier);
+      String suggestedStrategy = pki.suggestedStrategy;
     if(suggestedStrategy==null) {
       suggestedStrategy = collector.getSuggestedIdentifierStrategy( tableIdentifier.getCatalog(), tableIdentifier.getSchema(), tableIdentifier.getName() );
       if(suggestedStrategy==null) {
@@(protected) @@
     processed.add(pkc);
   }
   id.setIdentifierGeneratorStrategy(tableIdentifierStrategyName);
-    id.setIdentifierGeneratorProperties(revengStrategy.getTableIdentifierProperties(tableIdentifier));    
+    pki.suggestedProperties = revengStrategy.getTableIdentifierProperties(tableIdentifier);
+    id.setIdentifierGeneratorProperties(pki.suggestedProperties);    
   if(naturalId) {
     id.setNullValue("undefined");
   }
   
-    
   Property property = makeProperty(tableIdentifier, makeUnique(rc,idPropertyname), id, true, true, false, null, null);
   rc.setIdentifierProperty(property);        
   rc.setIdentifier(id);
     
+    return pki;
 }

 /**
@@(protected) @@
           foreignKey.getReferencedColumns())) {
         // TODO: if many-to-one is excluded should the column be marked as processed so it won't show up at all ?
         log.debug("Rev.eng excluded many-to-one for foreignkey " + foreignKey.getName());
-        } else {
+        } else if (revengStrategy.isOneToOne(foreignKey)){
+        Property property = bindOneToOne(rc, foreignKey.getReferencedTable(), foreignKey, processedColumns, true);
+        rc.addProperty(property);
+      } else {
         boolean isUnique = isUniqueReference(foreignKey);
         String propertyName = revengStrategy.foreignKeyToEntityName(
             foreignKey.getName(),

Modified: branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/cfg/reveng/DefaultReverseEngineeringStrategy.java
===================================================================
--- branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/cfg/reveng/DefaultReverseEngineeringStrategy.java  2008-08-01 17:10:49 UTC (rev 15014)
+++ branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/cfg/reveng/DefaultReverseEngineeringStrategy.java  2008-08-03 16:04:47 UTC (rev 15015)
@@(protected) @@
   this.settings = settings;    
 }

+  public boolean isOneToOne(ForeignKey foreignKey) {
+    if(settings.getDetectOneToOne()) {
+      List fkColumns = foreignKey.getColumns();
+      List pkForeignTableColumns = null;
+
+      if (foreignKey.getTable().hasPrimaryKey())
+        pkForeignTableColumns = foreignKey.getTable().getPrimaryKey().getColumns();
+
+      boolean equals =
+        fkColumns != null && pkForeignTableColumns != null
+        && fkColumns.size() == pkForeignTableColumns.size();
+
+      Iterator columns = foreignKey.getColumnIterator();
+      while (equals && columns.hasNext()) {
+        Column fkColumn = (Column) columns.next();
+        equals = equals && pkForeignTableColumns.contains(fkColumn);
+      }
+
+      return equals;
+    } else {
+      return false;
+    }
+   }
+
 public boolean isManyToManyTable(Table table) {
   if(settings.getDetectManyToMany()) {
     Iterator foreignKeyIterator = table.getForeignKeyIterator();

Modified: branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/cfg/reveng/DelegatingReverseEngineeringStrategy.java
===================================================================
--- branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/cfg/reveng/DelegatingReverseEngineeringStrategy.java  2008-08-01 17:10:49 UTC (rev 15014)
+++ branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/cfg/reveng/DelegatingReverseEngineeringStrategy.java  2008-08-03 16:04:47 UTC (rev 15015)
@@(protected) @@
 public boolean isManyToManyTable(Table table) {
   return delegate==null?true:delegate.isManyToManyTable( table );
 }
+  
+  public boolean isOneToOne(ForeignKey foreignKey) {
+    return delegate==null?true:delegate.isOneToOne( foreignKey );
+   }

+
 public String foreignKeyToManyToManyName(ForeignKey fromKey, TableIdentifier middleTable, ForeignKey toKey, boolean uniqueReference) {
   return delegate==null?null:delegate.foreignKeyToManyToManyName( fromKey, middleTable, toKey, uniqueReference );
 }

Modified: branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/cfg/reveng/ReverseEngineeringSettings.java
===================================================================
--- branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/cfg/reveng/ReverseEngineeringSettings.java  2008-08-01 17:10:49 UTC (rev 15014)
+++ branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/cfg/reveng/ReverseEngineeringSettings.java  2008-08-03 16:04:47 UTC (rev 15015)
@@(protected) @@
 private boolean createCollectionForForeignKey = true;
 private boolean createManyToOneForForeignKey = true;
 private boolean detectManyToMany = true;
+  private boolean detectOneToOne = true;
+
 
 public ReverseEngineeringSettings(ReverseEngineeringStrategy rootStrategy) {
   this.rootStrategy = rootStrategy;
@@(protected) @@
   return detectManyToMany;
 }
 
+  public boolean getDetectOneToOne() {
+    return detectOneToOne;
+  }
+  
 /** return the top/root strategy. Allows a lower strategy to ask another question. Be aware of possible recursive loops; e.g. do not call the root.tableToClassName in tableToClassName of a custom reversengineeringstrategy. */
 public ReverseEngineeringStrategy getRootStrategy() {
   return rootStrategy;
 }
+
 
+  
}

Modified: branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/cfg/reveng/ReverseEngineeringStrategy.java
===================================================================
--- branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/cfg/reveng/ReverseEngineeringStrategy.java  2008-08-01 17:10:49 UTC (rev 15014)
+++ branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/cfg/reveng/ReverseEngineeringStrategy.java  2008-08-03 16:04:47 UTC (rev 15015)
@@(protected) @@
   */
  public String foreignKeyToManyToManyName(ForeignKey fromKey, TableIdentifier middleTable, ForeignKey toKey, boolean uniqueReference);

+  public boolean isOneToOne(ForeignKey foreignKey);
+
 
 
 

Modified: branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/tool/Version.java
===================================================================
--- branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/tool/Version.java  2008-08-01 17:10:49 UTC (rev 15014)
+++ branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/tool/Version.java  2008-08-03 16:04:47 UTC (rev 15015)
@@(protected) @@

final public class Version {

-  public static final String VERSION = "3.2.2.CR1";
+  public static final String VERSION = "3.2.2.GA";
 
 private static final Version instance = new Version();
 

Added: branches/Branch_3_2/HibernateExt/tools/src/templates/hbm/one-to-one.hbm.ftl
===================================================================
--- branches/Branch_3_2/HibernateExt/tools/src/templates/hbm/one-to-one.hbm.ftl                  (rev 0)
+++ branches/Branch_3_2/HibernateExt/tools/src/templates/hbm/one-to-one.hbm.ftl  2008-08-03 16:04:47 UTC (rev 15015)
@@(protected) @@
+   <one-to-one
+     name="${property.name}"
+     class="${c2j.getJavaTypeName(property, false)}"
+<#if !property.basicPropertyAccessor>
+     access="${property.propertyAccessorName}"
+</#if>
+<#if property.cascade != "none">
+     cascade="${property.cascade}"
+</#if>
+<#if property.value.constrained>
+     constrained="true"
+</#if>
+
+<#if property.value.hasFormula()>
+<#assign formula = c2h.getFormulaForProperty(property)>
+<#if formula>
+     formula="${formula.text}"
+</#if>
+</#if>
+   >
+  </one-to-one>

Added: branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/hbm2x/hbm2hbmxml/Address.java
===================================================================
--- branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/hbm2x/hbm2hbmxml/Address.java                  (rev 0)
+++ branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/hbm2x/hbm2hbmxml/Address.java  2008-08-03 16:04:47 UTC (rev 15015)
@@(protected) @@
+package org.hibernate.tool.hbm2x.hbm2hbmxml;
+
+public class Address {
+
+  int id;
+  
+  Person person;
+}

Added: branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/hbm2x/hbm2hbmxml/OneToOneTest.java
===================================================================
--- branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/hbm2x/hbm2hbmxml/OneToOneTest.java                  (rev 0)
+++ branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/hbm2x/hbm2hbmxml/OneToOneTest.java  2008-08-03 16:04:47 UTC (rev 15015)
@@(protected) @@
+//$Id$
+
+/*
+ * Tests for generating the HBM documents from the Configuration data structure.
+ * The generated XML document will be validated and queried to make sure the
+ * basic structure is correct in each test.
+ */
+package org.hibernate.tool.hbm2x.hbm2hbmxml;
+
+import java.io.File;
+import java.util.List;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.dom4j.Document;
+import org.dom4j.DocumentException;
+import org.dom4j.DocumentHelper;
+import org.dom4j.Element;
+import org.dom4j.XPath;
+import org.dom4j.io.SAXReader;
+import org.hibernate.cfg.Configuration;
+import org.hibernate.tool.NonReflectiveTestCase;
+import org.hibernate.tool.hbm2x.Exporter;
+import org.hibernate.tool.hbm2x.HibernateMappingExporter;
+import org.hibernate.util.DTDEntityResolver;
+
+public class OneToOneTest extends NonReflectiveTestCase {
+
+  private Exporter hbmexporter;
+
+  public OneToOneTest(String name) {
+    super( name );
+  }
+
+  protected void setUp() throws Exception {
+    super.setUp();
+    
+    hbmexporter = new HibernateMappingExporter(getCfg(), getOutputDir() );
+    hbmexporter.start();    
+  }
+  
+  public void testAllFilesExistence() {
+
+    assertFalse(new File(getOutputDir().getAbsolutePath() + "/GeneralHbmSettings.hbm.xml").exists() );
+    assertFileAndExists(new File(getOutputDir().getAbsolutePath() + "/org/hibernate/tool/hbm2x/hbm2hbmxml/Person.hbm.xml") );
+    assertFileAndExists(new File(getOutputDir().getAbsolutePath() + "/org/hibernate/tool/hbm2x/hbm2hbmxml/Address.hbm.xml") );    
+  }
+  
+  public void testArtifactCollection() {
+    
+    assertEquals(2,hbmexporter.getArtifactCollector().getFileCount("hbm.xml"));
+    
+  }
+  
+  public void testReadable() {
+     Configuration cfg = new Configuration();
+    
+     cfg.addFile(new File(getOutputDir(), getBaseForMappings() + "Person.hbm.xml"));
+     cfg.addFile(new File(getOutputDir(), getBaseForMappings() + "Address.hbm.xml"));
+    
+     cfg.buildMappings();
+    
+   }
+  
+  public void testOneToOne() throws DocumentException {
+    Document document = getXMLDocument(getBaseForMappings() + "Person.hbm.xml");    
+  
+    XPath xpath = DocumentHelper.createXPath("//hibernate-mapping/class/one-to-one");
+    List list = xpath.selectNodes(document);
+    assertEquals("Expected to get one-to-one element", 1, list.size());
+    Element node = (Element) list.get(0);
+    assertEquals(node.attribute( "name" ).getText(),"address");
+    assertEquals(node.attribute( "constrained" ).getText(),"false");
+    
+    document = getXMLDocument(getBaseForMappings() + "Address.hbm.xml");
+    
+    xpath = DocumentHelper.createXPath("//hibernate-mapping/class/one-to-one");
+    list = xpath.selectNodes(document);
+    assertEquals("Expected to get one set element", 1, list.size());
+    node = (Element) list.get(0);
+    assertEquals(node.attribute( "name" ).getText(),"person");
+    assertEquals(node.attribute( "constrained" ).getText(),"true");
+    
+    
+  }
+
+  private Document getXMLDocument(String location) throws DocumentException {
+    File outputXml = new File(getOutputDir(), location);
+    assertFileAndExists(outputXml);
+
+    SAXReader xmlReader = this.getSAXReader();
+    
+    Document document = xmlReader.read(outputXml);
+    return document;
+  }
+  
+  
+   private SAXReader getSAXReader() {
+    SAXReader xmlReader = new SAXReader();
+    xmlReader.setEntityResolver(new DTDEntityResolver() );
+    xmlReader.setValidation(true);
+    return xmlReader;
+   }
+  
+  protected String getBaseForMappings() {
+    return "org/hibernate/tool/hbm2x/hbm2hbmxml/";
+  }
+  
+  protected String[] getMappings() {
+    return new String[] {
+        "PersonAddressOneToOnePrimaryKey.hbm.xml"        
+    };
+  }
+    
+  public static Test suite() {
+    return new TestSuite(OneToOneTest.class);
+  }
+
+}

Added: branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/hbm2x/hbm2hbmxml/Person.java
===================================================================
--- branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/hbm2x/hbm2hbmxml/Person.java                  (rev 0)
+++ branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/hbm2x/hbm2hbmxml/Person.java  2008-08-03 16:04:47 UTC (rev 15015)
@@(protected) @@
+package org.hibernate.tool.hbm2x.hbm2hbmxml;
+
+public class Person {
+
+  int id;
+  
+  Address address;
+}

Added: branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/hbm2x/hbm2hbmxml/PersonAddressOneToOnePrimaryKey.hbm.xml
===================================================================
--- branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/hbm2x/hbm2hbmxml/PersonAddressOneToOnePrimaryKey.hbm.xml                  (rev 0)
+++ branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/hbm2x/hbm2hbmxml/PersonAddressOneToOnePrimaryKey.hbm.xml  2008-08-03 16:04:47 UTC (rev 15015)
@@(protected) @@
+<?xml version="1.0"?>
+<!DOCTYPE hibernate-mapping PUBLIC
+  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
+  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
+
+<hibernate-mapping
+  package="org.hibernate.tool.hbm2x.hbm2hbmxml">
+<class name="Person">
+   <id name="id" column="personId">
+     <generator class="native"/>
+   </id>
+   <one-to-one name="address"/>
+</class>
+
+<class name="Address">
+   <id name="id" column="personId">
+     <generator class="foreign">
+        <param name="property">person</param>
+     </generator>
+   </id>
+   <one-to-one name="person"
+     constrained="true"/>
+</class>
+</hibernate-mapping>
\ No newline at end of file

Added: 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                  (rev 0)
+++ branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/test/jdbc2cfg/OneToOneTest.java  2008-08-03 16:04:47 UTC (rev 15015)
@@(protected) @@
+/*
+ * Created on 2004-12-01
+ *
+ */
+package org.hibernate.tool.test.jdbc2cfg;
+
+import java.io.File;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.hibernate.MappingException;
+import org.hibernate.cfg.Configuration;
+import org.hibernate.cfg.JDBCMetaDataConfiguration;
+import org.hibernate.cfg.reveng.DefaultReverseEngineeringStrategy;
+import org.hibernate.cfg.reveng.ReverseEngineeringSettings;
+import org.hibernate.mapping.OneToOne;
+import org.hibernate.mapping.PersistentClass;
+import org.hibernate.mapping.Property;
+import org.hibernate.mapping.SimpleValue;
+import org.hibernate.tool.JDBCMetaDataBinderTestCase;
+import org.hibernate.tool.hbm2x.HibernateMappingExporter;
+
+/**
+ * @author max
+ *
+ */
+public class OneToOneTest extends JDBCMetaDataBinderTestCase {
+  
+  public static Test suite() {
+    return new TestSuite(OneToOneTest.class);
+  }
+
+  private JDBCMetaDataConfiguration localCfg;
+
+  protected void configure(JDBCMetaDataConfiguration configuration) {
+    super.configure( configuration );      
+    
+  }
+  
+  protected void setUp() throws Exception {
+    super.setUp();
+    
+    localCfg = new JDBCMetaDataConfiguration();
+    
+     DefaultReverseEngineeringStrategy c = new DefaultReverseEngineeringStrategy();
+          
+     localCfg.setReverseEngineeringStrategy(c);
+     localCfg.readFromJDBC();
+  }
+  protected void tearDown() throws Exception {
+    localCfg = null;
+    
+    super.tearDown();
+  }
+
+  public void testOneToOneSingleColumnBiDirectional() {
+    
+    PersistentClass person = localCfg.getClassMapping("Person");
+    
+    Property addressProperty = person.getProperty("addressPerson");
+    assertNotNull(addressProperty);
+        
+    assertTrue(addressProperty.getValue() instanceof OneToOne);
+    
+    OneToOne oto = (OneToOne) addressProperty.getValue();
+    
+    assertEquals(oto.getColumnSpan(),1);
+    assertEquals("Person", oto.getEntityName());
+    assertEquals("AddressPerson", oto.getReferencedEntityName());
+    
+    
+    assertEquals(2, person.getPropertyClosureSpan());    
+    assertEquals("personId", person.getIdentifierProperty().getName());
+    assertFalse(oto.isConstrained());
+    
+    PersistentClass addressPerson = localCfg.getClassMapping("AddressPerson");
+    
+    
+    Property personProperty = addressPerson.getProperty("person");
+    assertNotNull(personProperty);
+    
+    assertTrue(personProperty.getValue() instanceof OneToOne);
+    
+    oto = (OneToOne) personProperty.getValue();
+    
+    assertTrue(oto.isConstrained());    
+    assertEquals(oto.getColumnSpan(),1);
+    assertEquals("AddressPerson", oto.getEntityName());
+    assertEquals("Person", oto.getReferencedEntityName());
+    
+    assertEquals(2, addressPerson.getPropertyClosureSpan());
+    assertEquals("addressId", addressPerson.getIdentifierProperty().getName());
+        
+  }
+  
+  public void testAddressWithForeignKeyGeneration() {
+    
+    PersistentClass address = localCfg.getClassMapping("AddressPerson");
+    
+    assertEquals("foreign", ((SimpleValue)address.getIdentifier()).getIdentifierGeneratorStrategy());
+  }
+
+  public void testOneToOneMultiColumnBiDirectional() {
+    
+    PersistentClass person = localCfg.getClassMapping("MultiPerson");
+    
+    Property addressProperty = person.getProperty("addressMultiPerson");
+    assertNotNull(addressProperty);
+        
+    assertTrue(addressProperty.getValue() instanceof OneToOne);
+    
+    OneToOne oto = (OneToOne) addressProperty.getValue();
+    
+    assertEquals(oto.getColumnSpan(),2);
+    assertEquals("MultiPerson", oto.getEntityName());
+    assertEquals("AddressMultiPerson", oto.getReferencedEntityName());
+    assertFalse(oto.isConstrained());
+    
+    assertEquals(2, person.getPropertyClosureSpan());    
+    assertEquals("compositeid gives generic id name", "id", person.getIdentifierProperty().getName());
+    
+    PersistentClass addressPerson = localCfg.getClassMapping("AddressMultiPerson");
+    
+    
+    Property personProperty = addressPerson.getProperty("multiPerson");
+    assertNotNull(personProperty);
+    
+    assertTrue(personProperty.getValue() instanceof OneToOne);
+    
+    oto = (OneToOne) personProperty.getValue();
+    
+    assertEquals(oto.getColumnSpan(),2);
+    assertEquals("AddressMultiPerson", oto.getEntityName());
+    assertEquals("MultiPerson", oto.getReferencedEntityName());
+    
+    assertEquals(2, addressPerson.getPropertyClosureSpan());
+    assertEquals("compositeid gives generic id name","id", addressPerson.getIdentifierProperty().getName());
+    assertTrue(oto.isConstrained());
+  }
+  
+
+  public void xtestNoCreation() {
+    
+     assertNull("No middle class should be generated.", cfg.getClassMapping( "WorksOn" ));
+    
+     assertNotNull("Should create worksontext since one of the foreign keys is not part of pk", cfg.getClassMapping( "WorksOnContext" ));
+    
+     PersistentClass projectClass = cfg.getClassMapping("Project");
+    assertNotNull( projectClass );
+
+    PersistentClass employeeClass = cfg.getClassMapping("Employee");
+    assertNotNull( employeeClass );
+        
+    assertPropertyNotExist( projectClass, "worksOns" );
+    assertPropertyNotExist( employeeClass, "worksOns" );
+    
+     Property property = employeeClass.getProperty( "projects" );
+    assertNotNull( property);
+    assertNotNull( projectClass.getProperty( "employees" ));        
+    
+  }
+  
+  public void testBuildMappings() {
+    
+    localCfg.buildMappings();
+  }
+  
+  public void testGenerateAndReadable() {
+    
+    cfg.buildMappings();
+    
+    HibernateMappingExporter hme = new HibernateMappingExporter(cfg, getOutputDir());
+    hme.start();    
+    
+    assertFileAndExists( new File(getOutputDir(), "Person.hbm.xml") );
+    assertFileAndExists( new File(getOutputDir(), "AddressPerson.hbm.xml") );
+    assertFileAndExists( new File(getOutputDir(), "AddressMultiPerson.hbm.xml") );
+    assertFileAndExists( new File(getOutputDir(), "MultiPerson.hbm.xml") );
+    
+    assertEquals(4, getOutputDir().listFiles().length);
+    
+    Configuration configuration = new Configuration()
+       .addFile( new File(getOutputDir(), "Person.hbm.xml") )
+       .addFile( new File(getOutputDir(), "AddressPerson.hbm.xml") )
+      .addFile( new File(getOutputDir(), "AddressMultiPerson.hbm.xml"))
+      .addFile( new File(getOutputDir(), "MultiPerson.hbm.xml"));
+    
+    configuration.buildMappings();
+    
+  }
+  
+
+  private void assertPropertyNotExist(PersistentClass projectClass, String prop) {
+    try {
+      projectClass.getProperty(prop);
+      fail("property " + prop + " should not exist on " + projectClass);
+    } catch(MappingException e) {
+      // expected
+    }
+  }
+  
+  protected String[] getCreateSQL() {
+    return new String[] {
+      "create table PERSON ( person_id integer not null, name varchar(50), primary key (person_id) )",
+      "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)",
+    };
+  }
+
+  protected String[] getDropSQL() {
+    return new String[] {
+        "drop table ADDRESS_PERSON",
+        "drop table PERSON",
+        "drop table ADDRESS_MULTI_PERSON",
+        "drop table MULTI_PERSON",
+        
+
+      };
+  }
+
+}

Modified: branches/Branch_3_2/HibernateExt/tools/src/testsupport/NoopReverseEngineeringStrategy.java
===================================================================
--- branches/Branch_3_2/HibernateExt/tools/src/testsupport/NoopReverseEngineeringStrategy.java  2008-08-01 17:10:49 UTC (rev 15014)
+++ branches/Branch_3_2/HibernateExt/tools/src/testsupport/NoopReverseEngineeringStrategy.java  2008-08-03 16:04:47 UTC (rev 15015)
@@(protected) @@
   // TODO Auto-generated method stub
   return null;
 }
+  
+  public boolean isOneToOne(ForeignKey foreignKey) {
+    // TODO Auto-generated method stub
+    return false;
+  }
}

_______________________________________________
hibernate-commits mailing list
hibernate-commits@(protected)
https://lists.jboss.org/mailman/listinfo/hibernate-commits
©2008 gg3721.com - Jax Systems, LLC, U.S.A.