-->
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: Getting the Hibernate Generated Native Id after merge
PostPosted: Wed Aug 23, 2006 5:12 pm 
Newbie

Joined: Wed Aug 23, 2006 4:55 pm
Posts: 1
Hi,

Is there a to find the id generated by Hibernate for an Object with out firing a Query after the merge is called ??

I have a the following Mapping
Code:
<class name="RiskReportDataTypes" table="RISK_REPORT_DATA_TYPES" optimistic-lock="none">
      <id name="riskReportDataTypesId" type="string" unsaved-value="null">
            <column name="RISK_REPORT_DATA_TYPES_ID" not-null="true" unique="true"/>
            <generator class="native"/>
        </id>
    <property name="reportItemId" type="string" column="REPORT_ITEM_ID" not-null="true"/>
    <property name="dataType" type="string" column="DATA_TYPE" length="100" />
  </class>


and the code i am using is
Code:

                   RiskReport dpaRiskReport = new RiskReport();
                   riskReport.setFileName(theFile.getName());
                   riskReport.setJobId(fileHash);
                   getDataAccessBean().mergeEntity(riskReport);
                   getDataAccessBean().refreshEntity(riskReport);
                   System.out.println("riskReport.getId()="+riskReport.getId());


now the above output statement is printing null..Is there a way to get the Id generated by the hibernate Sequence with out firing a Query ??

Thanks
Sateesh


Top
 Profile  
 
 Post subject: Re: Getting the Hibernate Generated Native Id after merge
PostPosted: Fri Feb 22, 2013 6:41 pm 
Newbie

Joined: Fri Feb 22, 2013 6:29 pm
Posts: 1
EntityManager.merge(..) gets an instance and returns an instance that is managed. And in case of transient instances it returns a new instance (does not modify the original)

So your method should return em.merge(entity)

When a object is cloned say for example, The below merge will create a new record in to db with a new generated id.

Foo foo = getById(xxxx);
foo.setid(null or 0)
BeanUtils.copyproperties(newFoo,foo);
newFoo= mergeFoo(newFoo)

mergeFoo(foo){
return getHibernateTemplate().merge(entity);

getHIbernateTemplate(Sessionfactory sessionfactory){
return new HibernateTemplate(sessionFactory);
}


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.