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: Incorrect data in returned records
PostPosted: Tue Mar 28, 2006 2:55 pm 
Newbie

Joined: Tue Mar 28, 2006 2:19 pm
Posts: 4
I'm creating a Java based web application that uses hibernate for database access. I've had success until now. For some reason there are two columns in the records created after a query do not reflect what is in the database.

Hibernate version: 3

No errors occur.

Name and version of the database: DB2 v8.1.2.169

Here is the relationship mapping.
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
  "-//Hibernate/Hibernate Mapping DTD//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping.dtd">

<hibernate-mapping>
   <class name="com.ibm.acin.beans.Relationship"
      table="RELATIONSHIP">
      <id name="schoolid" column="SCHOOLID">
         <generator class="assigned" />
      </id>
      <property name="volunteerid" column="INTRANETID" />
      <property name="role" column="ROLE" />
      <property name="otherrole" column="OTHER" />
   </class>

</hibernate-mapping>

And the hibernate mapping.
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>

      <!-- Database connection settings -->
      <property name="connection.driver_class">
         COM.ibm.db2.jdbc.app.DB2Driver
      </property>
      <property name="connection.url">jdbc:db2:SAMPLE</property>
      <property name="connection.username">Administrator</property>
      <property name="connection.password">mypass</property>

      <!-- JDBC connection pool (use the built-in) -->
      <property name="connection.pool_size">1</property>

      <!-- SQL dialect -->
      <property name="dialect">
         org.hibernate.dialect.DB2Dialect
      </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>

      <!-- Drop and re-create the database schema on startup
         <property name="hbm2ddl.auto">create</property>-->

      <mapping resource="com/ibm/acin/beans/Fark.hbm.xml" />
      <mapping resource="com/ibm/acin/beans/School.hbm.xml" />
      <mapping resource="com/ibm/acin/beans/Relationship.hbm.xml" />
      <mapping resource="com/ibm/acin/beans/Volunteer.hbm.xml" />

   </session-factory>

</hibernate-configuration>



The following code is in the manager class that executes the query itself.




Code:
    private void listWeight() {
        isClosedOpen();
        HibernateUtil.getSessionFactory().getCurrentSession()
                .beginTransaction();
        List records = HibernateUtil.getSessionFactory().getCurrentSession()
                .createQuery("from Relationship").list();
             
        relationship = (Relationship[]) records
                .toArray(new Relationship[records.size()]);
       
        HibernateUtil.getSessionFactory().getCurrentSession().getTransaction()
                .commit();
        return;
    }




Here's what's in the database. This data is inserted programmatically.

Quote:
000003 meyou 1 1X
000003 meyou 2 2X
000003 meyou 3 3X
000003 meyou 4 4X

And this is what is displayed on my JSF JSP.
Quote:
000003 meyou 1 1X
000003 meyou 1 1X
000003 meyou 1 1X
000003 meyou 1 1X

At no point does the manager have the correct data, records seems to 'come out' corrupted already. Any ideas why this is doing this? So far, this is the only class that has this problem but trying to replicate the database has not proved effective.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 28, 2006 3:21 pm 
Expert
Expert

Joined: Mon Jan 09, 2006 5:01 pm
Posts: 311
Location: Sacramento, CA
few things to look at:

1) mapping file to make sure the column is correctly specified.
2) you aren't reusing a session object that is stored in some cached sessions from your HibernateUtil - you will get back the data as it was originally stored or loaded from that session - and only get back changes that are made through that session - the session in a sense is a snap-shot.
3) your print statement is not printing properly or looping through columns correct from the list iterator.

_________________
-JT

If you find my replies helpful, please rate by clicking 'Y' on them. I appreciate it.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 28, 2006 3:46 pm 
Newbie

Joined: Tue Mar 28, 2006 2:19 pm
Posts: 4
I've check the mapping file quite a few times now. If you have any suggestions on how to change it I'm all ears.

As for the iterator in suggestion three, there is none.

For reference, here is my HibernateUtil class. Anything wrong with it?
Code:
public class HibernateUtil {

    private static SessionFactory sessionFactory;

    static {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            sessionFactory = new Configuration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public  static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 28, 2006 3:54 pm 
Expert
Expert

Joined: Mon Jan 09, 2006 5:01 pm
Posts: 311
Location: Sacramento, CA
how do you print out "relationship" array?

_________________
-JT

If you find my replies helpful, please rate by clicking 'Y' on them. I appreciate it.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 28, 2006 4:46 pm 
Newbie

Joined: Tue Mar 28, 2006 2:19 pm
Posts: 4
I don't actually iterate through the list. There is a JSF structure that takes care of the display. If you'd like to look at the code it's included below.

Some additional weird behavior is that if I use a select statement that selects on that third or forth column, I will find the record as it appears in the database. If I try to select all the records in the table, they come out corrupted in the manner pointed out in the original post. What's the deal?


Code:
package com.ibm.acin.beans;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.faces.context.FacesContext;

public class RelationshipManager implements java.io.Serializable {

    private List logsjsf;

    private Relationship[] relationship;

    private String selection;

    /**
     * @return Returns the selection.
     */
    public String getSelection() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Map params = facesContext.getExternalContext().getRequestParameterMap();
        String kk = (String) params.get("entryID");
        System.out.println("getSelection entryID" + kk);
        return kk;

    }

    /**
     * @param selection
     *            The selection to set.
     */
    public void setSelection(String selection) {
        this.selection = selection;
    }

    public RelationshipManager() {
        listWeight();
    }

    private static void isClosedOpen() {
        if (HibernateUtil.getSessionFactory().isClosed()) {
            HibernateUtil.getSessionFactory().openSession();
        }
    }

    private static void open() {
        HibernateUtil.getSessionFactory().openSession();
    }

    private static void close() {
        HibernateUtil.getSessionFactory().close();
    }

    public static void deleteWeight(Relationship curBean) {
        isClosedOpen();
        HibernateUtil.getSessionFactory().getCurrentSession()
                .beginTransaction();
        HibernateUtil.getSessionFactory().getCurrentSession().delete(curBean);
        HibernateUtil.getSessionFactory().getCurrentSession().getTransaction()
                .commit();
    }

    public static void storeWeight(Relationship curBean) {
        isClosedOpen();
        HibernateUtil.getSessionFactory().getCurrentSession()
                .beginTransaction();
        HibernateUtil.getSessionFactory().getCurrentSession().save(curBean);
        HibernateUtil.getSessionFactory().getCurrentSession().getTransaction()
                .commit();
    }

    public static void updateWeight(Relationship curBean) {
        isClosedOpen();
        HibernateUtil.getSessionFactory().getCurrentSession()
                .beginTransaction();
        HibernateUtil.getSessionFactory().getCurrentSession().update(curBean);
        HibernateUtil.getSessionFactory().getCurrentSession().getTransaction()
                .commit();
    }

    private void listWeight() {
        isClosedOpen();
        HibernateUtil.getSessionFactory().getCurrentSession()
                .beginTransaction();
        List records = HibernateUtil.getSessionFactory().getCurrentSession()
                .createQuery("from Relationship").list();
             
        relationship = (Relationship[]) records
                .toArray(new Relationship[records.size()]);
       
        HibernateUtil.getSessionFactory().getCurrentSession().getTransaction()
                .commit();
        return;
    }

    /*
     * This method gets all records with email of email and school id of
     * schoolid. Then it searches through those records returned for roles
     * passed in. For all those roles that exist in the records - false, for all
     * those not found - true.
     * 
     */
    private void findEntries(String schoolid, String email, boolean[] roles,
            boolean[] foundroles, String[] ROLETYPE) {
        isClosedOpen();
        HibernateUtil.getSessionFactory().getCurrentSession()
                .beginTransaction();
        List records = HibernateUtil.getSessionFactory().getCurrentSession()
                .createQuery(
                        "from Relationship as relationship where schoolid='"
                                + schoolid + "' AND intranetid LIKE '" + email
                                + "'").list();
        relationship = null;
        relationship = (Relationship[]) records
                .toArray(new Relationship[records.size()]);

        HibernateUtil.getSessionFactory().getCurrentSession().getTransaction()
                .commit();

        System.out.println("    OUTPUT   ");
        for(int i=0; i<relationship.length;i++){

            System.out.println("    " + relationship[i].getRole() + "  " + relationship[i].getSchoolid() + "  "  + relationship[i].getVolunteerid());
        }
       
       
        // User has not signed up before for this schoool, no entries
        if (relationship.length == 0) {
            // True means they haven't been found, so insert them
            foundroles[0] = true;
            foundroles[1] = true;
            foundroles[2] = true;
            foundroles[3] = true;
        } else { // search for all of the roles

            for (int i = 0; i < 4; i++) {
                if (roles[i]) { // If the user signed up for this role
                    System.out.println(" ROLETYPE = " + ROLETYPE[i]);
                    if (searchLogsForRoleOf(ROLETYPE[i])) {
                        // we've found the thing, set false so we don't insert
                        // later
                        foundroles[i] = false;
                    } else {
                        // the role is not in the gathered records, set true to
                        // insert later
                        foundroles[i] = true;
                    }
                } else{
                    // If the user has not attempted to sign up for this role, make sure to
                    // set to false to prevent insert later
                    foundroles[i] = false;
                }
            }

        }
    }

    /**
     *
     * Returns true or false, depending on whether or not the roletype was found
     * in the logs.
     *
     * @param string
     * @return
     */
    private boolean searchLogsForRoleOf(String role) {

        for (int i = 0; i < relationship.length; i++) {
            System.out.println("    " + relationship[i].getRole() + "  " + role);
            if (relationship[i].getRole().equalsIgnoreCase(role)) {
                return true;
            }
        }
        return false;
    }

    public static void addLogs(List newLogs) {
        for (Iterator i = newLogs.iterator(); i.hasNext();) {
            storeWeight((Relationship) i.next());
        }
    }

    public static void deleteLogs(List delLogs) {
        for (Iterator i = delLogs.iterator(); i.hasNext();) {
            deleteWeight((Relationship) i.next());
        }
    }

    public static void updateLogs(List delLogs) {
        for (Iterator i = delLogs.iterator(); i.hasNext();) {
            updateWeight((Relationship) i.next());
        }
    }

    /**
     * @return Returns the logs.
     */
    public Relationship[] getLogs() {
        //return (LogWeightBean[]) logs.toArray(new
        // LogWeightBean[logs.size()]);
        listWeight();
        return relationship;
    }

    /**
     * @param ivLogWeightBean
     *            The ivLogWeightBean to set.
     */
    public void setLogs(Relationship[] beans) {
        //logs = Arrays.asList(beans);
        relationship = beans;
        return;
    }

    /**
     * @return Returns the logs.
     */
    public List getLogsjsf() {
        listWeight();
        //return (LogWeightBean[]) logs.toArray(new
        // LogWeightBean[logs.size()]);
        if (logsjsf == null) {
            logsjsf = Arrays.asList(relationship);
        }
        return logsjsf;
    }

    /**
     * @param ivLogWeightBean
     *            The ivLogWeightBean to set.
     */
    public void setLogsjsf(List logs) {
        //Arrays.asList(logs);
        logsjsf = logs;
        return;
    }

    /**
     * Returns the elements of List B that are not in List A
     *
     * @param A
     * @param B
     */
    private List compareLists(List A, List B) {
        List newElems = new ArrayList();
        for (Iterator i = B.iterator(); i.hasNext();) {
            Relationship curBean = (Relationship) i.next();
            if (!A.contains(curBean)) {
                //elements that are not in List A
                newElems.add(curBean);
            }
        }
        return newElems;
    }

    private void createAndStoreRelationship(String schoolid, String userid,
            String role, String verified) {

        try {
            HibernateUtil.getSessionFactory().getCurrentSession()
                    .beginTransaction();

            Relationship theRelationship = new Relationship();
            theRelationship.setSchoolid(schoolid);
            theRelationship.setVolunteerid(userid);
            theRelationship.setRole(role);
            theRelationship.setOtherrole(verified);

            HibernateUtil.getSessionFactory().getCurrentSession().save(
                    theRelationship);

            HibernateUtil.getSessionFactory().getCurrentSession()
                    .getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void addRelationship(String schoolid, String email, String role,
            boolean verified) {

        this.createAndStoreRelationship(schoolid, email, role, role+"X");

    }

    public String viewSelection() {

        System.out.println("viewSelection - schoolid");
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Map params = facesContext.getExternalContext().getRequestParameterMap();
        String kk = (String) params.get("schoolid");
        System.out.println(kk);
        return kk;
    }

    /**
     * @param string
     */
    public Relationship getBeanByID(String schoolid) {
        // TODO Auto-generated method stub
        System.out
                .println("getBeanByID - We are searching for the bean on school id = "
                        + schoolid);
        return null;
    }

    /**
     * @param schoolid
     * @param email
     * @param string
     * @return
     */
    public void checkExistingRelationships(String schoolid, String email,
            boolean[] roles, boolean[] foundroles, String[] ROLETYPE) {

        this.findEntries(schoolid, email, roles, foundroles, ROLETYPE);
    }

}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 28, 2006 4:55 pm 
Expert
Expert

Joined: Mon Jan 09, 2006 5:01 pm
Posts: 311
Location: Sacramento, CA
did you debug this section of code with a debugger:

Code:
List records = HibernateUtil.getSessionFactory().getCurrentSession().createQuery(
   "from Relationship as relationship where schoolid='"
         + schoolid + "' AND intranetid LIKE '" + email
         + "'").list();
relationship = null;
relationship = (Relationship[]) records
.toArray(new Relationship[records.size()]);

HibernateUtil.getSessionFactory().getCurrentSession().getTransaction()
.commit();


to make sure the relationship array is as expected. Just want to elimanate that as a possibility at this point.

_________________
-JT

If you find my replies helpful, please rate by clicking 'Y' on them. I appreciate it.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 04, 2006 4:34 pm 
Newbie

Joined: Tue Mar 28, 2006 2:19 pm
Posts: 4
The relationship array does not contain the correct information. Neither does the records array. Data from the table is corrupted for only that 'role' column and any column that was added afterwards.


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.