-->
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.  [ 7 posts ] 
Author Message
 Post subject: Why TransientObjectException?
PostPosted: Tue Dec 23, 2003 12:28 pm 
Beginner
Beginner

Joined: Wed Dec 17, 2003 10:13 am
Posts: 33
Still struggling with Nick Heudecker's "Player/Team" example.

I've got a main that tries to instantiate instances of Player and Team and then persist them to an Oracle 9i database. I can see all positive messages flying by in my console window until I try to commit my transaction. Then I get this:

Code:
net.sf.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: example.Team


When I log into Oracle and query those tables, there are no rows saved.

What am I missing?

Here's the main method that exercises Hibernate:

Code:
    public static void main(String [] args)
    {
        if (args.length > 0)
        {
            Session session         = null;
            Transaction tx          = null;
            SessionFactory factory  = null;

            try
            {   
                // configure Hibernate session
                Configuration config = new Configuration();
               
                config.addClass(example.Player.class);             
                config.addClass(example.Team.class);
               
                factory = config.buildSessionFactory();               
                session = factory.openSession();

                tx = session.beginTransaction();

                // create a new player
                Player p = new Player();
                Team team = new Team();
                p.setFirstName((args.length > 0) ? args[0] : "");
                p.setLastName((args.length > 1) ? args[1] : "");
                p.setDraftDate((args.length > 2) ? dateFormatter.parse(args[2]) : new Date());
                p.setAnnualSalary((args.length > 3) ? Float.parseFloat(args[3]) : 0.0f);
                p.setJerseyNumber((args.length > 4) ? Integer.parseInt(args[4]) : 0);
                team.setCity((args.length > 5) ? args[5] : "Boston");
                team.setName((args.length > 6) ? args[6] : "Celtics");               
                p.setTeam(team);
                team.addPlayer(p);

                System.out.println("player: " + p);
                System.out.println("team: " + team);
               
                session.saveOrUpdate(team);                                             
                session.saveOrUpdate(p);

                tx.commit();
                                                           
            }
            catch (Exception e)
            {
                if (tx != null)
                {
                     try { tx.rollback(); } catch (Exception ignore) { ; }
                }
                e.printStackTrace();
            }
            finally
            {
                try
                {
                    java.sql.Connection connection = session.close();
               
                    if (connection != null)
                        connection.close();
               
                    factory.close();
                }
                catch (Exception ignore)
                {
                    // do nothing, you've done your best
                }               
            }
        }
    }


I've posted on this topic in other threads:

http://forum.hibernate.org/viewtopic.ph ... highlight=
http://forum.hibernate.org/viewtopic.ph ... highlight=
http://forum.hibernate.org/viewtopic.ph ... highlight=

I'm making progress, thanks to the forum's help. Sorry that it's so slow. - MOD

_________________
MOD


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 23, 2003 1:09 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Check your team mapping. Consider using cascade="save-update" on the one-to-many to player to allow hibernate to save players when saving team.

Also consider using inverse="true" since you have a many-to-one on the other side. Have a look at http://www.hibernate.org/Documentation/InsideExplanationOfInverseTrue

PS: this is far easier with the mapping.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 23, 2003 2:34 pm 
Beginner
Beginner

Joined: Wed Dec 17, 2003 10:13 am
Posts: 33
Thank you, Emmanuel, both for watching and for your help.

Still failed. I've got to be missing something else in my mapping.

I'm not sure I understand your PS. I'm following your advice from a previous thread and using XDoclet and Ant to regenerate my hbm.xml every time. What do you mean by "easier with the mapping"? Should I abandon XDoclet until this works? Are you suggesting that I modify the hbm.xml mappings directly? - MOD

_________________
MOD


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 23, 2003 2:35 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
No, it's easier for me to help with the mapping in the post. I have a mapping to issue translator in brain ;-)

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 23, 2003 3:11 pm 
Beginner
Beginner

Joined: Wed Dec 17, 2003 10:13 am
Posts: 33
I'm sorry - I'm not thinking clearly.

I did one experiment that makes me feel better. I removed the one-to-many and inverse relationships from both classes, altered the tables to eliminate the foreign key constraints, and re-ran the code to see if I could persist Player and Team objects. Both were successful (thank God).

As you're suggesting, it's my mistake with the one-to-many and inverse relationships that's causing the problem.

I've put back those XDoclet tags and regenerated the hbm.xml mappings:

example.Players.hbm.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="example.Player"
        table="players"
        dynamic-update="false"
        dynamic-insert="false"
    >

        <id
            name="id"
            column="player_id"
            type="long"
            unsaved-value="null"
        >
            <generator class="sequence">
            </generator>
        </id>

        <property
            name="annualSalary"
            type="float"
            update="true"
            insert="true"
            column="annual_salary"
        />

        <property
            name="draftDate"
            type="date"
            update="true"
            insert="true"
            column="draft_date"
        />

        <property
            name="firstName"
            type="string"
            update="true"
            insert="true"
            column="first_name"
            length="15"
        />

        <property
            name="jerseyNumber"
            type="integer"
            update="true"
            insert="true"
            column="jersey_number"
            length="1"
        />

        <property
            name="lastName"
            type="string"
            update="true"
            insert="true"
            column="last_name"
            length="15"
            not-null="true"
        />

        <many-to-one
            name="team"
            class="example.Team"
            cascade="save-update"
            outer-join="auto"
            update="true"
            insert="true"
            column="team_id"
        />

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

    </class>

</hibernate-mapping>


example.Teams.hbm.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="example.Team"
        table="teams"
        dynamic-update="false"
        dynamic-insert="false"
    >

        <id
            name="id"
            column="team_id"
            type="long"
            unsaved-value="null"
        >
            <generator class="sequence">
            </generator>
        </id>

        <property
            name="city"
            type="string"
            update="true"
            insert="true"
            column="city"
            length="15"
            not-null="true"
        />

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

        <set
            name="players"
            table="players"
            lazy="true"
            inverse="true"
            cascade="all"
            sort="unsorted"
        >

              <key
                  column="player_id"
              />

              <one-to-many
                  class="example.Player"
              />
        </set>

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

    </class>

</hibernate-mapping>


I reviewed the documentation more carefully this time, because I believe it worked. If you spot anything else that I ought to correct, I'd appreciate hearing about it.

One question: the players table has a foreign key to the teams table, but the inverse is NOT true. Correct? (Makes sense for one-to-many, I guess.)

But thanks again for your help and patience. Sincerely, MOD

_________________
MOD


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 24, 2003 4:29 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
You screwed the set key. It must refer the "parent" object id
Code:
<set
            name="players"
            table="players"
            lazy="true"
            inverse="true"
            cascade="all"
            sort="unsorted"
        >

              <key
                  column="player_id"
              />


must be replaced by

Code:
<set
            name="players"
            table="players"
            lazy="true"
            inverse="true"
            cascade="all"
            sort="unsorted"
        >

              <key
                  column="team_id"
              />


Quote:
One question: the players table has a foreign key to the teams table, but the inverse is NOT true. Correct? (Makes sense for one-to-many, I guess.)

Correct

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 24, 2003 9:12 am 
Beginner
Beginner

Joined: Wed Dec 17, 2003 10:13 am
Posts: 33
Ack! Thank you again, Emmanuel. My best to you for the holidays. Sincerely, MOD

_________________
MOD


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