-->
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.  [ 21 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: The id is null
PostPosted: Wed Dec 13, 2006 12:56 pm 
Beginner
Beginner

Joined: Tue Sep 05, 2006 2:36 pm
Posts: 24
Hibernate version: 3

Mapping documents:

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

<hibernate-mapping>

<class name="User" table="users">
<id name="id" column="userId">
<generator class="native" />
</id>
<property name="name"/>
<property name="lastName"/>
</class>

</hibernate-mapping>


The problem is that when I get an instance of User, the id property remains in null, the name and lastName properties are getted correctly.
The userId filed in the database is a PK Autonumeric, so is never null.

Is this behavior ok, or maybe the id element in the mapping is wrong?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 13, 2006 1:00 pm 
Beginner
Beginner

Joined: Wed Sep 13, 2006 7:24 am
Posts: 26
this is right,

we have this in our system

The id stays Null until it saved to the database, then the object gets a Id from the database when you save/flush the item

_________________
Ben


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 13, 2006 1:11 pm 
Beginner
Beginner

Joined: Tue Sep 05, 2006 2:36 pm
Posts: 24
Ok, and how could I get the whole object included the id in a session.get call?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 13, 2006 1:29 pm 
Beginner
Beginner

Joined: Wed Sep 13, 2006 7:24 am
Posts: 26
mateamargo wrote:
Ok, and how could I get the whole object included the id in a session.get call?


Object needs to be saved first. once you have saved the object the object should have the id set

so if i do

Code:
Person x = new Person("me","somewhere);
session.save(x);
x.getId() 

Should return something

if not the you can do

Code:
session.update(x);


which will update the object with the details from the its store.

_________________
Ben


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 13, 2006 1:48 pm 
Beginner
Beginner

Joined: Tue Sep 05, 2006 2:36 pm
Posts: 24
The thing is that my object is already on the database, I don't need to save it or update it.

I'm using HQL query to retrieve a list of instances of User, and I need that each one has an id (previously I said session.get, but it was a mistake).

My HQL query is:

Code:

session.beginTransaction();
session.getTransaction().begin();
   
Query q = session.createQuery("from User");

return q.list();



But when I iterate through the list, I always get null when I try to read the id.


Top
 Profile  
 
 Post subject: Re: The id is null
PostPosted: Wed Dec 13, 2006 2:20 pm 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
mateamargo wrote:
The problem is that when I get an instance of User, the id property remains in null, the name and lastName properties are getted correctly.
The userId filed in the database is a PK Autonumeric, so is never null.

Is this behavior ok, or maybe the id element in the mapping is wrong?

What do you mean by "when I get an instance" ? Retrieving an instance from the db, though the session object or a brand new one created with "new YourObject()" for example ?

As bengrant said, if the object is newly created by you, this is totally normal that the id is null with the generator you specified. Even more : this is a requirement. If you put something in the id, you'll get an error because Hibernate will try and find a corresponding instance with the corresponding primary key in the db.

If the id is null after retrieving from the db, this would be very odd. Please let us know.

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 13, 2006 2:35 pm 
Beginner
Beginner

Joined: Tue Dec 12, 2006 6:43 am
Posts: 32
Location: London
My question is what is the database you are using.
For example if it is a sybase the PK autogenerator should be like


id numeric(5) identity constraint p_contraint_xxx primary key


if you work with Oracle so for autogenerartor ( native generator)
id BIGINT not null generated by default as identity

When you save the transient object
can you find out if the below "id" returns a vlaue

Person x = new Person("me","somewhere);
Serializable id = session.save(x);
System.out.println(id) // Do you see a value ????
x.getId()

_________________
Alan Mehio
London
UK


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 13, 2006 3:05 pm 
Beginner
Beginner

Joined: Tue Sep 05, 2006 2:36 pm
Posts: 24
I'm trying to retrieve a list of objects.

I'm using HQL (as I said before).

My code is something like this:

Code:

List<User> users = User.getList();

for(User user: users){
  System.out.println("The user ID is: " + user.getId());
}



The getList method:

Code:

public static List getList(){

session.beginTransaction();
session.getTransaction().begin();
   
Query q = session.createQuery("from User");

return q.list();

}



All the Id's are null.

PS: The database ser is MySQL 5 running on WinXP.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 14, 2006 6:02 am 
Beginner
Beginner

Joined: Wed Sep 13, 2006 7:24 am
Posts: 26
bengrant wrote:
mateamargo wrote:
Ok, and how could I get the whole object included the id in a session.get call?


Object needs to be saved first. once you have saved the object the object should have the id set

so if i do

Code:
Person x = new Person("me","somewhere);
session.save(x);
x.getId() 

Should return something

if not the you can do

Code:
session.update(x);


which will update the object with the details from the its store.


sorry i meant to type

Code:
session.refresh(x);

_________________
Ben


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 14, 2006 10:16 am 
Beginner
Beginner

Joined: Tue Sep 05, 2006 2:36 pm
Posts: 24
bengrant: The problem is when I get a list of objects. Should I perform session.refresh on each one in the list?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 14, 2006 10:34 am 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
Can we see your POJO please.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 14, 2006 11:16 am 
Beginner
Beginner

Joined: Tue Sep 05, 2006 2:36 pm
Posts: 24
Ananasi wrote:
Can we see your POJO please.


Code:
public class User{

private Long id;
private String name;
private String lastName;

public User(){
}

public Long getId(){
  return id;
}

public void setId(Long id){
  this.id = id;
}

public String getName(){
  return name;
}

public void setName(String name){
  this.name = name;
}

public String getLastName(){
  return lastName;
}

public void setLastName(String lastName){
  this.lastName = lastName;
}

}


This is my class without the getList method that I previously posted.
But basically the idea is to return a list of users (that is working ok), but all the id's are null.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 14, 2006 11:36 am 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
That is pretty odd. Can you put in type="java.lang.Long" in the id mapping and see if that helps. If not, turn on debug logging and see how the entities are being hydrated.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 14, 2006 12:33 pm 
Beginner
Beginner

Joined: Tue Sep 05, 2006 2:36 pm
Posts: 24
I changed it, but I'm still having the same issue.

The only way I found to resolve this is using a property that I called "dummyId" and points to same field that the original Id but with the attributes insert="false" and update="false".

Is this a bug? could any of you try to reproduce this and tell me if you are having the same problem?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 14, 2006 12:45 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
I can try to reproduce, please post the DDL so that I can recreate the database exactly as you have it.


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