-->
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: not understanding doc: exception from Session.update
PostPosted: Thu Nov 18, 2004 6:23 pm 
Newbie

Joined: Thu Nov 18, 2004 6:01 pm
Posts: 17
Hibernate version:
2.1

Code between sessionFactory.openSession() and session.close():
Code:
      session = getSession();
      transaction = session.beginTransaction();
      
      /*
       * RouteKey is a composite-id.  newRouteKey method instantiates
       * new RouteKey and populates all fields.  Parameter is used to generate
       * field values and so one RouteKey can be made unique from others.
       */
      RouteKey rk1A = newRouteKey("R1");
      Route r1A = (Route)session.load(Route.class, rk1A);

      /*
       * newRoute method instantiates new Route and populates all fields.
       * Parameter is used to generate field values and so one Route can be made
       * unique from others.
       */
      Route r1B = newRoute("R1");

      /*
       * r1A and r1B are equal but different instances of Route
       */

      // no exception thrown here: confused
      session.update(r1B);

      transaction.commit();   
      session.beginTransaction();
      


Name and version of the database you are using:
hsqldb


I'm confused about the statement in the API doc for Session.update. Here is quote:

Quote:
Update the persistent instance with the identifier of the given transient instance. If there is a persistent instance with the same identifier, an exception is thrown.


In the code segment I provide, I think an exception would be thrown when update is called on the second instance. I got this wrong, didn't I. What does the API mean then?

Thanks,
John


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 19, 2004 4:09 am 
Regular
Regular

Joined: Tue Jan 27, 2004 12:22 pm
Posts: 103
In rlA you have a composite key. In rlB you have a String instance as your key.

_________________
Dencel
- The sun has never seen a shadow -


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 19, 2004 10:55 am 
Newbie

Joined: Thu Nov 18, 2004 6:01 pm
Posts: 17
Sorry, no, the only identifier is RouteKey. This is not clear probably because I left off some clarifying information and I'm sorry for that. I was hoping to get away without providing the following. Here is the mapping file for Route class.

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

<hibernate-mapping>
    <class name="model.Route" table="route" >
     <composite-id name="key" class="model.RouteKey" >
       <key-property name="f1" type="java.lang.String" column="F1" length="31" />
       <key-property name="f2" type="java.lang.String" column="F2" length="31" />
       <!-- more key-properties, about 15 -->
     </composite-id>

     <property name="f16" type="java.lang.String" column="F10" length="23" />
     <property name="f17" type="int" column="F11" />
    </class>
</hibernate-mapping>


So, you can see that the identifier of the Route is always the composite RouteKey.

The string parameter passed into the newRouteKey and newRoute methods is only used to generate values for the fields. They are not identifiers. For example:
Code:
private RouteKey newRouteKey(String s) {
    RouteKey k = new RouteKey();
    k.setF1 = s + "F1";
    k.setF2 = s + "F2";
    // and so forth
    return k;
}

and then
Code:
private Route newRoute(String s) {
    Route r = new Route();
    r.setF1 = s + "F1";
    r.setF2 = s + "F2";
    // and so forth
    r.setF16 = s + "F16";
    return r;
}

Of course this code is not doing anything useful exept provide data while I try to ramp up on the hibernate interfaces.

Here are class definitions of Route and RouteKey so that you could see how they relate:
Code:
class RouteKey implements Serializable {
  private String f1;
  // and so on

  public String getF1() {return f1;}
  public void setF1(String s) {f1 = s; }

  // andso on
}

class Route {
  // key fields data members and accessors
  private RouteKey key;

  public String getF1() {return key.getF1(); }
  public void setF1(String s) {key.setF1(s); }

  // and so on


  // non-key fields data members and  accessors
  private String f16;
  private int f17;

  public String getF16() {return f16;}
  public void setF16(String s) {f16 = s;}




}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 19, 2004 11:08 am 
Regular
Regular

Joined: Tue Jan 27, 2004 12:22 pm
Posts: 103
Implement equals() and hashCode() in RouteKey. This is documented in 4.3 of the Hibernate documentation.

_________________
Dencel
- The sun has never seen a shadow -


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 19, 2004 12:13 pm 
Newbie

Joined: Thu Nov 18, 2004 6:01 pm
Posts: 17
I have implemented equals and hashcode. Sorry for not including them in the example. I omitted them from the posted code because I'm extracting from non-public code and I don't think the implementation of these methods are relevant to the question. Please correct me.

The problem isn't that code doesn't work. The problem is that I expect it not to, but it does.

The code in my initial post runs without an exception. I want to understand the meaning of the API doc that states an exception would be thrown. My code in the initial post is my attempt at generating the exception identified in the API doc. Since there is no exception, I think I misunderstand the document. So, what is the condition that would throw the exception? Can you provide a code sample that would cause this exception?


John


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.