I have a one to one mapping in a class, Thing, that maps to IntThing and UUIDThing.
IntThing and UUIDThing both define a many-to-one mapping back to Thing.
Int thing uses "native" generator for id. UUID uses "uuid.hex" generator for id.
I would have thought they'd behave the same, but they don't.
I've built a truth table for each, where I changed the values of the "constrained" attribute
on the one-to-one mapping and the "not-null" attribute on the many-to-one mapping.
For UUID thing:
Constrained = true | Constrained = false
-------------------------------------------------------
Not-Null |
true failed | sucess
-------------------------------------------------------
Not-Null |
false success | success
-------------------------------------------------------
For Int thing:
Constrained = true | Constrained = false
-------------------------------------------------------
Not-Null |
true failed | failed
-------------------------------------------------------
Not-Null |
false success | failed
-------------------------------------------------------
Now, I'm obviously a bit confused about exactly what the "constrained" flag does
exactly, but the above chart shows that regardless of what constrained is set to,
you can't have a one-to-one mapping to an entity with an int id type that has
a not-null constraint back to the parent.
(As a side note, I'm pretty sure I had a similar error with a one-to-many
mapping with an entity with an int id type with a non-null constraint back to
the parent).
I'll include the mapping file and the Java files below.
I'd be happy to bundle this up and submit it to JIRA.
Should I attach each file individually, or make a tar file, or a zip/jar file?
I should probably mention that I am using MySQL 4.0.14-standard.
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="test.Thing" table="thing">
<id name="id" type="string" unsaved-value="null" >
<generator class="uuid.hex"/>
</id>
<one-to-one name="intThing"
class="test.IntThing"
constrained="false"
outer-join="false"
property-ref="thing"
cascade="save-update"/>
<!-- one-to-one name="UUIDThing"
class="test.UUIDThing"
constrained="false"
outer-join="false"
property-ref="thing"
cascade="save-update"/ -->
</class>
<class name="test.UUIDThing" table="uuidthing" dynamic-update="true" lazy="true">
<id name="id" type="string" unsaved-value="null" >
<generator class="uuid.hex"/>
</id>
<many-to-one name="thing" class="test.Thing"
column="thingid" not-null="false"/>
</class>
<class name="test.IntThing" table="otherthing" dynamic-update="true" lazy="true">
<id name="id" type="int" unsaved-value="0" >
<generator class="native"/>
</id>
<many-to-one name="thing" class="test.Thing"
column="thingid" not-null="false"/>
</class>
</hibernate-mapping>
---- PropertyRefTest.java
package test;
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;
public class PropertyRefTest
{
public static void main(String[] args) throws Exception
{
SessionFactory factory = new Configuration().configure().buildSessionFactory();
Session session = factory.openSession();
Transaction transaction = session.beginTransaction();
Thing thing = new Thing();
IntThing intThing = new IntThing();
UUIDThing uuidThing = new UUIDThing();
intThing.setThing(thing);
thing.setIntThing(intThing);
//uuidThing.setThing(thing);
//thing.setUUIDThing(uuidThing);
session.save(thing);
transaction.commit();
session.flush();
session.close();
}
}
--- Thing.java
package test;
public class Thing
{
private String id;
private IntThing intThing;
private NullThing nullThing;
private UUIDThing UUIDThing;
public String getId()
{
return id;
}
public void setId(String id)
{
this.id=id;
}
public IntThing getIntThing()
{
return intThing;
}
public void setIntThing(IntThing intThing)
{
this.intThing=intThing;
}
public UUIDThing getUUIDThing()
{
return UUIDThing;
}
public void setUUIDThing(UUIDThing UUIDThing)
{
this.UUIDThing=UUIDThing;
}
public NullThing getNullThing()
{
return nullThing;
}
public void setNullThing(NullThing nullThing)
{
this.nullThing=nullThing;
}
}
---------- IntThing.java
package test;
public class IntThing
{
private int id;
private Thing thing;
public int getId()
{
return id;
}
public void setId(int id)
{
this.id=id;
}
public Thing getThing()
{
return thing;
}
public void setThing(Thing otherThing)
{
this.thing=otherThing;
}
}
--- UUIDThing.java
package test;
public class UUIDThing
{
private String id;
private Thing thing;
public String getId()
{
return id;
}
public void setId(String id)
{
this.id=id;
}
public Thing getThing()
{
return thing;
}
public void setThing(Thing otherThing)
{
this.thing=otherThing;
}
}
|