Hibernate

Simple Program Of Select in HQL
Previous Home Next
Step1-Create

hibernate.cfg.xml

<?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">

<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>

<session-factory>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
<property name="connection.username">system</property>
<property name="connection.password">system</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<mapping resource="Emp.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Step2-Create Persitent class

Emp.java

package mypack;

public class Emp {
int id;
String name,job;
int salary;
public Emp() {
super();
}
public Emp(String name, String job, int salary) {
super();
this.name = name;
this.job = job;
this.salary = salary;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
}

Emp.hbm.xml

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

<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-mapping>
<class name="mypack.Emp">
<id name="id" type="int"></id>
<property name="name"></property>
<property name="job"></property>
<property name="salary" type="int"></property>
</class>
</hibernate-mapping>

PersistTest.java

package mypack;
import java.util.*;
import org.hibernate.Query;
import org.hibernate.Session;
public class PersistTest {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
Session session=SessionProvider.getSession();
Query q=session.createQuery("from Emp e");
List<Emp> e=(List<Emp>) q.list();
Iterator<Emp> itr=e.iterator();
System.out.println("following data fetched");
while(itr.hasNext())
{
Emp emp=itr.next();
System.out.println(emp.getName()+"\t"+emp.getSalary());
}
session.close();
}
}
Previous Home Next

Tolal:0 Click:

Show All Comments

Did not find what you were looking for leave your name and message. We will revert within 24 hours
Name:
eMail:
Comment / Feedback: