-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: many-to-one relationship to non-PK field
PostPosted: Wed Oct 06, 2004 1:33 am 
Newbie

Joined: Thu Sep 23, 2004 4:11 am
Posts: 12
Location: Gold Coast, Australia
Hibernate version: 2.1.6

I'm trying to declare a foreign key relationship between two entities where the type is java.lang.String and the related entity field is not a primary key but is a unique field. If I attempt the same thing, but relating to a primary key field (by leaving off the property-ref attribute which defaults to the id), I have no problem.

I have narrowed down a test case in an attempt to solve this problem but to no avail. The problem is that 'schemaexport' will generate the two tables X and Y but will not create the relationship and I don't understand why (also, why it does the create the relationship when relating to a PK field).

Please find below two source files annotated with XDoclet and corresponding mapping files. Any assistance is most appreciated.

Code:
/**
* @hibernate.class table="X"
*/
public class X
{
    private Long id;
    private String uniqueField;

    public X()
    {
        this(null);
    }

    public X(Long id)
    {
        this.id = id;
    }

    /**
     * @hibernate.id generator-class="native"
     */
    public final Long getId()
    {
        return id;
    }

    /**
     * @hibernate.property
     * @hibernate.column name="uniqueField"
     */
    public final String getUniqueField()
    {
        return uniqueField;
    }
}


Code:
/**
* @hibernate.class table="Y"
*/
public class Y
{
    private Long id;
    private String field;

    public Y()
    {
        this(null);
    }

    public Y(Long id)
    {
        this.id = id;
    }

    /**
     * @hibernate.id generator-class="native"
     */
    public final Long getId()
    {
        return id;
    }

    /**
     * @hibernate.many-to-one name="field" class="X" property-ref="uniqueField"
     */
    public final String getField()
    {
        return field;
    }
}


Code:
<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
    <class
        name="com.tmits.web.re.schema.X"
        table="X"
        dynamic-update="false"
        dynamic-insert="false"
    >

        <id
            name="id"
            column="id"
            type="java.lang.Long"
        >
            <generator class="native">
            </generator>
        </id>

        <property
            name="uniqueField"
            type="java.lang.String"
            update="true"
            insert="true"
            access="property"
        >
            <column
                name="uniqueField"
            />
        </property>

        <!--
            To add non XDoclet property mappings, create a file named
                hibernate-properties-X.xml
            containing the additional properties and place it in your merge dir.
        -->

    </class>

</hibernate-mapping>


Code:
<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
    <class
        name="com.tmits.web.re.schema.Y"
        table="Y"
        dynamic-update="false"
        dynamic-insert="false"
    >

        <id
            name="id"
            column="id"
            type="java.lang.Long"
        >
            <generator class="native">
            </generator>
        </id>

        <property
            name="field"
            type="java.lang.String"
            update="true"
            insert="true"
            access="property"
            column="field"
        />

        <many-to-one
            name="field"
            class="com.tmits.web.re.schema.X"
            cascade="none"
            outer-join="auto"
            update="true"
            insert="true"
            access="property"
            property-ref="uniqueField"
            column="field"
        />

        <!--
            To add non XDoclet property mappings, create a file named
                hibernate-properties-Y.xml
            containing the additional properties and place it in your merge dir.
        -->

    </class>

</hibernate-mapping>

_________________
SCJP 1.4, SCJD
Software Engineer, IBM Australia


Top
 Profile  
 
 Post subject: Left off some information
PostPosted: Wed Oct 06, 2004 1:42 am 
Newbie

Joined: Thu Sep 23, 2004 4:11 am
Posts: 12
Location: Gold Coast, Australia
Realising I have left off some potentially important information.
- I forgot to annotate the uniqueField with unique="true".
After doing this, the result is the same (and also the corresponding mapping document was updated).
- I am using the Microsoft type 4 driver for MS SQL Server 2000.

_________________
SCJP 1.4, SCJD
Software Engineer, IBM Australia


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.