-->
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.  [ 11 posts ] 
Author Message
 Post subject: Unable to retreive the key for a object
PostPosted: Thu Oct 16, 2003 8:49 am 
Newbie

Joined: Wed Oct 15, 2003 9:20 am
Posts: 12
Location: Qu
I am able to create a new object in the DB but when I try to retreive it, I receive a null string for my key. I have a other string field(that is not a key) and the retreived value is good.

Here is my hbm file:
Code:
<hibernate-mapping>
  <class name="dti.xmscore.model.cLaboratory" table="lab">
    <id name="aNewLabID" column="lab_id" type="string" length="20">
      <generator class="assigned"/>
    </id>

    <property name="aManagerName" column="manager_name" type="string" length="30"/>
   
  </class>
</hibernate-mapping>


Here is my code to retreive the object:
Code:
try {
   List results = (List) session.find(key);
   cLaboratory lab = (cLaboratory)results.get(0);
   System.out.pritnln(lab);
} catch (Exception e) {
   ...
}


The output is:
Code:
   dti.xmscore.model.cLaboratory@6f9b8e[aNewLabID=<null>]


If I change my key to a long with hilo generation it work well:
Code:
<hibernate-mapping>
  <class name="dti.xmscore.model.cLaboratory" table="lab">
    <id name="id" column="key_id" type="long" unsaved-value="null">
      <generator class="hilo"/>
    </id>

    <property name="aManagerName" column="manager_name" type="string" length="30"/>
   
  </class>
</hibernate-mapping>


The output is now:
Code:
dti.xmscore.model.cLaboratory@14db0e3[id=1]


Any idea?

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 16, 2003 9:34 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Can you show us your creation code ?
Is there null in DB ?
Did you explicitly call cLaboratory.setaNewLabID(myNotNullKey) before saving it ?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 16, 2003 9:54 am 
Newbie

Joined: Wed Oct 15, 2003 9:20 am
Posts: 12
Location: Qu
Thanks.

The code to create the object is ok, because the lab_id field in the database contains 'lab200000240' that is the laboratory identifier. There's no null in the DB.

Here's the creation code:
Code:
cLaboratory lab = new cLaboratory("lab200000240", "MyName");
session.save(lab);
session.flush();
session.connection().commit();


It's strange, because when a retreive the object stored, It display 'MyName' string.


epbernard wrote:
Can you show us your creation code ?
Is there null in DB ?
Did you explicitly call cLaboratory.setaNewLabID(myNotNullKey) before saving it ?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 16, 2003 10:15 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Can I have a look at cLaboratory class code ?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 16, 2003 10:27 am 
Newbie

Joined: Wed Oct 15, 2003 9:20 am
Posts: 12
Location: Qu
Here's the class definition:

public final class cLaboratory implements Serializable {

/** The unique laboratory ID */
protected String aNewLabID;

/** The name of the owner/manager of this lab. */
protected String aManagerName;

/** default constructor */
protected cLaboratory() {
}

/**
* Minimal constructor
*/
public cLaboratory(String aNewLabID) {
super();
aLabID = 0;
this.aNewLabID = aNewLabID;
mInit();
}

private String getaNewLabID() {
return this.aNewLabID;
}

private void setaNewLabID(String newLabID) {
this.aNewLabID = aNewLabID;
}

private String getaManagerName() {
return this.aManagerName;
}

private void setaManagerName(String aManagerName) {
this.aManagerName = aManagerName;
}

public String toString() {
return new ToStringBuilder(this)
.append("aNewLabID", getaNewLabID())
.toString();
}

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

public int hashCode() {
return new HashCodeBuilder()
.append(getaNewLabID())
.toHashCode();
}
}


epbernard wrote:
Can I have a look at cLaboratory class code ?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 16, 2003 10:51 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Hum, that's not the class you used.
There is not Constructor(String, String)
mInit() isn't defined
aLabID=0 has no declaration.

The error is probably in
Code:
private void setaNewLabID(String newLabID) {
   this.aNewLabID = aNewLabID;
}


replace by this.aNewLabID = newLabID. aNewLabID = this.aNewLabID

Hibernate use setaNewLabId which is broken.

Hint, use the _newLabId conevntion for internal attribute, this will prevent such mistake.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 16, 2003 11:01 am 
Newbie

Joined: Wed Oct 15, 2003 9:20 am
Posts: 12
Location: Qu
Oups, excuse-me, I have put my test class trying to resolve my problem. Sorry.

Here is my real class:
Code:
package dti.xmscore.model;

import java.io.Serializable;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

public final class cLaboratory implements Serializable {

    /** The unique laboratory ID */
    protected String        aNewLabID;
   
    /** The name of the owner/manager of this lab. */
    protected String        aManagerName;

    /** default constructor */
    protected cLaboratory() {
    }
   
    /**
     * Minimal constructor
     */
    public cLaboratory(String aNewLabID) {
        this.aNewLabID = aNewLabID;
    }

    public String getaNewLabID() {
        return this.aNewLabID;
    }

    public void setaNewLabID(String newLabID) {
        this.aNewLabID = aNewLabID;
    }

    public String getaManagerName() {
        return this.aManagerName;
    }

    public void setaManagerName(String aManagerName) {
        this.aManagerName = aManagerName;
    }
   
    public String toString() {
        return new ToStringBuilder(this)
            .append("aNewLabID", getaNewLabID())
            .toString();
    }

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

    public int hashCode() {
        return new HashCodeBuilder()
            .append(getaNewLabID())
            .toHashCode();
    }
}



Thanks for your help. The setaNewLabId was broken. A stupid cut/paste when I was trying to make fonctionnal my class.

I have a other question for you: I have a Vector of Strings in a Class and I want to persist this list in the DB. I don't want to create a class only for one field. What is the best way to do it?

Thanks.


epbernard wrote:
Hum, that's not the class you used.
There is not Constructor(String, String)
mInit() isn't defined
aLabID=0 has no declaration.

The error is probably in
Code:
private void setaNewLabID(String newLabID) {
   this.aNewLabID = aNewLabID;
}


replace by this.aNewLabID = newLabID. aNewLabID = this.aNewLabID

Hibernate use setaNewLabId which is broken.

Hint, use the _newLabId conevntion for internal attribute, this will prevent such mistake.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 16, 2003 11:07 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Use a collection of values. Have a look at Hibernate reference guide section 5.3.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 16, 2003 11:21 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
So are you still having problems with this? If so, I don't understand:
Quote:
but when I try to retreive it, I receive a null string for my key


and

Quote:
It's strange, because when a retreive the object stored, It display 'MyName' string


seem contradictory.


Quote:
Use a collection of values.

Depends how these get stored. If they are on a seperate table keyed by the owning class id, then a collection of values will work. If they are on the same table, you can utilize a UserType or store the collection in its serialized format.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 16, 2003 11:48 am 
Newbie

Joined: Wed Oct 15, 2003 9:20 am
Posts: 12
Location: Qu
Thanks.

epbernard wrote:
Use a collection of values. Have a look at Hibernate reference guide section 5.3.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 16, 2003 11:50 am 
Newbie

Joined: Wed Oct 15, 2003 9:20 am
Posts: 12
Location: Qu
No. Now my problem is solve. That was the setter that was incorrectly coded.

Thanks.

steve wrote:
So are you still having problems with this? If so, I don't understand:
Quote:
but when I try to retreive it, I receive a null string for my key


and

Quote:
It's strange, because when a retreive the object stored, It display 'MyName' string


seem contradictory.


Quote:
Use a collection of values.

Depends how these get stored. If they are on a seperate table keyed by the owning class id, then a collection of values will work. If they are on the same table, you can utilize a UserType or store the collection in its serialized format.


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