-->
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.  [ 3 posts ] 
Author Message
 Post subject: is session cleaning database table?
PostPosted: Thu Jun 07, 2007 8:40 am 
Newbie

Joined: Thu Jun 07, 2007 7:50 am
Posts: 2
Hi

I'm trying hibernate retrieve data from MySQL database. When I am running my FirstExample.java file, It first clean my database table 'users' and then retrieving data. this means my query doesn't have any data. it returns null.

Can any body tell me why it is happening.

here are my code -

FirstExample.java

Code:
package rohan.example;

import java.util.Iterator;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

/**
* @author rohan.chandane
*
*/
public class FirstExample {
   
   private static Logger log =Logger.getLogger(FirstExample.class);

     public static void main(String[] args) {
        listData();
        System.out.println("Data Dispyed");
        }

   
   public static void listData(){
   
      SessionFactory factory = new Configuration().configure().buildSessionFactory();
      Session session = factory.openSession();
      Transaction tx = null;

      try{
         tx = session.beginTransaction();
         String SQL_STRING = "from Users u";
         Query query = session.createQuery(SQL_STRING);
         System.out.println("after createQuery" );
         
         for (Iterator it = query.iterate(); it.hasNext();) {
               Users user = (Users) it.next();
               System.out.println("User first name " + user.getFirstName() );
               System.out.println("User last name " + user.getLastName() );
         }
         
         tx.commit();
         session.close();
      }
      catch(HibernateException e){
         System.out.println("Exception : "+ e);
      }
   }
}


Hibernate Mapping file -

users.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
   
<hibernate-mapping package="rohan.example">
  <class name="rohan.example.Users" table="users">
   <id name="id" column="id" type="java.lang.Integer">
      <generator class="native"/>
   </id>
   <property name="firstName" length="50" column="first_name" type="java.lang.String"/>
   <property name="lastName" length="50" column="last_name" type="java.lang.String"/>
  </class>
</hibernate-mapping>


Hibernate Conf file -

Hibernate.cfg.xml
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>
      <!-- properties -->
      <property name="show_sql">true</property>
      <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
      <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
      <property name="hibernate.connection.url">jdbc:mysql://localhost/test</property>
      <property name="hibernate.connection.username">wss</property>
      <property name="hibernate.connection.password">wss</property>
      
       <property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
       <property name="current_session_context_class">thread</property>
       <property name="hibernate.show_sql">true</property>
       <property name="hibernate.hbm2ddl.auto">create</property>
      
      <!-- mapping resource -->
      <mapping resource="rohan/example/users.hbm.xml"/>
   </session-factory>
</hibernate-configuration>


Pojo file -

Users.java
Code:
package rohan.example;


/**
* @author rohan.chandane
*
*/
public class Users {
   
   private Integer id;
   private String firstName;
   private String lastName;

   /** full constructor */
   public Users(Integer id, String firstName, String lastName){
      this.id = id;
      this.firstName = firstName;
      this.lastName = lastName;
   }

   /** default constructor */
   public Users(){
   }
   
   /**
    * @return the firstName
    */
   public String getFirstName() {
      return firstName;
   }

   /**
    * @param firstName the firstName to set
    */
   public void setFirstName(String firstName) {
      this.firstName = firstName;
   }

   /**
    * @return the id
    */
   public Integer getId() {
      return id;
   }

   /**
    * @param id the id to set
    */
   public void setId(Integer id) {
      this.id = id;
   }

   /**
    * @return the lastName
    */
   public String getLastName() {
      return lastName;
   }

   /**
    * @param lastName the lastName to set
    */
   public void setLastName(String lastName) {
      this.lastName = lastName;
   }
   
   
}


Thanks

_________________
Rohan Chandane


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 07, 2007 10:27 am 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
Quote:
<property name="hibernate.hbm2ddl.auto">create</property>


creates the database schema when you instantiate your SessionFactory, probably erasing whatever used to be there

http://www.hibernate.org/hib_docs/v3/re ... ation.html

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 07, 2007 10:33 am 
Newbie

Joined: Thu Jun 07, 2007 7:50 am
Posts: 2
hey thanks buddy! its working now!

Cheers,
Rohan Chandane

_________________
Rohan Chandane


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