Friday, July 22, 2011
Incorrect Order of SQL execution within a Spring Hibernate Transaction
On checking on the hibernate log found that the insert was fired before the update .I tried to disable the Spring transaction and then the order of execution was correct.
Looks like the order of SQl execution is not guaranteed within a transaction.To solve this issue one of the following needs to be done after the update and before the insert
1.call hibernateTemplate.flush() or
2.call hibernateTemplate.refresh() or
3.again read the record from table ,this internally calls the flush.
Friday, July 2, 2010
Hibernate Criteria Sample Implementation
The following are some commonly used criterion.
(i) To query based on simple search criteria like Employee Type Permanent and Confirmation Date less than Request Date
Criterion[] criterions = new Criterion[] {
Restrictions.eq( "empType", 'P' ),
Restrictions.and( Restrictions.isNotNull( "confirmationDate" ),
Restrictions.le( "confirmationDate",
requestDate ) )
};
for ( Criterion criterion : criterions ) {
criteria.add( criterion );
}
List
(ii) To Retrieve values between
Criteria criteria = session.createCriteria( Emp.class );
Criterion[] criterions = new Criterion[] {
Restrictions.between( "joiningDate", startDate,endDate )};
for ( Criterion criterion : criterions ) {
criteria.add( criterion );
}
List
(iii) Using In Clause
Criteria criteria = session.createCriteria( Emp.class );
Criterion[] criterions = new Criterion[] {
Restrictions.in( "empType",new Object[]{'P','C','R' )};
for ( Criterion criterion : criterions ) {
criteria.add( criterion );
}
List result=criteria.list( );
(iv) To find Record Count.
Criteria criteria = session.createCriteria( Emp.class );
criteria.setProjection(Projections.rowCount());
result=criteria.list( );
if ((null != result) && (!result.isEmpty())) {
Integer rowCount = (Integer) result.get(0);
}
(v) Returning 'n' columns from the Entity List based on Search Criteria
Criteria criteria = session.createCriteria( Emp.class );
Criterion[] criterions = new Criterion[] {
Restrictions.in( "empType",new Object[]{'P','C','R' )};
for ( Criterion criterion : criterions ) {
criteria.add( criterion );
}
criteria.setProjection( Projections.projectionList( )
.add( Projections.property( fieldName1 )) .add( Projections.property( fieldName2 ) ) );
List result=criteria.list( );
(vi) Return Result based on descending order
Criteria criteria = session.createCriteria( Emp.class );
Criterion[] criterions = new Criterion[] {
Restrictions.in( "empType",new Object[]{'P','C','R' )};
for ( Criterion criterion : criterions ) {
criteria.add( criterion );
}
crietrai.addOrder( Order.desc("empName") )
List result=criteria.list( );
Tuesday, May 18, 2010
Named Queries
@NamedQueries({
@NamedQuery(
name = "findEmpByAge",
query = "from emp e where e.age>?"
),
@NamedQuery(
name = "findEmpByRole",
query = "from emp e where e.role=?"
)
})
Adding additional fields in @Entity Class
Thursday, February 18, 2010
Hibernate Sequence Generator
Hibernate generator element generates the primary key for new record. This is used in conjunction with the Id element. Hibernate provides about 11 types of generator .Among these the Sequence generator uses the Sequence from the Database and returns identifier value of type long,short or int.
To use the Sequence from the database,need to map the field for Id, and specify the Generation Type as Sequence. By default the sequence generator provides values that are incremented in counts of 50,this can be overridden by specifying the allocationSize
@Entity( name = "EmployeeEntity" )
@SequenceGenerator( name = "id_sequence", allocationSize = 1, sequenceName = "SEQ_EMP_ID" )
@Table( name = "T_EMP_MASTER" )
public class EmployeeEntity implements Serializable {
@Id
@GeneratedValue( strategy = GenerationType.SEQUENCE, generator = "id_sequence" )
@Column( name = "emp_id" )
private Long empId;
@Column(name="firstName")
private String firstName;
@Column(name="lastName")
private String lastName;
//Getters and Setters
}