how to get all record from table in hibernate


How to get all record from table in hibernate

Let’s understand how to get all record from table in hibernate , Here i will use oracle 10G database and java 8 version , Please make sure before running below program you have to add all hibernate jar files into lib folder and configure build path because this example is on standalone application.

How to get all record from table in hibernate

  1. hibernate.cfg.xml
  2. studentinfo.hbm.xml
  3. StudentInfo.java
  4. Run.java

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
 <session-factory>
 <property name="hbm2ddl.auto">update</property>
 <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">manager</property>
 <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
 <mapping resource="com/javagf/bean/StudentInfo.hbm.xml" />
 </session-factory>
</hibernate-configuration>

studentinfo.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 16 Jun, 2017 7:57:05 PM by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
 <class name="com.javagf.bean.StudentInfo" table="STUDENTINFO">
 <id name="roll" type="int">
 <generator class="assigned" />
 </id>
 <property name="name" type="java.lang.String"></property>
 <property name="address" type="java.lang.String"></property>
 </class>
</hibernate-mapping>

StudentInfo.java

package com.javagf.bean;
public class StudentInfo {
 private int roll;
 private String name; 
 private String address;
 public int getRoll() { return roll; }
 public void setRoll(int roll) { this.roll = roll; }
 public String getName() { return name; }
 public void setName(String name) { this.name = name; }
 public String getAddress() { return address; }
 public void setAddress(String address) { this.address = address; }
}

Run.java

package com.javagf.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.javagf.bean.StudentInfo;
public class Run {
 public static void main(String[] args) {
 Configuration cfg = new Configuration(); 
 cfg.configure("com/javagf/hibernate/hibernate.cfg.xml");
 SessionFactory factory = cfg.buildSessionFactory();
 Session session = factory.openSession();

 List<StudentInfo> list = session.createCriteria(StudentInfo.class).list();
 System.out.println(list);

 session.close();

 }
}

Output

[StudentInfo [roll=101, name=Vicky, address=Hyderabad], StudentInfo [roll=102, name=Rahul, address=Pune]]

Related Posts

  1. Why hibernate is better than JDBC
  2. Hibernate Architecture
  3. Steps to create hibernate application in eclipse
  4. Hibernate Mapping and Configuration file
  5. Hibernate SQL Dialects List
  6. Hibernate Query Language
  7. Hibernate ID generator
  8. insert data into table using hibernate example
  9. update record database using hibernate
  10. Delete record database using hibernate






Full-stack web and mobile app development company Contact Us : Skype: indore.webcoder Email: info@w3sparks.com Sales: +91 8085506229