-->
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.  [ 8 posts ] 
Author Message
 Post subject: hibernate changing id fields with postgres
PostPosted: Mon Feb 23, 2004 4:11 am 
Beginner
Beginner

Joined: Thu Feb 19, 2004 6:07 am
Posts: 21
Hi all,
Can anyone tell me why hibernate seems to change id's of records retrieved from database ?
When I type the following query:
select f.forum_id as forum_id0_, f.name as name0_ from Forum as f
i have two records both of which are returned, but id numbers 1 and 2 are both changed into 0. Why ?
When I type the following query - however -
select f.forum_id as forum_id0_, f.name as name0_ from Forum as f where f.forum_id=2
Here the stack trace shows that hibernate changed the identifier from
2 to 0.
Any light shed on the matter will be appreciated.

regards,
sanjeev.




Code:
  [java] net.sf.hibernate.HibernateException: identifier of an instance of Fo
rum altered from 1 to 0
     [java]     at net.sf.hibernate.impl.SessionImpl.checkId(SessionImpl.java:25
24)
     [java]     at net.sf.hibernate.impl.SessionImpl.flushEntity(SessionImpl.jav
a:2347)
     [java]     at net.sf.hibernate.impl.SessionImpl.flushEntities(SessionImpl.j
ava:2340)
     [java]     at net.sf.hibernate.impl.SessionImpl.flushEverything(SessionImpl
.java:2207)
     [java]     at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2186
)
     [java]     at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTrans
action.java:61)
     [java] Foo cast done !!!
     [java] My foo id is:0
     [java] My foo id is:0
     [java] HibernateException stack trace followsidentifier of an instance of F
orum altered from 1 to 0
     [java]     at QueryEngine.runprog(QueryEngine.java:180)
     [java]     at QueryEngine.<init>(QueryEngine.java:38)
     [java]     at Myfe.actionPerformed(Myfe.java:111)
     [java]     at javax.swing.AbstractButton.fireActionPerformed(AbstractButton
.java:1786)
     [java]     at javax.swing.AbstractButton$ForwardActionEvents.actionPerforme
d(AbstractButton.java:1839)
     [java]     at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultBut
tonModel.java:420)
     [java]     at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.
java:258)
     [java]     at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Basi
cButtonListener.java:245)
     [java]     at java.awt.Component.processMouseEvent(Component.java:5099)
     [java]     at java.awt.Component.processEvent(Component.java:4896)
     [java]     at java.awt.Container.processEvent(Container.java:1569)
     [java]     at java.awt.Component.dispatchEventImpl(Component.java:3614)
     [java]     at java.awt.Container.dispatchEventImpl(Container.java:1627)
     [java]     at java.awt.Component.dispatchEvent(Component.java:3476)
     [java]     at java.awt.LightweightDispatcher.retargetMouseEvent(Container.j
ava:3483)
     [java]     at java.awt.LightweightDispatcher.processMouseEvent(Container.ja
va:3198)
     [java]     at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3
128)
     [java]     at java.awt.Container.dispatchEventImpl(Container.java:1613)
     [java]     at java.awt.Window.dispatchEventImpl(Window.java:1606)
     [java]     at java.awt.Component.dispatchEvent(Component.java:3476)
     [java]     at java.awt.EventQueue.dispatchEvent(EventQueue.java:456)
     [java]     at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDi
spatchThread.java:201)
     [java]     at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDisp
atchThread.java:151)
     [java]     at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.j
ava:145)
     [java]     at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.j
ava:137)
     [java]     at java.awt.EventDispatchThread.run(EventDispatchThread.java:100
)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 23, 2004 10:51 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Quote:
Why ?


Broken get/set pair?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2004 12:04 am 
Beginner
Beginner

Joined: Thu Feb 19, 2004 6:07 am
Posts: 21
Thanks for taking the time to respond - gavin.
Really appreciate the way you've been helping everybody
out !
Ok - here are my Forum.hbm.xml and Forum.java bean class.
Pretty simple - i'd very much appreciate it if someone could
point out what the error is - coz i'm losing sleep over this.


Code:
public class Forum {
   protected int forum_id;
   private String name;
   
   // ===============
public Forum() {
   
}
public Forum(int forum_id, String name){
   this.forum_id=forum_id;
   this.name=name;
}
   public int getForum_id() {
      return forum_id;
   }
   public String getName() {
      return name;
   }
   
   public void setForum_id(int forum_id) {
      forum_id = forum_id;
   }
   public void setName(String name) {
      name = name;
   }
   
}




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

    <class name="Forum" table="forum">

              <id name="forum_id" type="int" unsaved-value="null" >
            <column name="forum_id" sql-type="int" not-null="true"/>
            <generator class="native"/>
        </id>

        <property name="name">
            <column name="name" sql-type="char(255)" not-null="true"/>
        </property>
               
    </class>
   


</hibernate-mapping>   


Why is hibernate changing the id values retrieved from database ?
Broken getter/setter ? I'd be very thankful if anyone could point out
where.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2004 2:36 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Code:
   public void setForum_id(int forum_id) {
      forum_id = forum_id;
   }


yup, its broken :)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2004 3:43 am 
Beginner
Beginner

Joined: Thu Feb 19, 2004 6:07 am
Posts: 21
Aaargh - ok - i am blind(apparently).
I have to ask you how(i mean how broken ? And what the
correct implementation should be.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2004 4:19 am 
Newbie

Joined: Thu Feb 12, 2004 10:02 am
Posts: 10
Location: Ghent, Belgium
This is a typical case of name hiding
it should be

Code:
  public void setForum_id(int forum_id)
  {
    this.forum_id = forum_id;
  }


or change the parameter name to something else[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2004 5:45 am 
Beginner
Beginner

Joined: Thu Feb 19, 2004 6:07 am
Posts: 21
Gavin - thanks a lot. Really appreciate it.
Am grateful - now everything works. Yoo hoo !


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2004 6:04 am 
Beginner
Beginner

Joined: Thu Feb 19, 2004 6:07 am
Posts: 21
Thanks Vigo - i managed to figure it out finally - whew and all that !


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