-->
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.  [ 2 posts ] 
Author Message
 Post subject: Generating an id using custom id generator
PostPosted: Tue May 15, 2012 9:56 am 
Newbie

Joined: Tue May 15, 2012 9:43 am
Posts: 3
Hi,

I want to export one database contents into another.But i dont want to auto generate the ids.
I want to use the same ids of the previous database.

I have the same hbm file for both the databases.In one database i want auto increment and in other database i want to use the existing id.

i want to overcome this error:

Code:

org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1; nested exception is org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1




My hbm is :

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>
    <class name="com.bean.test" table="test" dynamic-insert="true" dynamic-update="true" >
        <id name="restaurantId" type="java.lang.Long">
            <column name="restaurant_id" />
            <generator class="com.bean.commons.CustomIdGenerator" />
        </id>


I tried using this:

Code:

import java.io.Serializable;

import org.hibernate.HibernateException;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.id.IdentifierGenerator;
import org.hibernate.id.IdentifierGeneratorFactory;
import org.hibernate.persister.entity.EntityPersister;

public class CustomIdGenerator implements IdentifierGenerator
{

   @Override
   public Serializable generate(SessionImplementor sessionImplementor, Object object) throws HibernateException
   {
      try
      {
      System.out.println("in custom generator..."+object.getClass().getCanonicalName());
      EntityPersister entityPersister = sessionImplementor.getEntityPersister("restaurantId", object);
      
      Serializable id = entityPersister.getIdentifierGenerator().generate(sessionImplementor, "restaurantId");
      
      System.out.println("id is "+id);
      if(id == null)
      {
         return IdentifierGeneratorFactory.POST_INSERT_INDICATOR;
      }
      
      return id;
      }
      catch (Exception e)
      {
         System.out.println("in exception ");
         System.out.println(e.getMessage());
         return null;
      }
   }

}



If the object does not contain an id, use the autoincremented one else use the existing.
Where am i going wrong.

Please help.
Thanks in advance.

Regards,
Annuk


Top
 Profile  
 
 Post subject: Re: Generating an id using custom id generator
PostPosted: Fri Nov 29, 2013 1:16 am 
Newbie

Joined: Fri Nov 29, 2013 1:09 am
Posts: 1
Hi Annuk,
Not sure if this problem is still valid for you but for others.
The mistake here is that you are implementing the interface instead you should
Code:
extends  IdentityGenerator

e.g
Code:
public class UIGenerator extends  IdentityGenerator
{
     public Serializable generate(SessionImplementor session, Object object) throws HibernateException
   {
      //
                long id = generateMyID();
                if (id == null)
         return IdentifierGeneratorHelper.POST_INSERT_INDICATOR; //asking Hibernate to generate ID
         
        }
}


Please note here that "POST_INSERT_INDICATOR" is valid for DB's that generate ID on insertion e.g SQL Server.


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