-->
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.  [ 6 posts ] 
Author Message
 Post subject: cascade question
PostPosted: Mon Feb 16, 2004 1:16 pm 
Regular
Regular

Joined: Tue Oct 14, 2003 11:11 pm
Posts: 62
Location: Brasil/Curitiba
I was reading about cascades in section 9.3...

I've defined a parent / child relationship between Team and Player:

In this test class, only player p1 is saved. This happens because the player identifier is null, and the equals method in PlayerVO class returns "true" when it compares p1 and p2 instances. Then p2 instance isn't added to the collection.

My equals and hascode methods were created by Hibernate CodeGenerator.

please anybody can help me?

thanks

Code:
public boolean equals(Object other) {
        if ( !(other instanceof PlayerVO) ) return false;
        PlayerVO castOther = (PlayerVO) other;
        return new EqualsBuilder()
            .append(this.getId(), castOther.getId())
            .isEquals();
    }


Code:
public static void main(String[] args) throws Exception
   {
      SessionFactory sf = getSessionFactory();

      Session session = sf.openSession();

      TeamVO team = new TeamVO();
      team.setCity("curitiba");
      team.setName("Timao");
      team.setPlayers(new HashSet());

      PlayerVO p1 = new PlayerVO("Ricardo","Lecheta");
      addPlayer(team, p1);

      PlayerVO p2 = new PlayerVO("Carlos","Tosin");
      addPlayer(team, p2);

      session.save(team);

      close(session);
   }

   private static void addPlayer(TeamVO team, PlayerVO p1)
   {
      p1.setTeam(team);
      team.getPlayers().add(p1);
   }


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="hibernate.TeamVO" table="teams" proxy="hibernate.TeamVO">
      
      <id name="id" column="team_id" type="long" unsaved-value="null">
         <generator class="sequence">
                <param name="sequence">team_sequence</param>
         </generator>
      </id>

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

      <set name="players" cascade="all" inverse="false" lazy="true">
         <key>
            <column name="team_id" not-null="true"/>
         </key>
         <one-to-many class="hibernate.PlayerVO" />
      </set>
   </class>

</hibernate-mapping>


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="hibernate.PlayerVO" table="players">

    <id name="id" type="long" column="player_id" unsaved-value="null">
        <generator class="sequence">
                <param name="sequence">player_sequence</param>
        </generator>
   </id>
    <property name="firstName" column="first_name" type="string" length="12" not-null="true"/>
   <property name="lastName" column="last_name" type="string" length="15" not-null="true"/>

   <many-to-one name="team" class="hibernate.TeamVO" column="team_id"  insert="true"/>
  </class>

</hibernate-mapping>

_________________
Ricardo Lecheta


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2004 1:17 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Take a look at http://www.hibernate.org/109.html please


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2004 1:40 pm 
Regular
Regular

Joined: Tue Oct 14, 2003 11:11 pm
Posts: 62
Location: Brasil/Curitiba
gloeglm wrote:
Take a look at http://www.hibernate.org/109.html please


thanks.

Hibernate suports the meta-attribute in the mapping file, like "use-in-tostring" witch include this property in the toString() method. Shouldn't be a "use-in-equals" attribute ? :-)

I like to use CodeGenerator to generate my classes, but sometimes is more useful use XDoclet, what's the recommended?

thanks.

_________________
Ricardo Lecheta


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2004 1:42 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Quote:
Hibernate suports the meta-attribute in the mapping file, like "use-in-tostring" witch include this property in the toString() method. Shouldn't be a "use-in-equals" attribute ? :-)


You are welcome to submit a patch adding it.

Quote:
I like to use CodeGenerator to generate my classes, but sometimes is more useful use XDoclet, what's the recommended?


That really depends on your preference, I personally prefer to write my Java code and let XDoclet generate the mappings.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2004 3:19 pm 
Regular
Regular

Joined: Tue Oct 14, 2003 11:11 pm
Posts: 62
Location: Brasil/Curitiba
I've submitted a patch to JIRA:

http://opensource.atlassian.com/project ... key=HB-717

thanks

_________________
Ricardo Lecheta


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2004 3:26 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Great, thanks :) But if possible please repost in a diff format, like produced by diff -u on Linux.


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