Announcement Announcement Module
Collapse
No announcement yet.
Trouble retriving row using Hibernate Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Trouble retriving row using Hibernate

    I am trying to retrieve one row from my MySQL database using hibernate 3.05 and Spring 1.2.3 on Windows XP and am unsuccessful. Here is the mapping for my Player object:

    Code:
    <hibernate-mapping>
      <class name="bnice.golf.data.Player" table="PLAYER">
        
        <id name="playerId" column="PLAYER_ID" unsaved-value="null">	
          <generator class="identity"/>
        </id>
    
        <property name="username" type="string" column="username" length="16" not-null="true"/>
        <property name="password" length="16" column="password" not-null="true"/>
        <property name="firstName" column="first_name" length="16" not-null="true"/>
        <property name="lastName" column="last_name" length="16" not-null="true"/>
        <property name="handicap"/>
        
        <many-to-one name="address" column="address_id" class="Address"/>
        
      </class>
    </hibernate-mapping>
    My -servlet.xml file for Spring:
    Code:
    	<!-- DATABASE -->
    	<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    		<property name="jndiName">
    			<value>java&#58;comp/env/jdbc/myGolf</value>
    		</property>
    	</bean>
    	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    		<property name="dataSource">
    			<ref bean="dataSource"/>
    		</property>
    		<property name="hibernateProperties">
    			<props>
    				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
    				<prop key="hibernate.show_sql">true</prop>
    			</props>
    		</property>
    		<property name="mappingDirectoryLocations">
    			<list>
    				<value>classpath&#58;/bnice/golf/data</value>
    			</list>
    		</property>		
    	</bean>		
    
    	<!-- DAO -->
    	<bean id="playerDAO" class="bnice.golf.dao.PlayerDAOImpl">
    		<property name="sessionFactory">
    			<ref bean="sessionFactory"/>
    		</property>
    	</bean>
    And finally my code for the DAO:
    Code:
    	public Player login&#40;final String username, final String password&#41; &#123;
    		HibernateTemplate template = new HibernateTemplate&#40;this.sessionFactory&#41;;
    		return &#40;Player&#41; template.execute&#40;new HibernateCallback&#40;&#41; &#123;
    			public Object doInHibernate&#40;Session session&#41; throws HibernateException &#123;
    				Query query = session.createQuery&#40;
    					"from bnice.golf.data.Player player where player.username=? and password=?"&#41;;
    				query.setString&#40;0, username&#41;;
    				query.setString&#40;1, password&#41;;
    				System.out.println&#40;"Size&#58;"+query.list&#40;&#41;.size&#40;&#41;+" "+username+" "+password+" "+query.getQueryString&#40;&#41;&#41;;
    				return query.uniqueResult&#40;&#41;;
    			&#125;
    		&#125;&#41;;
    	&#125;
    It looks like I have the database setup correctly because if instead of calling this DAO, I use some straightforward JDCB code in my controller, it works perfectly:

    Code:
      DataSource ds = null;
    
    	        try &#123;
    	            try &#123;
    	                Context ic = &#40;Context&#41; new InitialContext&#40;&#41;.lookup&#40;"java&#58;comp/env"&#41;;
    
    	                ds = &#40;DataSource&#41; ic.lookup&#40;"jdbc/myGolf"&#41;;
    	            &#125; catch &#40;Exception e&#41; &#123;
    	                System.out.println&#40;"Error getting DS."&#41;;
    	            &#125;
    
    	            Connection con = ds.getConnection&#40;&#41;;
    	            Statement st = con.createStatement&#40;&#41;;
    	            ResultSet rs = st.executeQuery&#40;"Select * from player where username='bcnice99"&#41;;
    	            if &#40;rs.next&#40;&#41;&#41; &#123;
    	            	System.out.println&#40;"Found"&#41;;
    	            &#125;
    	            else &#123;
    	            	System.out.println&#40;"not Found"&#41;;
    	            &#125;
    	            con.close&#40;&#41;;
    	        &#125; catch &#40;Exception e&#41; &#123;
    	            System.out.println&#40;"Error getting DataSource"&#41;;
    	        &#125;
    			return showForm&#40;req, res, errors&#41;;
    This is my first attempt at this so it is probably something simple. Thanks for the help!

    Brian

  • #2
    Can you describe what happens when you try to run it?

    Comment


    • #3
      The println in the DAO code:
      Code:
                 System.out.println&#40;"Size&#58;"+query.list&#40;&#41;.size&#40;&#41;+" "+username+" "+password+" "+query.getQueryString&#40;&#41;&#41;;
      always prints:

      Code:
      "Size&#58;0 bcnice99 password from bnice.golf.data.Player player where player.username=? and player.password=?
      So it never finds a row, even though there is a row in the database that matches. I never get any sort of error, so I guess the query is getting run? Contrast to that when I run using straight JDBC it does find a row in the database and prints "Found!"

      Thanks for the help!
      BRian

      Comment


      • #4
        Your SQL query is not the same as your Hibernate query. Your Hibernate one looks for a player with a username and password of what is passed in, whereas your SQL query only looks based on username. This explains the difference in results.

        Comment


        • #5
          I did that for simplicity sake. I can change the query in the JDBC to include the password and I am able to find a row. I wrote the JDBC part just to make sure that I could retrieve a database connection ok and that it was set up correctly. So there is something else that I am doing wrong I guess with the Hibernate part.
          Thanks for the help
          Brian

          Comment


          • #6
            Found the problem - I thought my ant script was copying my hbm.xml files over to my WEB-INF/classes/../ directory but it wasn't. After I added those files to the correct directory, it started working.
            Brian

            Comment

            Working...
            X