Showing posts with label Hibernate. Show all posts
Showing posts with label Hibernate. Show all posts

Monday, April 11, 2011

Derby sequence with Hibernate







Derby sequence

CREATE SEQUENCE MD_USER_SEQ
AS INT
START WITH 1;


Hibernate mapping to get User_id from sequence

<id name="userId" type="integer" column="USER_ID" >
<generator class="sequence">
<param name="sequence">MD_USER_SEQ</param>
</generator>
</id>

[update:]
after all there is an issue with DerbyDialect of hibernate.
So need the following fix. 



package entity;


import org.hibernate.dialect.DerbyDialect;


/**
 *
 * @author Srinath
 */
public class MyDerbyDialect extends DerbyDialect{


    @Override
public String getSequenceNextValString(String sequenceName) {
        return "values next value for " + sequenceName;
}
}


Then in hibernate config

  <property name="hibernate.dialect">entity.MyDerbyDialect</property>




Share Article : Derby sequence with Hibernate
Share/Save/Bookmark

Sunday, July 12, 2009

Hibernate querying and iteration using JSTL


1.If a query return a single raw with data from mulitple tables
(unique result with mulitple objects in-hibernate),
it returns array of Entity Objects.
A[0]
A[n]
....

2.If a query return multiple raws with data from mulitple tabels
it return List of Array objects.
L[0] ---
|+--- A[0]...A[m]
L[n] ---
|+--- A[0]...A[m]
.....

Code for situation 1

SQLQuery query = session.createSQLQuery(sql);
query.addEntity("person", Person.class);
query.addEntity("specializations", Entity.Specializations.class);

Object obj = query.uniqueResult();
// obj is an array
//A[0] will be a "person" and A[1] will be a "specializations" Object

View using JSTL

${objectArray[0].salutationText}. ${objectArray[0].nameWithInitials}
${objectArray[1].specializationText}



Code for situation 2

SQLQuery query = session.createSQLQuery(sql);
query.addEntity("session", Entity.Session.class);
query.addEntity("person", Person.class);

list = query.list();
// this return List of Arrays
// where L[n] A[0] -- "session" , A[1] -- "person"

Iterate using JSTL


${p[0].sessionId}
${p[0].sessionDate}
${p[1].name}
${p[1].address}





Share Article : Hibernate querying and iteration using JSTL
Share/Save/Bookmark