Hibernate

adplus-dvertising
Example of Select Program in HQL
Previous Home Next

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="Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>

Student.java

package mypack;

public class Student {
int id;
String name;
String subject;
public Student() {
super();
}
public Student(String name, String subject) {
super();
this.name = name;
this.subject = subject;
}
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 getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
}

web.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.Student" table="stud">
<id name="id" type="int">
<generator class="increment"/>
</id>
<property name="name"/>
<property name="subject"/>
</class>
</hibernate-mapping>

InsertDemo.java

package mypack;

import java.util.Iterator;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class InsertDemo {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
Configuration cfg=new Configuration().configure();
SessionFactory f=cfg.buildSessionFactory();
Session session=f.openSession();
Query q=session.createQuery("from Student");
List<Student> student=q.list();
Iterator<Student> itr=student.iterator();
while(itr.hasNext())
{
Student s=itr.next();
System.out.println(s.getName());
}
session.close();
}
}
Previous Home Next