-->
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.  [ 4 posts ] 
Author Message
 Post subject: Connecting to database but Query returns 0 results??
PostPosted: Sun Jun 06, 2010 11:14 pm 
Newbie

Joined: Sun Jun 06, 2010 11:00 pm
Posts: 5
Hey I'm using Hibernate with Eclipse and I seem to have set this up correctly and I'm connecting to my petclinic database. I gathered that I'm successfully connecting becase if I change the password for the MySQL database in the configuration file to an invalid one I get a connection error. My problem is that my query
is not returning any results when I do have data in the table that I'm querying.

This is my java testing code:

Code:
public class Test {
  public static void main(String[] args) {
  Session session = null;

  try{
    // This step will read hibernate.cfg.xml
   //and prepare hibernate for use
    SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
    session = sessionFactory.openSession();
         
   String SQL_QUERY ="from Pet pets";
   
   Query query = session.createQuery(SQL_QUERY);
   
   Iterator it2 = query.iterate();
   
   if(it2.hasNext() == false){
      System.out.println("No elements\n");
   }else{
      System.out.println("Elements Exist\n");
   }
   
   for(Iterator it=query.iterate();it.hasNext();){
      Pet pet=(Pet)it.next();
      System.out.println("ID: " + pet.getId());
      System.out.println("Pet Name: " + pet.getName());
   }
   
        session.close();
  }catch(Exception e){
    System.out.println(e.getMessage());
   
  }finally{
    }
  }


This is what the output in Eclipse is when I run this code:

2010-06-07 12:55:21,249 INFO [org.hibernate.cfg.Environment] - <Hibernate 3.3.2.GA>
2010-06-07 12:55:21,250 INFO [org.hibernate.cfg.Environment] - <hibernate.properties not found>
2010-06-07 12:55:21,253 INFO [org.hibernate.cfg.Environment] - <Bytecode provider name : javassist>
2010-06-07 12:55:21,256 INFO [org.hibernate.cfg.Environment] - <using JDK 1.4 java.sql.Timestamp handling>
2010-06-07 12:55:21,301 INFO [org.hibernate.cfg.Configuration] - <configuring from resource: /hibernate.cfg.xml>
2010-06-07 12:55:21,301 INFO [org.hibernate.cfg.Configuration] - <Configuration resource: /hibernate.cfg.xml>
2010-06-07 12:55:21,356 INFO [org.hibernate.cfg.Configuration] - <Reading mappings from resource : petclinic.hbm.xml>
2010-06-07 12:55:21,401 INFO [org.hibernate.cfg.HbmBinder] - <Mapping class: org.springframework.samples.petclinic.Vet -> vets>
2010-06-07 12:55:21,416 INFO [org.hibernate.cfg.HbmBinder] - <Mapping collection: org.springframework.samples.petclinic.Vet.specialtiesInternal -> vet_specialties>
2010-06-07 12:55:21,418 INFO [org.hibernate.cfg.HbmBinder] - <Mapping class: org.springframework.samples.petclinic.Specialty -> specialties>
2010-06-07 12:55:21,418 INFO [org.hibernate.cfg.HbmBinder] - <Mapping class: org.springframework.samples.petclinic.Owner -> owners>
2010-06-07 12:55:21,419 INFO [org.hibernate.cfg.HbmBinder] - <Mapping class: org.springframework.samples.petclinic.Pet -> pets>
2010-06-07 12:55:21,458 INFO [org.hibernate.cfg.HbmBinder] - <Mapping class: org.springframework.samples.petclinic.PetType -> types>
2010-06-07 12:55:21,459 INFO [org.hibernate.cfg.HbmBinder] - <Mapping class: org.springframework.samples.petclinic.Visit -> visits>
2010-06-07 12:55:21,459 INFO [org.hibernate.cfg.Configuration] - <Configured SessionFactory: null>
2010-06-07 12:55:21,460 INFO [org.hibernate.cfg.HbmBinder] - <Mapping collection: org.springframework.samples.petclinic.Owner.petsInternal -> pets>
2010-06-07 12:55:21,460 INFO [org.hibernate.cfg.HbmBinder] - <Mapping collection: org.springframework.samples.petclinic.Pet.visitsInternal -> visits>
2010-06-07 12:55:21,465 INFO [org.hibernate.connection.DriverManagerConnectionProvider] - <Using Hibernate built-in connection pool (not for production use!)>
2010-06-07 12:55:21,465 INFO [org.hibernate.connection.DriverManagerConnectionProvider] - <Hibernate connection pool size: 10>
2010-06-07 12:55:21,465 INFO [org.hibernate.connection.DriverManagerConnectionProvider] - <autocommit mode: false>
2010-06-07 12:55:21,470 INFO [org.hibernate.connection.DriverManagerConnectionProvider] - <using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost/hibernatetutorial>
2010-06-07 12:55:21,470 INFO [org.hibernate.connection.DriverManagerConnectionProvider] - <connection properties: {user=root, password=****}>
2010-06-07 12:55:21,639 INFO [org.hibernate.cfg.SettingsFactory] - <RDBMS: MySQL, version: 5.1.46-community>
2010-06-07 12:55:21,639 INFO [org.hibernate.cfg.SettingsFactory] - <JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-5.1.12 ( Revision: ${bzr.revision-id} )>
2010-06-07 12:55:21,653 INFO [org.hibernate.dialect.Dialect] - <Using dialect: org.hibernate.dialect.MySQLDialect>
2010-06-07 12:55:21,656 INFO [org.hibernate.transaction.TransactionFactoryFactory] - <Using default transaction strategy (direct JDBC transactions)>
2010-06-07 12:55:21,657 INFO [org.hibernate.transaction.TransactionManagerLookupFactory] - <No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)>
2010-06-07 12:55:21,657 INFO [org.hibernate.cfg.SettingsFactory] - <Automatic flush during beforeCompletion(): disabled>
2010-06-07 12:55:21,657 INFO [org.hibernate.cfg.SettingsFactory] - <Automatic session close at end of transaction: disabled>
2010-06-07 12:55:21,657 INFO [org.hibernate.cfg.SettingsFactory] - <JDBC batch size: 15>
2010-06-07 12:55:21,657 INFO [org.hibernate.cfg.SettingsFactory] - <JDBC batch updates for versioned data: disabled>
2010-06-07 12:55:21,657 INFO [org.hibernate.cfg.SettingsFactory] - <Scrollable result sets: enabled>
2010-06-07 12:55:21,657 INFO [org.hibernate.cfg.SettingsFactory] - <JDBC3 getGeneratedKeys(): enabled>
2010-06-07 12:55:21,658 INFO [org.hibernate.cfg.SettingsFactory] - <Connection release mode: auto>
2010-06-07 12:55:21,658 INFO [org.hibernate.cfg.SettingsFactory] - <Maximum outer join fetch depth: 2>
2010-06-07 12:55:21,658 INFO [org.hibernate.cfg.SettingsFactory] - <Default batch fetch size: 1>
2010-06-07 12:55:21,658 INFO [org.hibernate.cfg.SettingsFactory] - <Generate SQL with comments: disabled>
2010-06-07 12:55:21,659 INFO [org.hibernate.cfg.SettingsFactory] - <Order SQL updates by primary key: disabled>
2010-06-07 12:55:21,659 INFO [org.hibernate.cfg.SettingsFactory] - <Order SQL inserts for batching: disabled>
2010-06-07 12:55:21,659 INFO [org.hibernate.cfg.SettingsFactory] - <Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory>
2010-06-07 12:55:21,660 INFO [org.hibernate.hql.ast.ASTQueryTranslatorFactory] - <Using ASTQueryTranslatorFactory>
2010-06-07 12:55:21,660 INFO [org.hibernate.cfg.SettingsFactory] - <Query language substitutions: {}>
2010-06-07 12:55:21,660 INFO [org.hibernate.cfg.SettingsFactory] - <JPA-QL strict compliance: disabled>
2010-06-07 12:55:21,660 INFO [org.hibernate.cfg.SettingsFactory] - <Second-level cache: enabled>
2010-06-07 12:55:21,660 INFO [org.hibernate.cfg.SettingsFactory] - <Query cache: disabled>
2010-06-07 12:55:21,660 INFO [org.hibernate.cfg.SettingsFactory] - <Cache region factory : org.hibernate.cache.impl.NoCachingRegionFactory>
2010-06-07 12:55:21,660 INFO [org.hibernate.cfg.SettingsFactory] - <Optimize cache for minimal puts: disabled>
2010-06-07 12:55:21,660 INFO [org.hibernate.cfg.SettingsFactory] - <Structured second-level cache entries: disabled>
2010-06-07 12:55:21,663 INFO [org.hibernate.cfg.SettingsFactory] - <Echoing all SQL to stdout>
2010-06-07 12:55:21,663 INFO [org.hibernate.cfg.SettingsFactory] - <Statistics: disabled>
2010-06-07 12:55:21,663 INFO [org.hibernate.cfg.SettingsFactory] - <Deleted entity synthetic identifier rollback: disabled>
2010-06-07 12:55:21,663 INFO [org.hibernate.cfg.SettingsFactory] - <Default entity-mode: pojo>
2010-06-07 12:55:21,663 INFO [org.hibernate.cfg.SettingsFactory] - <Named query checking : enabled>
2010-06-07 12:55:21,689 INFO [org.hibernate.impl.SessionFactoryImpl] - <building session factory>
2010-06-07 12:55:21,797 INFO [org.hibernate.impl.SessionFactoryObjectFactory] - <Not binding factory to JNDI, no JNDI name configured>
2010-06-07 12:55:21,801 INFO [org.hibernate.tool.hbm2ddl.SchemaUpdate] - <Running hbm2ddl schema update>
2010-06-07 12:55:21,801 INFO [org.hibernate.tool.hbm2ddl.SchemaUpdate] - <fetching database metadata>
2010-06-07 12:55:21,802 INFO [org.hibernate.tool.hbm2ddl.SchemaUpdate] - <updating schema>
2010-06-07 12:55:21,818 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] - <table found: hibernatetutorial.owners>
2010-06-07 12:55:21,818 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] - <columns: [id, first_name, address, last_name, telephone, city]>
2010-06-07 12:55:21,818 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] - <foreign keys: []>
2010-06-07 12:55:21,818 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] - <indexes: [primary]>
2010-06-07 12:55:21,828 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] - <table found: hibernatetutorial.pets>
2010-06-07 12:55:21,828 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] - <columns: [id, birth_date, name, owner_id, type_id]>
2010-06-07 12:55:21,828 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] - <foreign keys: [fk34733445829aa6, fk34733486a9b587]>
2010-06-07 12:55:21,828 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] - <indexes: [fk34733445829aa6, primary, fk34733486a9b587]>
2010-06-07 12:55:21,837 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] - <table found: hibernatetutorial.specialties>
2010-06-07 12:55:21,837 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] - <columns: [id, name]>
2010-06-07 12:55:21,837 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] - <foreign keys: []>
2010-06-07 12:55:21,837 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] - <indexes: [primary]>
2010-06-07 12:55:21,843 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] - <table found: hibernatetutorial.types>
2010-06-07 12:55:21,843 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] - <columns: [id, name]>
2010-06-07 12:55:21,843 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] - <foreign keys: []>
2010-06-07 12:55:21,843 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] - <indexes: [primary]>
2010-06-07 12:55:21,849 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] - <table found: hibernatetutorial.vet_specialties>
2010-06-07 12:55:21,849 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] - <columns: [vet_id, specialty_id]>
2010-06-07 12:55:21,849 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] - <foreign keys: [fk41fd6482b8ce6f27, fk41fd648246827f87]>
2010-06-07 12:55:21,849 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] - <indexes: [primary, fk41fd6482b8ce6f27, fk41fd648246827f87]>
2010-06-07 12:55:21,855 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] - <table found: hibernatetutorial.vets>
2010-06-07 12:55:21,855 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] - <columns: [id, first_name, last_name]>
2010-06-07 12:55:21,855 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] - <foreign keys: []>
2010-06-07 12:55:21,855 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] - <indexes: [primary]>
2010-06-07 12:55:21,861 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] - <table found: hibernatetutorial.visits>
2010-06-07 12:55:21,861 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] - <columns: [id, pet_id, visit_date, description]>
2010-06-07 12:55:21,861 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] - <foreign keys: [fkcf595c883c455547]>
2010-06-07 12:55:21,861 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] - <indexes: [fkcf595c883c455547, primary]>
2010-06-07 12:55:21,861 INFO [org.hibernate.tool.hbm2ddl.SchemaUpdate] - <schema update complete>
Hibernate: select pet0_.id as col_0_0_ from pets pet0_
No elements

Hibernate: select pet0_.id as col_0_0_ from pets pet0_


This is my hibernate.cfg.xml file:

Code:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
      <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
      <property name="hibernate.connection.url">jdbc:mysql://localhost/hibernatetutorial</property>
      <property name="hibernate.connection.username">root</property>
      <property name="hibernate.connection.password">reddark</property>
      <property name="hibernate.connection.pool_size">10</property>
      <property name="show_sql">true</property>
      <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
      <property name="hibernate.hbm2ddl.auto">update</property>
      <!-- Mapping files -->
      <mapping resource="petclinic.hbm.xml"/>
</session-factory>
</hibernate-configuration>


This is my petclinic.hbm.xml file

Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
      "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<!--
  - Mapping file for the Hibernate implementation of the Clinic interface.
   -->
<hibernate-mapping auto-import="true" default-lazy="false">

   <class name="org.springframework.samples.petclinic.Vet" table="vets">
      <id name="id" column="id">
         <generator class="identity"/>
      </id>
      <property name="firstName" column="first_name"/>
      <property name="lastName" column="last_name"/>
      <set name="specialtiesInternal" table="vet_specialties">
         <key column="vet_id"/>
         <many-to-many column="specialty_id" class="org.springframework.samples.petclinic.Specialty"/>
      </set>
   </class>

   <class name="org.springframework.samples.petclinic.Specialty" table="specialties">
      <id name="id" column="id">
         <generator class="identity"/>
      </id>
      <property name="name" column="name"/>
   </class>

   <class name="org.springframework.samples.petclinic.Owner" table="owners">
      <id name="id" column="id">
         <generator class="identity"/>
      </id>
      <property name="firstName" column="first_name"/>
      <property name="lastName" column="last_name"/>
      <property name="address" column="address"/>
      <property name="city" column="city"/>
      <property name="telephone" column="telephone"/>
      <set name="petsInternal" inverse="true" cascade="all">
         <key column="owner_id"/>
         <one-to-many class="org.springframework.samples.petclinic.Pet"/>
      </set>
   </class>

   <class name="org.springframework.samples.petclinic.Pet" table="pets">
      <id name="id" column="id">
         <generator class="identity"/>
      </id>
      <property name="name" column="name"/>
      <property name="birthDate" column="birth_date" type="date"/>
      <many-to-one name="owner" column="owner_id" class="org.springframework.samples.petclinic.Owner"/>
      <many-to-one name="type" column="type_id" class="org.springframework.samples.petclinic.PetType"/>
      <set name="visitsInternal" inverse="true" cascade="all">
         <key column="pet_id"/>
         <one-to-many class="org.springframework.samples.petclinic.Visit"/>
      </set>
   </class>

   <class name="org.springframework.samples.petclinic.PetType" table="types">
      <id name="id" column="id">
         <generator class="identity"/>
      </id>
      <property name="name" column="name"/>
   </class>

   <class name="org.springframework.samples.petclinic.Visit" table="visits">
      <id name="id" column="id">
         <generator class="identity"/>
      </id>
      <property name="date" column="visit_date" type="date"/>
      <property name="description" column="description"/>
      <many-to-one name="pet" column="pet_id" class="org.springframework.samples.petclinic.Pet"/>
   </class>

</hibernate-mapping>




Thanks heaps for your help, I just find it strange how none of my queries return any results.
The Pets table I'm querying has 13 pets in it.


Top
 Profile  
 
 Post subject: Re: Connecting to database but Query returns 0 results??
PostPosted: Mon Jun 07, 2010 3:35 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
I suggest you to log your sql-statements (and respective result-set) through p6spy (it's free).
Then you should see exactly how the sql-statements is executed against the database.
Copy the sql-statements and re-execute it again in a sql-tool (for example squirrel) to see if also there the query has 0 results.
Maybe your are using the wrong dialect.
I also suggest you to disable the automatic SchemaUpdate in your hibernate configuration.


Top
 Profile  
 
 Post subject: Re: Connecting to database but Query returns 0 results??
PostPosted: Mon Jun 07, 2010 7:13 am 
Newbie

Joined: Sun Jun 06, 2010 11:00 pm
Posts: 5
I downloaded the p6Spy View plugin for Eclipse. Not sure how I use this with my Test.java class which executes
my query? I can see the viewer is open but when I run that class as a java application nothing happens. Could you give some information, maybe a link on how I use this with Eclipse? I've been looking but haven't found anything.

Also I'm pretty sure I've got the dialect right
Code:
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>


I'm pretty much re-using the same hibernate.cfg.xml file I used in another java project in Eclipse which used Hibernate
and a My SQL database and that is working fine.

Thanks


Top
 Profile  
 
 Post subject: Re: Connecting to database but Query returns 0 results??
PostPosted: Mon Jun 07, 2010 8:06 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Sorry, I never used the p6spy plugin for Eclipse, I always use p6spy directly.


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