-->
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.  [ 5 posts ] 
Author Message
 Post subject: constrained attribute on one-to-one mappings
PostPosted: Fri Dec 05, 2003 12:24 pm 
Beginner
Beginner

Joined: Mon Sep 08, 2003 6:52 am
Posts: 46
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;
}
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 05, 2003 12:25 pm 
Beginner
Beginner

Joined: Mon Sep 08, 2003 6:52 am
Posts: 46
Obviously, I commented stuff out and tested the IntThing and the UUIDThing
individually.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 05, 2003 12:50 pm 
Beginner
Beginner

Joined: Mon Sep 08, 2003 6:52 am
Posts: 46
Urk, sorry about the formatting.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 05, 2003 2:07 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
constrained true means that you need to add the other side first.
not-null on a many-to-one means you need to add the other side first.

Hard stuff.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 05, 2003 3:14 pm 
Beginner
Beginner

Joined: Mon Sep 08, 2003 6:52 am
Posts: 46
Okay, cool. But one-to-one with int id's don't work either way, when not-null is true...

H


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.