-->
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.  [ 2 posts ] 
Author Message
 Post subject: many-to-one confusion
PostPosted: Fri Nov 21, 2003 3:31 pm 
Newbie

Joined: Tue Nov 11, 2003 6:24 pm
Posts: 17
i know this stuff is documented but i'm still having a few conceptual problems i think.

i have a table visitor with a column called role_type_id whose values belong to a table called role_type and have a foreign key constraint. i realize this is really just an object reference in the OO side of things.

i don't ever expect to work with a RoleType in my application since they are static so the relationship is not bi-directional.

i'm definitely doing something wrong when i save since i get:
javax.ejb.EJBException: nested exception is: net.sf.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.usg.domain.RoleType

my client code looks something like this and in this case i'm assuming that the column role_type_id in the parent table would become null.

Code:
        Visitor visitor = new Visitor();
        RoleType roleType = new RoleType();
...
        visitor.setRoleType(roleType);
..
// saving:
            session = SessionHelper.getSession();
            session.saveOrUpdate(visitor);
           
            session.flush();           
            session.close();




anyway here's my XML:
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.usg.domain.Visitor"
    table="VISITOR"
>

    <id
        name="visitorId"
        type="java.lang.Integer"
        column="VISITOR_ID"
    >
        <generator class="sequence">
            <param name="sequence">visitor_id</param>
        </generator>
    </id>

    <!-- associations -->
    <!-- uni-directional many-to-one association to RoleType -->
    <many-to-one
        name="roleType"
        class="com.usg.domain.RoleType"
    >
        <column name="ROLE_ID" />
    </many-to-one>

</class>
</hibernate-mapping>

RoleType:
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.usg.domain.RoleType"
    table="ROLE_TYPE"
>

    <id
        name="roleId"
        type="java.lang.Integer"
        column="ROLE_ID"
    >
        <generator class="sequence">
            <param name="sequence">role_type_id</param>
        </generator>
    </id>

    <property
        name="description"
        type="java.lang.String"
        column="DESCRIPTION"
        not-null="true"
        length="50"
    />

    <!-- associations -->

</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 21, 2003 6:03 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Enable cascade save-update, I suppose.


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