[hibernate-issues] [Hibernate-JIRA] Commented: (HBX-524) Reverse of one-to-one relationships

Marcio Carvalho (JIRA) noreply at atlassian.com
Thu Jul 17 12:42:47 EDT 2008


    [ http://opensource.atlassian.com/projects/hibernate/browse/HBX-524?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_30685 ] 

Marcio Carvalho commented on HBX-524:
-------------------------------------

Ok Max.

I really implemented the aforementioned PK-FK LAW #2, which is the standard way to express one-to-one relations here in my job.

I've just changed the JDBCBinder class. Here are my changes:

1- method bindIncomingForeignKeys(): I inserted another condition asking if the current FK is a OneToOne relation. In case it is, I call the new method bindOneToOne() which is similar to bindOneToMany().

2- method isOneToOne(ForeignKey fk): I created this method to check whether the referred fk represents a OneToOne relation according to the PK-FK Law #2.

    protected boolean isOneToOne(ForeignKey foreignKey) {
        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;
    }


3- method bindOneToOne(): I created this method to actually compose the Property which has an org.hibernate.mapping.OneToOne value. It mostly resembles the bindOneToMany method.

    private Property bindOneToOne(PersistentClass rc, Table table,
            ForeignKey fk, Set processedColumns) {

        OneToOne value = new OneToOne(table, rc);
        value.setReferencedEntityName(revengStrategy
                .tableToClassName(TableIdentifier.create(table)));

        boolean isUnique = isUniqueReference(fk);
        String propertyName =
                revengStrategy.foreignKeyToEntityName(fk.getName(),
                        TableIdentifier.create(fk.getReferencedTable()), fk
                                .getReferencedColumns(), TableIdentifier
                                .create(table), 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);
        return makeProperty(TableIdentifier.create(table), propertyName, value,
                true, true, value.getFetchMode() != FetchMode.JOIN, null, null);
    }


This are mostly the changes I made to recognize One-To-One relationships. To express this situation in my pojos, I 've put the correct annotations in my pojo templates.

Hope this helps.

Cheers,

Marcio


> Reverse of one-to-one relationships
> -----------------------------------
>
>                 Key: HBX-524
>                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HBX-524
>             Project: Hibernate Tools
>          Issue Type: Bug
>          Components: reverse-engineer
>    Affects Versions: 3.1beta2
>         Environment: HIbernate 3.1, Oracle 9i
>            Reporter: Andrea Cattani
>
> Hi,
> I've posted this issue to the forum and got this response from Max, Hibernate Team:
> "the reveng tools does not detect this as a one-to-one. it probably could, so add a request/patch to jira."
> The problem I've faced is the following:
> I have two tables, let's say
> - table A with column ID (PK) and other fields
> - table B with column ID (PK) and other fields
> table B has a foreign key constraint against table A, from column ID to column ID (one-to-one)
> When I reverese the tables with the HibernateTools I have such a resultant mapping for table B:
> <class name="B" table="B" schema="SCHEMA">
> <id name="id" type="string">
> <column name="ID" length="12" />
> <generator class="assigned" />
> </id>
> <[b]many-to-one name[/b]="a" class="A" update="false" insert="false" fetch="select">
> <column name="ID" length="12" not-null="true" unique="true" />
> </many-to-one>
> ....
> And this one for table A:
> <class name="A" table="A" schema="SCHEMA">
> <id name="id" type="string">
> <column name="ID" length="12" />
> <generator class="assigned"/>
> </id>
> <set name="b" inverse="true">
> <key>
> <column name="ID" length="12" not-null="true" unique="true" />
> </key>
> <[b]one-to-many[/b] class="B" />
> </set>
> </class>
> while I was expecting something like:
> [i]<one-to-one name="a" class="A" constrained="true"/>[/i]
> in table B, and the same (or nothing) in table A
> Thank you
> Andi

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        



More information about the hibernate-issues mailing list