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>