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: one-to-one mapping will only do update
PostPosted: Wed Feb 04, 2004 5:24 pm 
Regular
Regular

Joined: Mon Nov 03, 2003 6:10 am
Posts: 75
I have been trying unsuccessfully to get a cascading insert to work with one to one mapping.

Error: The system always does an update on the child object.. I need it to create the object. I am obviously missing something on the foreign key


Hibernate: insert into TelcomNumber (formattedNumber, TelcomNumberId) values (?, ?)

Hibernate: update TelcomNumberGroup set areaCityCode=?, extension=?, internationalCountryCode=?, nationalNumber=?, subscriberNumber=? where TelcomNumberGroupId=?





Telephone extends TelcomNumber{
// sets type = TELEPHONE
}

Fax extends TelcomNumber{
// set type = FAX
}

TelcomNumber {

Long telcomNumberId;
String formattedNumber;
TelcomNumberGroup telcomNumberGroup;
String type;

}

TelcomNumberGroup{
String areaCode;
String subscriberCode;
String nationCode;
}


-------

<?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.test.poc.Telephone"
table="TelcomNumber"
dynamic-update="false"
dynamic-insert="false"
>

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

<one-to-one name="telcomNumberGroup" class="com.test.poc.TelcomNumberGroup" cascade="all" constrained="true"/>

<property
name="formattedNumber"
type="java.lang.String"
update="true"
insert="true"
column="formattedNumber"
not-null="false"
/>

</class>

</hibernate-mapping>

----------


<?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.test.poc.TelcomNumberGroup"
table="TelcomNumberGroup"
dynamic-update="false"
dynamic-insert="false"
>

<id column="TelcomNumberGroupId" name="id" type="java.lang.Long">
<generator class="foreign">
<param name="property">Telephone</param>
</generator>
</id>

<!-- <one-to-one name="Telephone" class="com.test.poc.Telephone" constrained="true"/>

// Don't want it mapped back to telephone because it could actually be of type Fax, Telephone, Mobile etc etc.
The reason we are doing this is because there is a XML component where the xsd defines these types already.. even tho in theory they are exactly the same except for the type property which we will imply.

-->

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

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

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

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

<property
name="subscriberNumber"
type="java.lang.String"
update="true"
insert="true"
column="subscriberNumber"
not-null="true"
/>


</class>

</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2004 8:02 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
You'r misusing foreign

foreign geenrator mean the objet is dependant from the associated one.

In *this* object, the <one-to-one> must be constrained="true", not in the other side.

_________________
Emmanuel


Top
 Profile  
 
 Post subject: contrained true
PostPosted: Thu Feb 05, 2004 12:26 am 
Regular
Regular

Joined: Mon Nov 03, 2003 6:10 am
Posts: 75
I only have the one-to-one attribute in the parent object (Telephone)

The entire relation is commented out in the child (TelcomNumberGroup)

Troy


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 05, 2004 8:28 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
If your child id is dependant from it's parent, then you need to set the one-to-many.
Otherwise, you cannot use foreign

_________________
Emmanuel


Top
 Profile  
 
 Post subject: one 2 one
PostPosted: Thu Feb 05, 2004 12:22 pm 
Regular
Regular

Joined: Mon Nov 03, 2003 6:10 am
Posts: 75
I thought you could do a one to one relation of the sort:

Class Foo {

private Bar bar;
private String name;
}

Class Bar{
private String desc;
private int count;
}


table looks like:

FOO { fooid, barid, name }

BAR{ barid, desc, count }

Now if I create a Foo object and add a Bar I just want it to work.
Currently it always tries to just UPDATE the Bar object. But it doesn't exist yet... it should create it instead.

p.s. I know looking at this it is basically would support the one to many relation...

so say the table structure was like this instead...

FOO( fooid, name )
BAR (fooid, desc )

How would each of these 2 examples be set up to allow for cascade to work. i.e. 2 inserts vs an insert (foo) and update (bar) as currently occurs.


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.