-->
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.  [ 16 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: id is not set after getSession().save()
PostPosted: Mon May 11, 2009 5:09 pm 
Newbie

Joined: Thu Mar 19, 2009 8:19 pm
Posts: 12
I was wondering if anyone could help me with a hibernate problem:

The problem is that the id of the object is not set after save. For instance:

Code:
AreaType areaType = new AreaType(null, "AREA"); //i initiall set the id to null
databaseManager.save(areaType);
areaType.getId() is null! //but the object is saved in database with an id.


Here is how the id is defined in AreaType.hbm.xml

Code:
<id name="id" type="java.lang.Integer" unsaved-value="null" column="id">
         <generator class="sequence">
            <param name="sequence">area_type_seq</param>
         </generator>
</id>



I also tried using using my own SequenceGenerator but it didn't help.

Code:
public Serializable save(Object entity) throws NamingException,
      HibernateException
  {
    return getCurrentSession().save(entity);
  }



I am using Hibernate 3, Postgresql 8.3


Any ideas?


Top
 Profile  
 
 Post subject: Re: id is not set after getSession().save()
PostPosted: Tue May 12, 2009 2:58 am 
Newbie

Joined: Tue May 12, 2009 2:53 am
Posts: 8
call "flush" on the session(getSession().flush()) and then try to get the object from the DB.


Top
 Profile  
 
 Post subject: Re: id is not set after getSession().save()
PostPosted: Tue May 12, 2009 12:12 pm 
Newbie

Joined: Thu Mar 19, 2009 8:19 pm
Posts: 12
Thanks for your replay,

but I have already tried that and it doesn't work.

Any other ideas?


Top
 Profile  
 
 Post subject: Re: id is not set after getSession().save()
PostPosted: Wed May 13, 2009 1:29 am 
Senior
Senior

Joined: Tue Oct 28, 2008 10:39 am
Posts: 196
Are you sure your insert is executed und commited => show_sql? Can you see the row with a databasetool like PgAdmin?


Top
 Profile  
 
 Post subject: Re: id is not set after getSession().save()
PostPosted: Wed May 13, 2009 3:08 am 
Newbie

Joined: Tue May 12, 2009 2:53 am
Posts: 8
If you could see the new tuple(that you have updated through the code) in the DB ,then definitely "flush" should work..

The exact code snippet will be
**********************
AreaType areaType = new AreaType(null, "AREA"); //i initiall set the id to null
databaseManager.save(areaType);
databaseManager.flush(); // i'm assuming that "databaseManager" is an instance of Session
areaType.getId()
**********************


Top
 Profile  
 
 Post subject: Re: id is not set after getSession().save()
PostPosted: Wed May 13, 2009 12:16 pm 
Newbie

Joined: Thu Mar 19, 2009 8:19 pm
Posts: 12
That's exactly what happens:

When I call databaseManager.save() followed by flush(), i see the the following in the log:

09:11:31,588 INFO [STDOUT] Hibernate: select nextval ('public.area_type_seq')
09:11:31,604 INFO [STDOUT] Hibernate: insert into public.area_type (name, id) values (?, ?)

the id and name is set in the table,

but still the id is null, after areaType.getId()

Do I have to call areaType.getId() in another transaction or does that matter?

I also tried this (thought that maybe setting the id to null through the constructor is the cause but no luck):

Code:
AreaType areaType = new AreaType();
areaType.setName("name");
databaseManager.save(areaType);
databaseManager.flush();


Top
 Profile  
 
 Post subject: Re: id is not set after getSession().save()
PostPosted: Wed May 13, 2009 12:59 pm 
Newbie

Joined: Wed Jan 28, 2009 9:57 am
Posts: 16
Location: Leinfelden, Germany
very strange problem indeed....
If you can see the Id in the DB then it means only 1 thing, the Id is not cached (which is strange as this is the "id" column).
Is there a chance that you are using a CacheMode other than CacheMode.NORMAL?
Just a shot in the dark!


Top
 Profile  
 
 Post subject: Re: id is not set after getSession().save()
PostPosted: Wed May 13, 2009 1:01 pm 
Newbie

Joined: Thu Mar 19, 2009 8:19 pm
Posts: 12
Not sure where to find that information

here is my hibernate settings:


Code:
   <mbean code="org.jboss.hibernate.jmx.Hibernate" name="jboss.har:service=Hibernate">
      <attribute name="DatasourceName">java:/PostgresDS</attribute>
      <attribute name="Dialect">org.hibernatespatial.postgis.PostgisDialect
      </attribute>
      <attribute name="DefaultSchema">public</attribute>
      <attribute name="SessionFactoryName">
         java:/hibernate/SessionFactory
      </attribute>
      <attribute name="CacheProviderClass">org.hibernate.cache.HashtableCacheProvider
      </attribute>
      <attribute name="ShowSqlEnabled">true</attribute>
      <attribute name="MaxFetchDepth">5</attribute>

      
   </mbean>


Top
 Profile  
 
 Post subject: Re: id is not set after getSession().save()
PostPosted: Wed May 13, 2009 3:18 pm 
Newbie

Joined: Thu Mar 19, 2009 8:19 pm
Posts: 12
I also set the CacheMode to CacheMode.NORMAL but it didn't help.


Any other ideas?


Top
 Profile  
 
 Post subject: Re: id is not set after getSession().save()
PostPosted: Thu May 14, 2009 3:30 am 
Newbie

Joined: Thu Jan 15, 2009 9:54 am
Posts: 12
Even i have the same problem.

can any body guess it why ??

here is my code.

getHibernateTemplate().save( cabRequestDTO ) ;
System.out.println(cabRequestDTO.getRequestId());


output is 0.

database is updated with a new record but not able to get the generated ID


Top
 Profile  
 
 Post subject: Re: id is not set after getSession().save()
PostPosted: Thu May 14, 2009 6:06 am 
Newbie

Joined: Wed Jan 28, 2009 9:57 am
Posts: 16
Location: Leinfelden, Germany
At the moment all I can think of is, give org.hibernate.cache.NoCacheProvider a try instead of org.hibernate.cache.HashtableCacheProvider.
There are several articles on HashtableCacheProvider not being the best for production use, but that's a different issue.
Try with NoCacheProvider (or with no CacheProvider attribute at all, default was EhCache was the default prior to v 3.2).
All the best!


Top
 Profile  
 
 Post subject: Re: id is not set after getSession().save()
PostPosted: Tue May 19, 2009 8:31 pm 
Newbie

Joined: Thu Mar 19, 2009 8:19 pm
Posts: 12
Well finally, I figured out why it's happening,

I perform the save from inside an ejb, and the id is set as soon as the save call is made.

however as soon as I leave the ejb (back to the client ), the id disappears!!

Here is the scenario:

unit test calls ejb to save an object.
id is set inside ejb.

unit test checks if the id is null or not.
id is null.

Now that's some weird magic...i guess it has to do with how RMI works, but don't have a clue how to get the id to stay.


Top
 Profile  
 
 Post subject: Re: id is not set after getSession().save()
PostPosted: Wed May 20, 2009 1:17 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
If its an RMI call then the areaType object inside the EBJ is a different object than the one in the calling method. So as you are not returning the saved areaType from the EJB and assigning it to the areaType of calling method, the id field change will not be reflected.

Code:
AreaType areaType = new AreaType(null, "AREA"); //i initiall set the id to null
areaType = databaseManager.save(areaType);
areaType.getId()

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject: Re: id is not set after getSession().save()
PostPosted: Wed May 20, 2009 2:38 pm 
Newbie

Joined: Thu Mar 19, 2009 8:19 pm
Posts: 12
Thanks littypreethkr, but I am still trying to figure out if there is a way to resolve this issue perhaps using
ejb annotation or configuration?

I noticed calling databaseManager from another ejb will result in the same behavior, i.e. the id disappears:

Here is the code for databaseManagerEjb and another ejb calling databaseManagerEjb :

Code:
@Stateless
@Remote(IDatabaseManager.class)
@TransactionManagement(javax.ejb.TransactionManagementType.CONTAINER)
@TransactionAttribute(javax.ejb.TransactionAttributeType.MANDATORY)
@RemoteBinding(jndiBinding = ServiceNames.AC_DATABASE_MANAGER)
public class DatabaseManagerEjb implements IDatabaseManager
{
  /**
   * @param entity
   * @return
   * @throws NamingException
   * @throws HibernateException
   */
  public Serializable save(Object entity) throws NamingException,
      HibernateException
  {
     return getCurrentSession().save(entity);
  }

/**
   * @return
   */
  public Session getCurrentSession() throws NamingException, HibernateException
  {
    Session hsession = null;
    SessionFactory factory = getSessionFactory();
    try
    {
      hsession = factory.getCurrentSession();
    }
    catch (Exception e)
    {

      e.printStackTrace();
      logger.info(e.getMessage());
      if (hsession == null)
      {
        hsession = factory.openSession();

      }
    }

    return hsession;
  }

  public SessionFactory getSessionFactory() throws NamingException,
      HibernateException
  {
    SessionFactory factory = null;
    try
    {
      InitialContext ctx = new InitialContext();
      factory = (SessionFactory) ctx
          .lookup(ServiceNames.HIBERNATE_SESSION_FACTORY);
    }
    catch (Exception e)
    {
      e.printStackTrace();
      logger.info(e.getMessage());
    }

    return factory;
  }

}


Other ejb

Code:
@Stateless
@Remote(IAreaManager.class)
@TransactionManagement(javax.ejb.TransactionManagementType.CONTAINER)
@TransactionAttribute(javax.ejb.TransactionAttributeType.REQUIRES_NEW)
@RemoteBinding(jndiBinding = ServiceNames.AC_AREA_MANAGER)
public class AreaManagerEjb implements IAreaManager
{
public Area createArea(Date creationTime, Date modificationTime,
      Geometry geometry)
  {
   
    Area area = new Area(null, creationTime, modificationTime, geometry);
    try
    {
      databaseManager.save(area);
      //area.getId() is null
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
    return area;
  }

}


Top
 Profile  
 
 Post subject: Re: id is not set after getSession().save()
PostPosted: Wed Jun 03, 2009 9:00 am 
Newbie

Joined: Wed Jan 28, 2009 9:57 am
Posts: 16
Location: Leinfelden, Germany
Does the AreaType object implement Serializable?


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