-->
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.  [ 9 posts ] 
Author Message
 Post subject: id setting issue
PostPosted: Fri Feb 15, 2008 5:24 am 
Newbie

Joined: Fri Feb 15, 2008 5:05 am
Posts: 5
Location: Geneva
Good morning every body,

I've a little problem with the hibernate's id managing. I've learned in the reference documentation and in a book that I'm reading that when we persist an object, hibernate will associate the resulting id to the java object (depending on the id generator used).

In my testing code I use a native generator, so when I save the object, an insert occure immediately to retrieve the id. The id is returned by the save method but normally the id is set in the java object isn't it?... In my case... nothing happen. My object is correctly persisted but it doesn't have the id set.

I read, re-read the doc, I debug my code and hibernate, I searched in the forum, in the faq, on google... I didn't find why hibernate didn't init my id... Could anyone help me please?

Thanks in advance.

Hibernate version:3.2.5

Mapping documents:
Code:
<class name="Event" table="tevent">
    <id type="long">
        <generator class="native" />
    </id>
    <property name="title" type="string" />
    <many-to-one cascade="save-update" class="Speaker" column="speaker_id" name="speaker" />
</class>


Code between sessionFactory.openSession() and session.close():
Code:
tr = session.beginTransaction();
session.save(e);
tr.commit();
log.debug("ID Event = "+ e.getId());


Name and version of the database you are using: MySQL 5

The generated SQL (show_sql=true): insert into tevent (title, speaker_id) values (?, ?)

Debug level Hibernate log excerpt: DEBUG


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 15, 2008 10:35 am 
Newbie

Joined: Fri Feb 15, 2008 5:07 am
Posts: 13
If your column is auto_increment you should change your generator to
<generator class="increment" />


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 15, 2008 11:29 am 
Newbie

Joined: Fri Feb 15, 2008 5:05 am
Posts: 5
Location: Geneva
gishac wrote:
If your column is auto_increment you should change your generator to
<generator class="increment" />


Thank you gishac for your response.

Unfortunately, this doesn't work better. In my case, the id generation is correct, it's only the "binding" between the db and my pojo that doesn't work.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 15, 2008 11:47 am 
Newbie

Joined: Wed Feb 13, 2008 6:01 am
Posts: 8
Can you post the contents of the Event class and the top element of you hibernate mapping file i.e. <hibernate-mapping ........>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 15, 2008 11:57 am 
Newbie

Joined: Fri Feb 15, 2008 5:05 am
Posts: 5
Location: Geneva
ericp wrote:
Can you post the contents of the Event class and the top element of you hibernate mapping file i.e. <hibernate-mapping ........>

Of course.

Here is the top of the mapping file:
Code:
<?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 package="org.poxd.entity">


and here is the content of the Event class:
Code:
package org.poxd.entity;

import java.io.Serializable;
import java.util.Set;
import java.util.jar.Attributes.Name;

public class Event implements Serializable{
   private static final long serialVersionUID = 1L;
   private Long id;
   private String title;
   private Speaker speaker;
   
   public Event() {
      this("", null);
   }
   
   public Event(String title, Speaker speaker) {
      this.title = title;
      this.speaker = speaker;
   }

   public Long getId() {
      return id;
   }
   public void setId(Long id) {
      this.id = id;
   }
   public String getTitle() {
      return title;
   }
   public void setTitle(String title) {
      this.title = title;
   }

   public Speaker getSpeaker() {
      return speaker;
   }

   public void setSpeaker(Speaker speaker) {
      this.speaker = speaker;
   }
   
   @Override
   public boolean equals(Object obj) {
      if (obj == this)
         return true;
      if (!(obj instanceof Event))
         return false;
      Event a = (Event) obj;
      if (a.id == this.id && a.title.equals(this.title))
         return true;
      return false;
   }
   
   @Override
   public int hashCode() {
      return this.title.hashCode();
   }
   
   @Override
   public String toString() {
      return title +" by " + speaker.getName();
   }
}


Last edited by pollux on Fri Feb 15, 2008 5:54 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 15, 2008 12:17 pm 
Newbie

Joined: Fri Feb 15, 2008 5:07 am
Posts: 13
pollux wrote:
gishac wrote:
If your column is auto_increment you should change your generator to
<generator class="increment" />


Thank you gishac for your response.

Unfortunately, this doesn't work better. In my case, the id generation is correct, it's only the "binding" between the db and my pojo that doesn't work.


It's strange i have the following configuration and hibernate bind's well the id in the object after insert

<id name="idRole" type="java.lang.Integer">
<column name="ID_ROLE" />
<generator class="increment" />
</id>

i'm using hibernate 3.1, how much is the diferrence between these versions?

:S


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 18, 2008 4:00 am 
Newbie

Joined: Fri Feb 15, 2008 5:05 am
Posts: 5
Location: Geneva
This is really strange! It's fine with the version 3.1 and then when I tried with the 3.2 it went nice.... Odd... But tanks a lot!

Unfortunately I've always a problem.. Between the Event and the Attendee class I have a bidirectionnal relation. The retrieve of the Event's id is correct but for the attendees associated.... None id!... I don't understand.

Here is my Attendee class:
Code:
import java.io.Serializable;
import java.util.Set;

public class Attendee implements Serializable{
   private static final long serialVersionUID = 1L;
   private Long id;
   private String name;
   private Set<Event> events;

   public Attendee() {
      this("");
   }

   public Attendee(String name) {
      this.name = name;
   }
       
       //getter/setter

   @Override
   public int hashCode() {
      return name.hashCode();
   }

   @Override
   public boolean equals(Object obj) {
      if (obj == this)
         return true;
      if (!(obj instanceof Attendee))
         return false;
      Attendee a = (Attendee) obj;
      if (a.id == this.id && a.name.equals(this.name))
         return true;
      return false;
   }

   @Override
   public String toString() {
      return "[" + Attendee.class.getName() + " : " + getId() + " - " + getName() +" ]";
   }
}


In my Event class I just added a Set of Attendee member and its getter/setter and here is my mapping config:

Code:
<class name="Attendee" table="tattendee">
      <id type="long">
         <generator class="increment" />
      </id>
      <property name="name" type="string" />
      <set name="events" table="event_attendees" inverse="true">
         <key column="attendee_id" />
         <many-to-many class="Event" column="event_id"/>
      </set>
   </class>

...

<class name="Event" table="tevent">
      <id type="long">
         <generator class="increment" />
      </id>
      <property name="title" type="string" />
      <set name="attendees" cascade="save-update"
         table="event_attendees">
         <key column="event_id" />
         <many-to-many class="Attendee" column="attendee_id"/>
      </set>
   </class>


Somebody can help me?

Regards


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 18, 2008 11:34 am 
Newbie

Joined: Fri Feb 15, 2008 5:07 am
Posts: 13
Did you try the lazy="false" attribute in atendee property?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 18, 2008 11:40 am 
Newbie

Joined: Fri Feb 15, 2008 5:05 am
Posts: 5
Location: Geneva
Thank you for your response but it's not better.


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