* hibernate.properties file *
# dialect : for this DB hibernate will generate sql
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
# db's driver class
hibernate.connection.driver_class=com.mysql.jdbc.Driver
# db's url
hibernate.connection.url=jdbc:mysql://localhost:3306/june26
# db's username and password
hibernate.connection.username=root
hibernate.connection.password=root
# hey hibernate please create tables for me automatically
hibernate.hbm2ddl.auto=update
# hibernate please show the sql that you produce
hibernate.show_sql=true
# hibernate please show the sql in a good format
hibernate.format_sql=true
============================
# dialect : for this DB hibernate will generate sql
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
# db's driver class
hibernate.connection.driver_class=com.mysql.jdbc.Driver
# db's url
hibernate.connection.url=jdbc:mysql://localhost:3306/june26
# db's username and password
hibernate.connection.username=root
hibernate.connection.password=root
# hey hibernate please create tables for me automatically
hibernate.hbm2ddl.auto=update
# hibernate please show the sql that you produce
hibernate.show_sql=true
# hibernate please show the sql in a good format
hibernate.format_sql=true
============================
package beans;
public class Person
{
// private data members
private int id;
private String name, city;
private int age;
// setters and getters
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 getCity() {
return city;
}
public void setCity(String city) {
System.out.println("lage loo");
this.city = city;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Person() {
System.out.println("object of person created");
}
}
============================
* Person.hbm.xml file *
public class Person
{
// private data members
private int id;
private String name, city;
private int age;
// setters and getters
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 getCity() {
return city;
}
public void setCity(String city) {
System.out.println("lage loo");
this.city = city;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Person() {
System.out.println("object of person created");
}
}
============================
* Person.hbm.xml file *
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="beans">
<class name="Person" table="person_info">
<id name="id" column="person_id">
<generator class="assigned">
</generator>
</id>
<property name="name" column="person_name"></property>
<property name="age" column="person_age"></property>
<property name="city" column="person_city"></property>
</class>
</hibernate-mapping>
============================
package beans;
import java.util.Scanner;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
public class FetchSingleObject
{
public static void main(String[] args)
{
Session session = new Configuration().
addResource("Person.hbm.xml").buildSessionFactory()
.openSession();
Scanner sc = new Scanner(System.in);
System.out.print("Input person id: ");
int pId = sc.nextInt();
// fetch an object of person
Person p = session.get(Person.class, pId);
// check for null object
if(p != null)
{
// fetch data from p using getters
System.out.println(p.getName()+":"+p.getAge()
+":"+p.getCity());
}
else
{
System.out.println("invalid id");
}
// close session
session.close();
}
}
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="beans">
<class name="Person" table="person_info">
<id name="id" column="person_id">
<generator class="assigned">
</generator>
</id>
<property name="name" column="person_name"></property>
<property name="age" column="person_age"></property>
<property name="city" column="person_city"></property>
</class>
</hibernate-mapping>
============================
package beans;
import java.util.Scanner;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
public class FetchSingleObject
{
public static void main(String[] args)
{
Session session = new Configuration().
addResource("Person.hbm.xml").buildSessionFactory()
.openSession();
Scanner sc = new Scanner(System.in);
System.out.print("Input person id: ");
int pId = sc.nextInt();
// fetch an object of person
Person p = session.get(Person.class, pId);
// check for null object
if(p != null)
{
// fetch data from p using getters
System.out.println(p.getName()+":"+p.getAge()
+":"+p.getCity());
}
else
{
System.out.println("invalid id");
}
// close session
session.close();
}
}
============================
package beans;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;
public class FetchListOfObjects
{
public static void main(String[] args)
{
Session session = new Configuration().
addResource("Person.hbm.xml").buildSessionFactory()
.openSession();
// get object of query
Query query = session.createQuery("from Person where id >= 2");
// get a list of all the persons from the table
List<Person> listOfPersons = query.list();
// get object from list using for each loop
for(Person p : listOfPersons)
{
// fetch data from p using getters
System.out.println(p.getName()+":"+p.getAge()
+":"+p.getCity());
}
// close session
session.close();
}
}
import java.util.List;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;
public class FetchListOfObjects
{
public static void main(String[] args)
{
Session session = new Configuration().
addResource("Person.hbm.xml").buildSessionFactory()
.openSession();
// get object of query
Query query = session.createQuery("from Person where id >= 2");
// get a list of all the persons from the table
List<Person> listOfPersons = query.list();
// get object from list using for each loop
for(Person p : listOfPersons)
{
// fetch data from p using getters
System.out.println(p.getName()+":"+p.getAge()
+":"+p.getCity());
}
// close session
session.close();
}
}
============================
No comments:
Post a Comment