Hi,
I have recently started using Hibernate. I have been going through the online documentation, to understand how to use Hibernate.
I am facing trouble with querying a table. This table has 14 records. On querying the table with using hibernate, the first record gets duplicated 14 times.
But, if I execute the query generated by Hibernate in a SQL client, I get the desired output, without any data duplication.
I am using hibernate3.3.1.GA
My database is Oracle 9i release 2.
The following are the classes and the xml files I am using.
Please let me know if I am missing anything.
Code:
public class UserRoles {
private String releaseId;
private String cycleCode;
private String roleName;
private String userId;
public UserRoles(){
}
/**
* @return the releaseId
*/
public String getReleaseId() {
return releaseId;
}
/**
* @param releaseId
* the releaseId to set
*/
public void setReleaseId(String releaseId) {
this.releaseId = releaseId;
}
/**
* @return the cycleCode
*/
public String getCycleCode() {
return cycleCode;
}
/**
* @param cycleCode
* the cycleCode to set
*/
public void setCycleCode(String cycleCode) {
this.cycleCode = cycleCode;
}
/**
* @return the roleName
*/
public String getRoleName() {
return roleName;
}
/**
* @param roleName
* the roleName to set
*/
public void setRoleName(String roleName) {
this.roleName = roleName;
}
/**
* @return the userId
*/
public String getuserId() {
return userId;
}
/**
* @param userId
* the userId to set
*/
public void setuserId(String userId) {
this.userId = userId;
}
Code:
import java.util.Iterator;
import java.util.List;
import org.hibernate.Session;
public class UserRolesManager {
public static void main(String[] args) {
UserRolesManager mgr = new UserRolesManager();
mgr.createAndStoreEvent();
HibernateUtil.getSessionFactory().close();
}
private void createAndStoreEvent() {
Session session = HibernateUtil.getSessionFactory().openSession();
List result = session.createQuery("from UserRoles").list();
Iterator iter = result.iterator();
while (iter.hasNext()) {
UserRoles userroles = new UserRoles();
userroles = (UserRoles) iter.next();
System.out
.print(" Release Id " + userroles.getReleaseId() + " ## ");
System.out
.print(" Cycle Code " + userroles.getCycleCode() + " ## ");
System.out.print(" Role Name " + userroles.getRoleName()
+ " ## ");
System.out.println(" User Id " + userroles.getuserId());
System.out.println(" ******************* ");
}
session.close();
}
}
UserRoles.hbm.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="UserRoles" table="USER_ROLES_T">
<id name="releaseId" column="RELEASE_ID">
<generator class="assigned"></generator>
</id>
<property name="cycleCode" column="CYCLE_CODE"/>
<property name="roleName" column="ROLE_NAME"/>
<property name="userId" column="USER_ID"/>
</class>
</hibernate-mapping>
hibernate.cfg.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory name="">
<!-- Database connection settings -->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">{dburl}</property>
<property name="connection.username">{username}</property>
<property name="connection.password">{password}</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<mapping resource="UserRoles.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Thanks,
Rishikesh