-->
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.  [ 4 posts ] 
Author Message
 Post subject: upgrade to hibernate 3.1 - saving object is not cascading as
PostPosted: Thu Oct 19, 2006 9:41 pm 
Newbie

Joined: Thu Oct 19, 2006 9:23 pm
Posts: 2
Hi all

I've recently upgraded to hibernate 3.1.3, and came across an issue I can't seem to figure out. I don't know if it's related to cascading or not. This code was previously working. The only difference is that I've gone from hibernate 3.0 to 3.1. Here's a piece of the code:

Code:
    Company company = new Company();
    dao.saveObject(company);  // this gives me a valid id generated by hibernate

   ...

    User user = new User();
    user.setCompany(company);
    dao.saveObject(user);   // my error is here


The error I get is
Code:
ERROR: insert or update on table "user" violates foreign key constraint "fk9501a78d45fc499f"
Detail: Key (company)=(111) is not present in table "company"


In my User object, the property for Company has cascade=save-update (many user to 1 company), so I would think that when it tries to save User, even if company has not been previously saved, it would attempt to cascade to the company and make it available.

Any thoughts would be greatly appreciated.

Hieu

Env:
Spring 1.2.7
Hibernate 3.1.3
Postgres 8.1[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 20, 2006 3:16 am 
Regular
Regular

Joined: Wed Jul 27, 2005 2:33 am
Posts: 118
Post your mapping files. Specify inverse=false on the many-to-one property for Company in your User hbm file.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 20, 2006 1:02 pm 
Newbie

Joined: Thu Oct 19, 2006 9:23 pm
Posts: 2
Thanks for the reply

Here's the hbm generated by xdoclet. The change didn't work, and it might be way the sql is being generated. I'm going to try see what the sql statements look like and see if it's an ordering issue.

Thanks ahead!

Code:
<?xml version="1.0" encoding="UTF-8"?>

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

<hibernate-mapping
>
    <class
        name="com.iloopmobile.davinci.model.User"
        table="User"
    >

        <id
            name="id"
            column="id"
            type="java.lang.Integer"
        >
            <generator class="identity">
              <!-- 
                  To add non XDoclet generator parameters, create a file named
                  hibernate-generator-params-User.xml
                  containing the additional parameters and place it in your merge dir.
              -->
            </generator>
        </id>

        <many-to-one
            name="company"
            class="com.iloopmobile.davinci.model.Company"
            cascade="save-update"
            outer-join="auto"
            update="true"
            insert="true"
            column="company"
        />

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

    </class>

</hibernate-mapping>




Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 22, 2006 9:10 am 
Regular
Regular

Joined: Wed Jul 27, 2005 2:33 am
Posts: 118
Quote:
Specify inverse=false on the many-to-one property for Company in your User hbm file


I'm sorry. I mentioned it on the wrong end. You will have to actually specify "inverse=false" on the collection mapping(one-to-many property) of your User objects in the Company.hbm file. Give it a try.


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