-->
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.  [ 32 posts ]  Go to page 1, 2, 3  Next
Author Message
 Post subject: Strange values on cascaded inserted id field...
PostPosted: Tue Nov 11, 2003 2:27 pm 
Newbie

Joined: Fri Nov 07, 2003 10:58 am
Posts: 14
Location: Albuquerque, NM
I sucessfully setup related tables to do a cascaded insert without a problem. Now I am setting up another one and the index field (<id> field) is getting STRANGE, WEIRD values. I am using Oracle 8.1, Hibernate (tested with both 2.0.3 and 2.1b6). The column is question is the primary key, a NUMBER(10). The underlying property is a Long, set to null (as it is a new object). The generator class is set to a sequence, pointing to a value Oracle sequence. No errors are being generated when I do the .save then .flush. I have also tried this with an "increment" but saw no difference.

An example of the value that is inserted is "7E-63" but the actual value changes every time.

I am absolutely mystified. Since it is doing an insert (show_sql shows me the insert happening) it is apparent that it knows this is a new row to insert. Nothing has changed in the vehcile object, so, no other updates are occuring (which is how it should be).

I am completely mystified. I cannot "see" what values are being inserted, is there some way to enable this? The sql that I see executing is...

To retrieve the data...
Code:
Hibernate: select vehicle0_.equipId as x0_0_ from ACES_EQUIPMENTLIST vehicle0_ where (tag=? )
Hibernate: select vehicle0_.equipId as equipId, vehicle0_.TAG as TAG, vehicle0_.TYPE as TYPE, vehicle0_.HOME as HOME, vehicle0_.SITE as SITE, vehicle0_.STATUS as STATUS, vehicle0_.SITE_UNDO as SITE_UNDO, vehicle0_.STATUS_UNDO as STATUS_U8_, vehicle0_.RETIRED as RETIRED, vehicle0_.TAG as f0_, vehicle0_.SITE as f1_, vehicle0_.SITE as f2_, vehicle0_.STATUS as f3_ from ACES_EQUIPMENTLIST vehicle0_ where vehicle0_.equipId=?
Hibernate: select aces_all0_.commentId as commentId__, user1_.userId as userId0_, user1_.USERNAME as USERNAME0_, user1_.FNAME as FNAME0_, user1_.LNAME as LNAME0_, user1_.HOMESITE as HOMESITE0_, user1_.SORTORDER as SORTORDER0_, aces_all0_.commentId as commentId1_, aces_all0_.EQUIPID as EQUIPID1_, aces_all0_.USERID as USERID1_, aces_all0_.COMMENTS as COMMENTS1_, aces_all0_.COMMENTS_DATE as COMMENTS5_1_ from ACES_ALLCOMMENTS aces_all0_, ACES_USERS user1_ where aces_all0_.EQUIPID=? and aces_all0_.USERID=user1_.userId(+) order by aces_all0_.COMMENTS_DATE DESC
Hibernate: select aces_use0_.roleId as roleId__, aces_use0_.roleId as roleId, aces_use0_.USERID as USERID, aces_use0_.ROLE as ROLE from ACES_USERROLES aces_use0_ where aces_use0_.USERID=?
Hibernate: select aces_use0_.roleId as roleId__, aces_use0_.roleId as roleId, aces_use0_.USERID as USERID, aces_use0_.ROLE as ROLE from ACES_USERROLES aces_use0_ where aces_use0_.USERID=?


To insert the new row
Code:
Hibernate: select aces_seq_comments_id.nextval from dual
Hibernate: insert into ACES_ALLCOMMENTS (EQUIPID, USERID, COMMENTS, COMMENTS_DATE, commentId) values (?, ?, ?, ?, ?)


I have tried stopping and starting Tomcat. I have tried everything I can think of.

Any help is appreciated. Code that I thought made sense to include is below.

Thanks,
Kevin

-----------

The .hbm.xml file
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class name="fmtnm.aces.entities.Vehicle" table="ACES_EQUIPMENTLIST">
  <id name="equipId">
    <generator class="sequence">
      <param name="sequence">aces_seq_equipment_id</param>
    </generator>
  </id>
  <property name="tag">
    <column name="TAG"/>
  </property>
  <property name="newTag" insert="false" update="false"
    formula="TAG"/>
  <property name="type" >
    <column name="TYPE"/>
  </property>
  <property name="home" >
    <column name="HOME"/>
  </property>
  <property name="site" >
    <column name="SITE"/>
  </property>
  <property name="siteOrig" insert="false" update="false"
    formula="SITE"/>
  <property name="prevSite" insert="false" update="false"
    formula="SITE"/>
  <property name="status" >
    <column name="STATUS"/>
  </property>
  <property name="statusOrig" insert="false" update="false"
    formula="STATUS"/>
  <property name="siteUndo" >
    <column name="SITE_UNDO"/>
  </property>
  <property name="statusUndo" >
    <column name="STATUS_UNDO"/>
  </property>
  <property name="retired" >
    <column name="RETIRED"/>
  </property>
  <bag name="comments" inverse="true"
    order-by="COMMENTS_DATE DESC" cascade="all-delete-orphan">
    <key column="EQUIPID"/>
    <one-to-many class="fmtnm.aces.entities.Comment"/>
  </bag>
</class>

<class name="fmtnm.aces.entities.Comment" table="ACES_ALLCOMMENTS">
  <id name="commentId">
    <generator class="sequence">
      <param name="sequence">aces_seq_comments_id</param>
    </generator>
  </id>
  <many-to-one name="equipment" class="fmtnm.aces.entities.Vehicle" column="EQUIPID"/>
  <many-to-one name="user" class="fmtnm.aces.entities.User" column="USERID"/>
  <property name="comment">
    <column name="COMMENTS"/>
  </property>
  <property name="date">
    <column name="COMMENTS_DATE"/>
  </property>
</class>
</hibernate-mapping>


Comment.java
Code:
package fmtnm.aces.entities;

public class Comment implements Serializable {
  private Long olCommentId = null;
  private Vehicle oEquipment = null;
  private User oUser = null;
  private Timestamp tsDate = null;
  private String sComment = "";

  public Comment() {}

  public Comment(User oUser,Vehicle oEquipment, String sComment) {
    this.oUser = oUser;
    this.oEquipment = oEquipment;
    this.sComment = sComment;
    tsDate = new Timestamp(System.currentTimeMillis());
  }

  public void setDate(Timestamp dtNewVal) {
    tsDate = dtNewVal;
  }
  public Timestamp getDate() {
    return (tsDate);
  }

  public String getComment() {
    return sComment;
  }
  public void setComment(String string) {
    sComment = string;
  }

  public Vehicle getEquipment() {
    return oEquipment;
  }

  public void setEquipment(Vehicle newval) {
    oEquipment = newval;
  }

  public User getUser() {
    return oUser;
  }
  public void setUser(User newval) {
    oUser = newval;
  }

  public Long getCommentId() {
    return olCommentId;
  }
  public void setCommentId(Long newval) {
    olCommentId = newval;
  }
}


Code where I add a vehicle
Code:
  session = HibernateFactory.getSessionFactory().openSession();
  Query query =
  session.createQuery("SELECT o FROM fmtnm.aces.entities.Vehicle o where tag=:tag");
  query.setString("tag", "E27979");
  for (Iterator it = query.iterate(); it.hasNext();) {
    Vehicle v = (Vehicle)it.next();
    User insertUser = (User)session.load(User.class,getCurUser().
      getUserId());
    v.addComment(new Comment(insertUser,v,"New Comment"));
    v.describe();
    session.update(v);
    session.flush();
  }
  session.close();


Snippit of Vehicle.java
Code:
package fmtnm.aces.entities;

public class Vehicle implements Serializable {
  private Long olEquipId = null;
  private Collection comments;
   
  public Collection getComments() {
    return (comments);
  }
  public void setComments(Collection comments) {
    this.comments = comments;
  }
  public void addComment(Comment comment) {
    comments.add(comment);
  }

  public Long getEquipId() {
    return olEquipId;
  }
  public void setEquipId(Long newval) {
    olEquipId = newval;
  }
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 11, 2003 3:15 pm 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
Just a guess: you do not specify property (and Id) types. According to doc, "If you do not specify a type, Hibernate will use reflection upon the named property to take a guess at the correct Hibernate type.".

But your vehicle does not have a property named "equipId"...


Top
 Profile  
 
 Post subject: Will try...
PostPosted: Tue Nov 11, 2003 3:21 pm 
Newbie

Joined: Fri Nov 07, 2003 10:58 am
Posts: 14
Location: Albuquerque, NM
Gives me some place to start (type). I believe I have tried it with explicit type but will try again.

BTW: I didn't include all of Vehicle.java (lots of properties) but if you look at Vehicle.java above it does have a property equipId (getEquipId, setEquipId are both there).


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 11, 2003 3:24 pm 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
It was "olEquipId", not "equipId". I do not know if Hibernate check return type of getter (which will succeed in your case) ot just checks type of the property (which will fail).


Top
 Profile  
 
 Post subject: Properties
PostPosted: Tue Nov 11, 2003 4:03 pm 
Newbie

Joined: Fri Nov 07, 2003 10:58 am
Posts: 14
Location: Albuquerque, NM
One of us is confused about how properties work... A property referred to is made up of 3 things, the (private, hopefully) class member variable, such as

Code:
   private Long olEquipId = null;


and its getter and setter methods

Code:
   public Long getEquipId() {
      return(olEquipId);
   }
   public Long setEquipId(Long newval) {
      olEquipId = newval;
   }


The properties interface to the world (and this should be including Hibernate) IS the getter and setter methods and as far as the world is concerned (including Hibernate) the property (equipId) is completely a black box. The internal implementation (the fact that I implemented this using a member variable of type Long with a name of olEquipId) should make no difference at all to Hibernate. It should, technically, not even be able to see that I have a member variable named olEquipId because that is private, it should be able to use reflection and see that I have a those methods defined and determine that I do have a property of type "long" named "equipId". I should be able to just as easily name the private member variable "xxxFish" and Hibernate should never care (as long as I correctly name my private member variables).


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 11, 2003 10:09 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
correct. Hibernate ignores the instance variable.


Top
 Profile  
 
 Post subject: Fixed my problem, Hibernate bug?
PostPosted: Tue Nov 11, 2003 10:57 pm 
Newbie

Joined: Fri Nov 07, 2003 10:58 am
Posts: 14
Location: Albuquerque, NM
I haven't completely narrowed down the problem but it appears that for a date property (DATE in the Oracle 8.1 database), if both the type="date" is missing and the underlying property is a java.sql.Timestamp, the <id> will (for me) get scrambled (as described in my original posting) when the row gets inserted into the database. This seems quite consistent on my system but a coworker doesn't "seem" to have this problem. My system was running JDK1.41, I am upgrading to JDK1.4.2 tomorrow to see if it behaves the same way (the only "obvious" difference between our systems). I also don't know if we are running all of the same versions of the support libraries, I may try to check this tomorrow, too. I believe I installed my system with, primarily, the Tapestry 3.0b3-related libraries and added the additional required Hibernate libraries.

When I added type="date" and changed the underlying property to type java.util.Date the problem completely cleared up. At least I am now able to get some work done.

I see this behavior with both Hibernate 2.0.3 and 2.1b6.

Kevin


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 11, 2003 11:09 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
We know about this bug in at least some versions of Oracle 9i driver (and possibly Oracle8 also). I believe that we found it could be fixed by upgrading the Oracle JDBC driver but I'm not 100% on this. Perhaps Max remembers.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 25, 2003 1:12 pm 
Beginner
Beginner

Joined: Wed Sep 10, 2003 5:34 pm
Posts: 36
Location: New York, NY
I can confirm that I'm having this problem as well with a hibernate property setting of "timestamp" and with the java object type being either java.util.Date or java.sql.Timestamp.

Did any combination of drivers or JDKs seem to produce either reliable failure or a fix to the problem?

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 25, 2003 1:36 pm 
Newbie

Joined: Fri Nov 07, 2003 10:58 am
Posts: 14
Location: Albuquerque, NM
Nope and Max (who might or might not remember the answer) has not decided to participate in this thead.

I have tried the latest JDK, I have downloaded the latest "classes12.zip" (renamed to .jar of course so Tomcat can recognize it) from Oracle.

The odd thing is that a co-worker is using java.sql.Timestamp with no issues. Sounds like java.sql.Timestamp is like playing russian roulette when it comes to Hibernate and Oracle.

My solution is to only use java.util.Date, which works just fine.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 25, 2003 1:43 pm 
Beginner
Beginner

Joined: Wed Sep 10, 2003 5:34 pm
Posts: 36
Location: New York, NY
I've even tried some Oracle 9 drivers and it still won't work, so for now I'm on Date type as well, although that solution obviously ain't sweet since I need times in there too.

I'll post here if I figure anything further out.


Top
 Profile  
 
 Post subject: Awkward solution that includes time
PostPosted: Tue Nov 25, 2003 6:34 pm 
Newbie

Joined: Fri Nov 07, 2003 10:58 am
Posts: 14
Location: Albuquerque, NM
This may work for you and it may not...

I have switched to using a Number(19) in the database, "Long" in my bean and using Timestamp.getTime() to retrieve the value that will be stored in the database (or new Timestamp(db_value)).

This solved my problem, may not help you. I sure wish java.sql.Timestamp worked.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 26, 2003 11:47 am 
Beginner
Beginner

Joined: Wed Sep 10, 2003 5:34 pm
Posts: 36
Location: New York, NY
I was thinking of doing something like that but it's not going to cut it for people who want to do SQL on the database independent of the application, so I'm still stuck.

Interestingly, I've found that if you alter the order of operations when inserting the record with the timestamp you can avoid the error. For example, if you insert the record without the timestamp, flush, then update the record to set the timestamp it will work.

Also, it seems that when I've looked at the SQL that's causing the errors, the ID that gets mangled follows immediately after the timestamp in the field list in the INSERT statement. Perhaps we could modify the order of fields in the INSERT and also avoid the error.

Finally I can confirm the error still occurs with the latest JDBC (9.2.0.3) drivers from Oracle, although I am running against an Oracle 8.1 database.

Matt


Top
 Profile  
 
 Post subject: Hmmm.... Inserting timestamp and record independently
PostPosted: Wed Nov 26, 2003 12:59 pm 
Newbie

Joined: Fri Nov 07, 2003 10:58 am
Posts: 14
Location: Albuquerque, NM
How are you seperating the insert of the record and the update of the date? Something like...

Code:
   if(o.getId() == null) {
      java.sql.Timestamp tmp = o.getTimestamp();
      o.setTimestamp(null);
      session.save(o);
      o.setTimestamp(tmp);
   }
   session.update(o);


?? And this worked? This would work for me if it works... (little kludgely, though).

Kevin


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 26, 2003 1:06 pm 
Beginner
Beginner

Joined: Wed Sep 10, 2003 5:34 pm
Posts: 36
Location: New York, NY
Something like that, however you need to flush after the first save. It only worked for me because I had one specific location I was testing it and that was the only place that object was inserted. For all my other objects that have timestamps I can't do that so it's not a real solution for me. For the record though, what I'm talking about looked like (not a real cut and paste since I erased my test, so it could have errors):

Code:
TheObject newObj = new TheObject();
new.setName("Test Name");
session.save(newObj);
Transaction t = session.beingTransaction();
session.flush();
t.commit();

newObj.setTimeStamp(new Date());
Transaction t = session.beingTransaction();
session.flush();
t.commit();


I think the point is that you need to perform the real insert to the database without the timestamp, then do a second update to get the timestamp in there.

Obviously this sucks for a general workaround, but I'm still working on a better fix so I'll let you know if I figure anything else out.

Matt[/code]


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 32 posts ]  Go to page 1, 2, 3  Next

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.