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