-->
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.  [ 7 posts ] 
Author Message
 Post subject: Data duplication in Query output.
PostPosted: Wed Feb 04, 2009 9:42 am 
Newbie

Joined: Wed Feb 04, 2009 9:10 am
Posts: 4
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


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2009 10:03 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Hi, do your records all have the same "releaseId"?
It seems to me, that this is a first-level-cache problem. If they all have the same releaseId, hibernate thinks, that a record has already been fetched and thus returns the same object (the first one) again, instead of hydrating a new one.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2009 10:18 am 
Newbie

Joined: Wed Feb 04, 2009 9:10 am
Posts: 4
Hi mmerder,

Thank you very much for your response.

Yes, you are right. All the records have the same releaseID.

But that's how our data is, so, is there a way to get the correct output?

Thanks,
Rishikesh


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2009 10:35 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Well, an Id(entifier) should always be unique per definition. What is the primary key on the database? can you use a composite id or a surrogate key?

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2009 11:02 am 
Newbie

Joined: Wed Feb 04, 2009 9:10 am
Posts: 4
We do not have any primary key defined on this table. If we really do need to use a composite key, the entire row would end up becoming the composite key. I am not sure, why this table was designed this way. But, is there a work around?

Thanks,
Rishikesh


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2009 11:28 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Well, every table needs a primary key. If you can't change the table, then you really need to make all columns to primary key.
Another solution could be to create a view on this table where you put another surrogate primary key which is different for each row. But the cleaner solution would be to include all columns into your composite key (or change table structure ;-))

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2009 12:49 pm 
Newbie

Joined: Wed Feb 04, 2009 9:10 am
Posts: 4
Thanks for your responses mmerder. Let me try out your suggestions.

Thanks,
Rishikesh.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.