Enlightensoft's Blog

Helping in your each step

  • Categories

  • Authors

Archive for the ‘Hibernate’ Category

How to Use Hibernate Annotation & .hbm.xml files at single time on a Same project?

Posted by Pankil Patel on June 11, 2012

Use sessionFactory as org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean in its configuration file instead of org.springframework.orm.hibernate3.LocalSessionFactoryBean

Sample configuration is as below:

<bean id=”sessionFactory”
class=”org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean”>
<property name=”annotatedClasses“>
<list>
<value>co.cc.enlightensoft.model.businessobject.Product</value>
</list>
</property>
<property name=”mappingResources“>
<list>
<value>co/cc/enlightensoft/model/businessobject/Category.hbm.xml</value>
<value>co/cc/enlightensoft/model/businessobject/User.hbm.xml</value>
</list>
</property>

<property name=”hibernateProperties”>
<props>
<prop key=”hibernate.dialect”>org.hibernate.dialect.MySQLDialect</prop>
<prop key=”hibernate.show_sql”>true</prop>
<prop key=”hibernate.cglib.use_reflection_optimizer”>true</prop>
<prop key=”hibernate.cache.provider_class”>org.hibernate.cache.HashtableCacheProvider</prop>
</props>
</property>

<property name=”dataSource”>
<ref bean=”dataSource” />
</property>

</bean>

Posted in Hibernate, Spring | Tagged: , , , , , , , , , | Leave a Comment »

Hibernate Automatic Dirty Check for Detached Objects

Posted by Pankil Patel on April 18, 2012

To deal with Dirty check for detached objects in Hibernate there are two ways:

1)

.hbm.xml file:

Using <version> tag in .hbm.xml file like below:

In .hbm.xml file: <version name=”version” access=”field” column=”version” type=”long”/>

In respected VO / POJO: private Long version; /* used for optimistic locking */

Annotation:

import javax.persistence.Version;

@Version
private Long version;

 

2) 

.hbm.xml file:

Using <timestamp> tag in .hbm.xml file like below:

In .hbm.xml file: <timestamp name="lastModifyDataTime" column="LAST_MODIFIY_DATE_TIME" access="field">

In respected VO / POJO: private Long lastModifyDataTime;

Annotation:

import javax.persistence.Version;

@Version
private Long lastModifyDataTime;

 

Note: Hibernate ignores the version when getting object and updating fields and then version are in the same session.

 

For more detail on Optimistic lock in Hibernate visit:

http://turgaykivrak.wordpress.com/2009/05/16/72/

AND

http://www.intertech.com/Blog/Post/Versioning-Optimistic-Locking-in-Hibernate.aspx

 

3) Use select-before-update=”true” mapping into your .hbm.xml file:

<class name=”com…….AbcVO”
table=”ABC” select-before-update=”true”>

4) By extending some of its public API:

http://www.brimllc.com/2010/08/hibernate-automatic-dirty-check-for-detached-objects/

Posted in Hibernate | Tagged: , , , | Leave a Comment »

SubSelect in hbm.xml file

Posted by Pankil Patel on April 16, 2012

<class name=”com……StaffView”
entity-name=”staff_phase2_with_Inactive”>
<subselect>
<![CDATA[
SELECT DISTINCT u.ID,
u.FIRST_NM,
u.LAST_NM,
u.MIDDLE_INIT_NM,
u.FULL_NM,
u.PRFRRD_FRST_NM,
u.STTS_CD,
u.STTS_NM
FROM USER_TABLE u
]]>
</subselect>
<id name=”staffSID” type=”string” length=”28″>
<column name=”ID” />
<generator />
</id>
<property …

.

.

.

.

.

.
</property>
</class>

Posted in Hibernate | Leave a Comment »

How to add where clouse into xml.hbm mapping file

Posted by Pankil Patel on November 13, 2011

For many-to-one:

<many-to-one name=”rankTypeLookupData”
formula=”(SELECT l.LKP_ID FROM EQ_LOOKUP_DATA l WHERE l.DROPDOWN_CODE = ‘BV_STAFF_RANK_TYPE’ AND l.LKP_KEY = INDVL_RANK_TYPE)”
class=”com……LookupData” not-found=”ignore”>
<column name=”INDVL_RANK_TYPE” />
</many-to-one>

For one-to-many:

<set name=”regionList” table=”BV_REGION” cascade=”all” fetch=”join”
outer-join=”true” where=”DEL_IN = ‘N'” inverse=”true”>
<key column=”INDVL_RANK_ID” />
<one-to-many
class=”com……..StaffTeamRankRegion” />
</set>

Posted in Hibernate | Leave a Comment »

Issue with createCriteria result set, class name appended by _$$_javassist_

Posted by Pankil Patel on November 8, 2011

Issue:  _$$_javassist_ is appended after class name, when you are using createCriteria(xyz.class) which have <set> or <bag> (one-to-many) relationship with Object that having <composite-id> instead of <id>.

Resolution: Instead of Using createCriteria(xyz.class), use EntityName for Criteria Query.

Ex: DetachedCriteria detachedCriteria = DetachedCriteria.forEntityName(“xyzSearch”);

Impact of Issue:

  1. For java, if you are using any code which have  comparison with class name it will fail in that case.
  2. If you are using any communication by RMI like: Spring BlazeDS for Flex & Java, It will not able to cast the Object in proper format & will give result in generics Object format.

Posted in Hibernate | Tagged: , , , | Leave a Comment »

How to use sequence in ID tag of hbm.xml???

Posted by Pankil Patel on September 26, 2011

How to use sequence in ID tag of hbm.xml???

<id name=“id” type=“java.lang.Long” column=“ID”>

<generatorclass=“sequence”><paramname=“sequence”>XYZ_TAG_STAFF_SEQ</param></generator>

</id>

OR

@Id @GeneratedValue(generator=”sequence”)
@GenericGenerator(name=”sequence”, strategy = “sequence”,
parameters = {
@Parameter(name=”sequence”, value=”XYZ_TAG_STAFF_SEQ”)
}
)
public Integer getID() {
return ID;
}

 

Posted in Hibernate | Leave a Comment »

How to call store procedure or package in Hibernate???

Posted by Pankil Patel on September 26, 2011

How to call store procedure or package in Hibernate???

Please find attachment for the solution: StoreProcCallWithHibernate

Posted in Hibernate | Leave a Comment »

How to use Entity Name, specified in hbm.xml in DetachedCriteria???

Posted by Pankil Patel on September 26, 2011

 How to use Entity Name, specified in hbm.xml in DetachedCriteria???

OR

How to use OR Restriction in Criteria Query???

DetachedCriteria criteria = DetachedCriteria.forEntityName(“staff_phase2”);

Criterion crit1 = Restrictions.ilike(“lastName”, searchText.trim(), MatchMode.START);

 Criterion crit2 = Restrictions.ilike(“firstName”,searchText.trim(), MatchMode.START);

Criterion staffSearchCriterion = Restrictions.or(crit1, crit2); 

criteria.add(staffSearchCriterion);

Please find hbm.xml file used for this post, for your refrence: entity-name

Posted in Hibernate | Leave a Comment »

Parent Child Join With Criteria & DetachedCriteria using HibernateCallback

Posted by Pankil Patel on September 26, 2011

How to join parent table to child table using Criteria Query???

Please find attachment for solution:

ParentChildWithCriteriaJoin

 

Posted in Hibernate | Leave a Comment »

hbm.xml many-to-one & one-to-many Example

Posted by Pankil Patel on July 13, 2011

Example of many-to-one & one-to-many hbm.xml mapping

Mapping

Mapping

Posted in Hibernate | Leave a Comment »