Hibernate Introduction

223
http://candidjava.com/hibernate-tutorial Hibernate Introduction Hibernate was introduced by Gavin king in 2001. Hibernate is used to simplify work in data base. Hibernate is an Library in Java, used for an Object Relational Mapping (ORM). Hibernate mainly used for mapping tables in data base and simplify relation between data bases. It’s an open source. Advantages: It supports 12 data bases. It generates queries automatically according to data base driver what we used. Eh cache: 2 nd level cache – It retrieve data’s from data base and store it in some temporary file and process queries from that file instead of data base. It saves time because of small data’s in temporary files. Hibernate can be integrated in java and j2EE (Servlet,Struts). XML: xml file is an intermediate for java and data base. Mapping java class and database is process through xml. Xml file map the java class variables (java data types) to data base table attributes (sql data types). Persistence (POJO): Persistence – storage. Hibernate provides temporary storage for pojo class. Plain Old Java Object – This is the persistence class in hibernate. This class contains getter, setter method for attributes used in data base tables. It should contain a no-argument constructor. The variables used in this class are map with the attributes used in table through xml.

description

hb

Transcript of Hibernate Introduction

Page 1: Hibernate Introduction

http://candidjava.com/hibernate-tutorial

Hibernate IntroductionHibernate was introduced by Gavin king in 2001.

Hibernate is used to simplify work in data base.

Hibernate is an Library in Java, used for an Object Relational Mapping (ORM).

Hibernate mainly used for mapping tables in data base and simplify relation between data bases.

It’s an open source.

Advantages:

It supports 12 data bases.

It generates queries automatically according to data base driver what we used.

Eh cache:

2nd level cache – It retrieve data’s from data base and store it in some temporary file and process queries from that

file instead of data base.

It saves time because of small data’s in temporary files.

Hibernate can be integrated in java and j2EE (Servlet,Struts).

XML:

xml file is an intermediate for java and data base.

Mapping java class and database is process through xml.

Xml file map the java class variables (java data types) to data base table attributes (sql data types).

Persistence (POJO):

Persistence – storage.

Hibernate provides temporary storage for pojo class.

Plain Old Java Object – This is the persistence class in hibernate.

This class contains getter, setter method for attributes used in data base tables.

It should contain a no-argument constructor.

The variables used in this class are map with the attributes used in table through xml.

HQL:

HQL is an object oriented language and also platform independent.

Instead of sql queries we use HQL in hibernate, it is easy to understand than sql queries.

Page 2: Hibernate Introduction

Hibernate Query Language.

Types:

Create Query

Create Criteria

Sql Query

Hibernate configuration(cfg, hbm)

Hibernate.cfg.xml:

<session-factory name=”studentFactory”>

Provides a unique name for property used with this database.

<property name=”connection.driver_class”> oracle.jdbc.OracleDriver </property>

The property connection.driver_class is used for loading database driver just like loading driver in

JDBC(Class.forName();)

<property name=”connection.url”>jdbc:oracle:thin:@localhost:1521:XE </property>

Connection.url property is used to load connection for the paticular database

<property name=”connection.username”>system </property>

This property implies user name of the database.

“system”—This is default user name for oracle.

<property name=”connection.password”>system</property>

This property implies the database password.

system –Replace this with your oracle password.

<property name=”connection.pool_size”>5</property>

Connection.pool_size property is used to maintain connection pooling by application server not by the database

server

<property name=”dialect”>org.hibernate.dialect.OracleDialect </property>

This is an optional property. which generates sql query for database based on the dialect we used. Different database

have different query. Query may be change as the database changes. to over come this problem dialect come in

exist.

<property name=”show_sql”>true</property>

This property displays sql query in console generated by the previous property dialect.

Page 3: Hibernate Introduction

If it is false it doesn’t display the query.

<property name=”hbm2ddl.auto”>update</property>

Update–This Property Used to update the table Rather than create the new table.

If table already exists it update values into table otherwise it creates new table automatically.

Create–This property creates new table.

If the table already exists, it deletes all records in the table and create new table with the given values.

It’s better use update property rather than create to maintain old records.

<mapping resource=”com\\xml\\Student.hbm.xml”/>

Mapping Resource property is used for mapping pojo with table.

Be sure while com\\xml matches your package name else give your package name instead.

Hbm file:

hbm file is created for each pojo class.

It is used as mapping in hibernate.

It’s like an intermediate between hibernate java class and data base table.

All contents in this file are within hibernate mapping tag.

1 <hibernate-mapping>

2  

3 -------------------------

4  

5 </hibernate-mapping>

<class> tag is used for mapping a java class and table.

1 <class name=”POJOClassName” table=”tableName”>

2  <id name="identifierName" type="DataType" column="ColumnName">

3  <generator class="increment" />

4  </id>

IdentifierName — which should matches the member variable in java (pojo) class.

DataType – Should also matches the data type for the given variable in java class.

Page 4: Hibernate Introduction

ColumnName – It may be any column name for table. It must be a primary key for the given table.

Increment – It’s optional. It automatically increases primary key value for the given table.

<property name=” identifierName ” type=” DataType ” column=” ColumnName ” />

Property tag is created for each identifier in the java class.

POJO Classes in HibernatePlain Old Java Object:

It’s a java class, which have data members and methods.

Data members are map with the table attributes using xml file.

This class contains getter (getXXX) and setter (setXXX) method for attribute to get and set values into tables.

It should contain a no-argument constructor.

Hibernate Simple Program Using Command Prompthttp://candidjava.com/hibernate-simple-program-using-command-prompt

This program explains how to run hibernate in command prompt.

Step 1:

Create a Java File “AddStudent” and paste the below code into that.

AddStudent.java:

• Here we will use sessionFactory for create connetion Oracle database.

sessionFactory = new Configuration().configure(“com\\xml\\hibernate.cfg.xml”).buildSessionFactory();

• Then we will create objects for pojo classes and then save the database using session.

Student stu=new Student();

s.save(stu);

• Then we will use Transaction statement for begin and commit the database.

Transaction tx= s.beginTransaction();

tx.commit();

01 //package code;

02 import java.sql.*;

Page 5: Hibernate Introduction

03 import java.io.*;

04 import org.hibernate.*;

05 import org.hibernate.cfg.*;

06  

07 public class AddStudent {

08     private static SessionFactory sessionFactory;

09  

10     public static void main(String args[]) throws Exception {

11  

12         DataInputStream d = new DataInputStream(System.in);

13         System.out.println("ENTER YOUR NAME");

14         String name = d.readLine();

15         System.out.println("ENTER YOUR DEGREE");

16         String degree = d.readLine();

17         System.out.println("ENTER YOUR PHONE");

18         String phone = d.readLine();

19         System.out.println("Name: " + name);

20         System.out.println("Degree: " + degree);

21         System.out.println("Phone: " + phone);

22         if ((name.equals("") || degree.equals("") || phone.equals(""))) {

23             System.out.println("All informations are Required");

24         } else {

25             try {

26                 // begin try

Page 6: Hibernate Introduction

27                 sessionFactory = new Configuration().configure()

28                         .buildSessionFactory();

29             } catch (Exception e) {

30                 System.out.println(e.getMessage());

31                System.err.println("Initial SessionFactory creation failed."

32                         + e);

33             }

34             Session s = sessionFactory.openSession();

35             Transaction tx = s.beginTransaction();

36             Student stu = new Student();

37             stu.setName(name);

38             stu.setDegree(degree);

39             stu.setPhone(phone);

40             s.save(stu);

41             tx.commit();

42             System.out.println("Added to Database");

43             if (s != null)

44                 s.close();

45         }

46     }

47 }

Step 2:

Create a Java File “Student” paste the below code into that.

Student.java:

• It is a Pojo class. This class Must be a Serializable. Then here we will use get and set method.

Page 7: Hibernate Introduction

1 private long id;

2 public long getId() {return id;}

3 public void setId(long String) {id = String;}

01 //package code;

02 import java.io.*;

03  

04 public class Student implements Serializable {

05     private long id;

06     private String name;

07     private String degree;

08     private String phone;

09  

10     public long getId() {

11         return id;

12     }

13  

14     public String getName() {

15         return name;

16     }

17  

18     public String getDegree() {

19         return degree;

20     }

21  

Page 8: Hibernate Introduction

22     public String getPhone() {

23         return phone;

24     }

25  

26     public void setId(long string) {

27         id = string;

28     }

29  

30     public void setName(String string) {

31         name = string;

32     }

33  

34     public void setDegree(String string) {

35         degree = string;

36     }

37  

38     public void setPhone(String string) {

39         phone = string;

40     }

41  

42     public String toString() {

43         return name;

44     }

45 }

Page 9: Hibernate Introduction

Step 3:

Create an xml file “hibernate.cfg.xml” place the below code into that.

hibernate.cfg.xml:

<property name=”connection.driver_class”> oracle.jdbc.OracleDriver</property>

Here we will use this property for load the driver in oracle database

<property name=”connection.url”> jdbc:oracle:thin:@localhost:1521:XE</property>

Then this property will use for open connetion inoracle database.

<property name=”connection.username”> system</property>

This property will use for username in oracle database.

<property name=”connection.password”> system</property>

This property will use for password in oracle database

<property name=”dialect”> org.hibernate.dialect.OracleDialect </property>

Here we will use oracle Dialect for generating query.

01 <!DOCTYPE hibernate-configuration PUBLIC

02     "-//Hibernate/Hibernate Configuration DTD//EN"

03     "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

04 <hibernate-configuration>

05     <session-factory name="studentFactory">

06         <property name="connection.driver_class">

07             oracle.jdbc.OracleDriver

08             </property>

09         <property name="connection.url">

10             jdbc:oracle:thin:@localhost:1521:XE

11         </property>

12         <property name="connection.username">

13             system

14         </property>

Page 10: Hibernate Introduction

15         <property name="connection.password">

16             system

17         </property>

18         <property name="connection.pool_size">5</property>

19         <!-- SQL dialect -->

20         <property name="dialect">

21             org.hibernate.dialect.OracleDialect

22             </property>

23         <!-- Echo all executed SQL to stdout -->

24         <property name="show_sql">false</property>

25         <mapping resource="Student.hbm.xml" />

26     </session-factory>

27 </hibernate-configuration>

Step 4:

Create an xml File “student.hbm.xml” and place below code into that.

Student.hbm.xml:

<class name=”com.candidjava.Student” table=”student” >

This tag will use for mapping Pojo class name and table name.

<id name=”id” type=”long” column =”ID”><generator class=”increment”/></id>

This tag will use for generating primary key id and also increment the id value.

<property name=”name” column=”name” not-null=”true”/>

This property will use for mapping the pojo class veriable name(name=”name” ) and table attribute

name(column=”name”).

</class>”/>

End of the class.

Page 11: Hibernate Introduction

01 <?xml version="1.0"?>

02 <!DOCTYPE hibernate-mapping PUBLIC

03     "-//Hibernate/Hibernate Mapping DTD//EN"

04     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

05 <hibernate-mapping >

06     <class name="Student" table="Stutbl" >

07          <id name="id" type="long" column ="ID">

08         <generator class="increment"/>

09          </id>

10          <property name="name" column="name" not-null="true"/>

11          <property name="degree" column="degree" />

12          <property name="phone" column="phone" />

13     </class>

14 </hibernate-mapping>

Step 5:

Create a folder “lib” and add library files into that.

Add below Library files into that.

ant-antlr-1.6.3.jar

antlr-2.7.5H3.jar

asm-attrs.jar

asm.jar

cglib-2.1.jar

commons-collections-2.1.1.jar

commons-logging-1.0.4.jar

dom4j-1.6.jar

ehcache-1.1.jar

hibernate.properties

hibernate3.jar

hsqldb.jar

jta.jar

log4j-1.2.9.jar

ojdbc14.jar

Page 12: Hibernate Introduction

wloracle.jar

wlsqlserver.jar

Step 6:

Create a batch file “a.bat” and place the below code into that.

set classpath=lib\hibernate3.jar;lib\ant-antlr-1.6.3.jar;lib\antlr-2.7.5H3.jar;lib\asm-attrs.jar;lib\hsqldb.jar;lib\jta.jar;lib\

commons-logging-1.0.4.jar;lib\dom4j-1.6.jar;lib\log4j-1.2.9.jar;lib\wlsqlserver.jar;lib\commons-collections-2.1.1.jar;lib\

ehcache-1.1.jar;lib\cglib-2.1.jar;lib\asm.jar;lib\ojdbc14.jar;%classpath%;.;

OUTPUT:

 Step 7:

Run the batch file.

Page 13: Hibernate Introduction

Step 8:

Page 15: Hibernate Introduction

 

Hibernate Simple Program Using WebApplication with EclipseStep by Step tutorial on running your SimpleProgram Hibernate application with Eclipse IDE

Step – 1 Creating Dynamic web ProjectStep – 2 SimpleProgram Hibernate Project structureStep – 3 Adding Jar FilesStep – 4 Creating your SimpleProgram Hibernate program(Controller & Pojo)Step – 5 Creating hibernate.cfg.xmlStep – 6 Creating Student.hbm.xmlStep – 7 OutPut for Running Console hiberanate applicationStep – 8 OutPut for Running Oracle hibernate application

• This application we will used to insert the data’s in database like sql,oracle,DB2 and etc.

• Here We will see how to save the data’s in oracle database .

Step – 1  Creating Dynamic web Project:

Start Eclipse and goto File -> New -> Project -> Dynamic Web Project

Page 17: Hibernate Introduction

Step – 2  SimpleProgram  Hibernate Project structure:

Project Structure:

Page 18: Hibernate Introduction

 

 

 

 

 

 

 

 

 

 

Step – 3  Adding Jar Files: 

JAR Files:

Page 19: Hibernate Introduction

 

 

 

 

 

 

 

 

 

 

Step – 4  Creating your  SimpleProgram Hibernate program(Controller & Pojo): 

AddStudent.java:

01 package com.candidjava;

02  

03 public class AddStudent {

04     public static void main(String[] args) {

05         try {

06             AddStudentControl ctrl = new AddStudentControl();

07             ctrl.addNewStudent(args);

08         } catch (Exception e) {

09             System.out.println(e.getMessage());

10         }

11     }

12 }

AddStudentControl.java:

Here we will use sessionFactory for create connetion Oracle database.

Page 20: Hibernate Introduction

sessionFactory = new Configuration().configure(“com\\xml\\hibernate.cfg.xml”).buildSessionFactory();

• Then we will create objects for pojo classes and then save the database using session.

Student stu=new Student();

s.save(stu);

• Then we will use Transaction statement for begin and commit the database.

Transaction tx= s.beginTransaction();

tx.commit();

01 package com.candidjava;

02  

03 import java.sql.*;

04 import java.io.*;

05 import org.hibernate.*;

06 import org.hibernate.cfg.*;

07  

08 public class AddStudentControl {

09  

10     private static SessionFactory sessionFactory;

11  

12     private Session getSession() {

13         Session s = null;

14         try {

15             sessionFactory = new Configuration().configure(

16                    "com\\xmlFiles\\hibernate.cfg.xml").buildSessionFactory();

17             s = sessionFactory.openSession();

18         } catch (HibernateException e) {

19             System.out.println(e.getMessage());

Page 21: Hibernate Introduction

20         }

21         return s;

22     }

23  

24     public void addNewStudent(String[] args) throws Exception {

25         try {

26             Session s = getSession();

27             Transaction tx = s.beginTransaction();

28             Student stu = new Student();

29             DataInputStream ds = new DataInputStream(System.in);

30             System.out.println("*********************\n");

31             System.out.println("Enter the Student Id");

32             String stuid = ds.readLine();

33             System.out.println("Enter the Student RegNo");

34             String sturegno = ds.readLine();

35             System.out.println("Enter the Student name");

36             String stuname = ds.readLine();

37             System.out.println("Enter the Student mark1");

38             String stumark1 = ds.readLine();

39             System.out.println("Enter the Student mark2");

40             String stumark2 = ds.readLine();

41             System.out.println("Enter the Degree");

42             String degree = ds.readLine();

43             System.out.println("Enter the mobile No");

Page 22: Hibernate Introduction

44             String mobileno = ds.readLine();

45             System.out.println("Enter the Student mail Id");

46             String mailid = ds.readLine();

47  

48             stu.setStuid(stuid);

49  

50             stu.setSturegno(sturegno);

51             stu.setStuname(stuname);

52             stu.setStumark1(stumark1);

53             stu.setStumark2(stumark2);

54             stu.setStuid(stuid);

55             stu.setDegree(degree);

56             stu.setMobileno(mobileno);

57             stu.setMailid(mailid);

58             s.save(stu);

59             tx.commit();

60             System.out.println("\n\n Details Added \n");

61         } catch (HibernateException e) {

62             System.out.println("error1" + e.getMessage());

63             System.out.println("error1");

64         }

65  

66     }

Page 23: Hibernate Introduction

67  

68 }// end of class

Student.java:

• It is a Pojo class. This class Must be a Serializable. Then here we will use get and set method.

1 private long id;

2 public long getId() {return id;}

3 public void setId(long String) {id = String;}

 

01 package com.candidjava;

02  

03 import java.io.*;

04  

05 public class Student implements Serializable {

06     private long id;

07     private String stuid;

08     private String sturegno;

09     private String stuname;

10     private String stumark1;

11     private String stumark2;

12     private String degree;

13     private String mobileno;

14     private String mailid;

15  

16     public long getId() {

Page 24: Hibernate Introduction

17         return id;

18     }

19  

20     public String getStuid() {

21         return stuid;

22     }

23  

24     public String getSturegno() {

25         return sturegno;

26     }

27  

28     public String getStuname() {

29         return stuname;

30     }

31  

32     public String getStumark1() {

33         return stumark1;

34     }

35  

36     public String getStumark2() {

37         return stumark2;

38     }

39  

40     public String getDegree() {

Page 25: Hibernate Introduction

41         return degree;

42     }

43  

44     public String getMobileno() {

45         return mobileno;

46     }

47  

48     public String getMailid() {

49         return mailid;

50     }

51  

52     public void setId(long string) {

53         id = string;

54     }

55  

56     public void setStuid(String string) {

57         stuid = string;

58     }

59  

60     public void setSturegno(String string) {

61         sturegno = string;

62     }

63  

64     public void setStuname(String string) {

Page 26: Hibernate Introduction

65         stuname = string;

66     }

67  

68     public void setStumark1(String string) {

69         stumark1 = string;

70     }

71  

72     public void setStumark2(String string) {

73         stumark2 = string;

74     }

75  

76     public void setDegree(String string) {

77         degree = string;

78     }

79  

80     public void setMobileno(String string) {

81         mobileno = string;

82     }

83  

84     public void setMailid(String string) {

85         mailid = string;

86     }

87  

88 }

Page 27: Hibernate Introduction

Step – 5  Creating hibernate.cfg.xml:

hibernate.cfg.xml:

<property name=”connection.driver_class”> oracle.jdbc.OracleDriver</property>

Here we will use this property for load the driver in oracle database

<property name=”connection.url”> jdbc:oracle:thin:@localhost:1521:XE</property>

Then this property will use for open connetion inoracle database.

<property name=”connection.username”> system</property>

This property will use for username in oracle database.

<property name=”connection.password”> system</property>

This property will use for password in oracle database

<property name=”dialect”> org.hibernate.dialect.OracleDialect </property>

Here we will use oracle Dialect for generating query.

01 <!DOCTYPE hibernate-configuration PUBLIC

02     "-//Hibernate/Hibernate Configuration DTD//EN"

03     "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

04 <hibernate-configuration>

05     <session-factory name="studentFactory">

06         <property name="connection.driver_class">

07             oracle.jdbc.OracleDriver

08             </property>

09         <property name="connection.url">

10             jdbc:oracle:thin:@localhost:1521:XE

11         </property>

12         <property name="connection.username">

13             system

14         </property>

15         <property name="connection.password">

Page 28: Hibernate Introduction

16             system

17         </property>

18         <property name="connection.pool_size">5</property>

19         <!-- SQL dialect -->

20         <property name="dialect">

21             org.hibernate.dialect.OracleDialect

22             </property>

23         <!-- Echo all executed SQL to stdout -->

24         <property name="show_sql">false</property>

25         <property name="hbm2ddl.auto">update</property>

26         <mapping resource="\com\\xmlFiles\\Student.hbm.xml" />

27     </session-factory>

28 </hibernate-configuration>

Step – 6  Creating Student.hbm.xml:

Student.hbm.xml:

<class name=”com.candidjava.Student” table=”student” >

This tag will use for mapping Pojo class name and table name.

<id name=”id” type=”long” column =”ID”><generator class=”increment”/></id>

This tag will use for generating primary key id and also increment the id value.

<property name=”name” column=”name” not-null=”true”/>

This property will use for mapping the pojo class veriable name(name=”name” ) and table attribute

name(column=”name”).

</class>”/>

End of the class.

01 <?xml version="1.0"?>

Page 29: Hibernate Introduction

02 <!DOCTYPE hibernate-mapping PUBLIC

03     "-//Hibernate/Hibernate Mapping DTD//EN"

04     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

05 <hibernate-mapping>

06     <class name="com.candidjava.Student" table="Student2">

07         <id name="id" type="long" column="Id">

08             <generator class="increment" />

09         </id>

10         <property name="stuid" column="StuId" not-null="true" />

11         <property name="sturegno" column="StuRegNO" />

12         <property name="stuname" column="StuName" />

13         <property name="stumark1" column="StuMark1" />

14         <property name="stumark2" column="StuMark2" />

15         <property name="degree" column="Degree" />

16  

17         <property name="mobileno" column="MobileNo" />

18         <property name="mailid" column="MailId" />

19     </class>

20 </hibernate-mapping>

web.xml:

01 <?xml version="1.0" encoding="UTF-8"?>

02 <web-app id="WebApp_ID" version="2.4"

03    xmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

04    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/

Page 30: Hibernate Introduction

xml/ns/j2ee/web-app_2_4.xsd">

05     <display-name>HibernateSaveQuery</display-name>

06     <welcome-file-list>

07         <welcome-file>index.html</welcome-file>

08         <welcome-file>index.htm</welcome-file>

09         <welcome-file>index.jsp</welcome-file>

10         <welcome-file>default.html</welcome-file>

11         <welcome-file>default.htm</welcome-file>

12         <welcome-file>default.jsp</welcome-file>

13     </welcome-file-list>

14 </web-app>

Page 31: Hibernate Introduction

OUTPUT:

 Step – 7   OutPut  for  Running  Console hiberanate application:

Step – 8  OutPut  for  Running Oracle hibernate application:

Page 34: Hibernate Introduction

Hibernate Simple Program Using Java Application with EclipseStep by Step tutorial to run your Simple Hibernate Program in Eclipse IDE

Step – 1 Creating Java ProjectStep – 2 SimpleProgram Hibernate Project structureStep – 3 Adding Jar FilesStep – 4 Creating your SimpleProgram Hibernate program(Controller & Pojo)Step – 5 Creating hibernate.cfg.xmlStep – 6 Creating Student.hbm.xmlStep – 7 OutPut for Running Console hiberanate applicationStep – 8 OutPut for Running Oracle hibernate application

•  This application we will used to insert the data’s in database like sql,oracle,DB2 and etc.

•  Here We will see how to save the data’s in oracle database .

Page 35: Hibernate Introduction

Step – 1  Creating Java Project:

Start Eclipse and goto File -> New -> Project -> Java Project

Step – 2  SimpleProgram  Hibernate Project structure:

Project Structure:

Page 36: Hibernate Introduction

 

 

 

 

 

 

 

Step – 3  Adding Jar Files: 

JAR Files:

Page 37: Hibernate Introduction

 

 

 

 

 

 

 

 

  

 

How To Add The JAR Files:

Right click on project and choose properties

Page 38: Hibernate Introduction

Step – 4  Creating your  SimpleProgram Hibernate program(Controller & Pojo): 

AddStudent.java:

• Here we will use sessionFactory for create connetion Oracle database.

sessionFactory = new Configuration().configure(“com\\xml\\hibernate.cfg.xml”).buildSessionFactory();

• Then we will create objects for pojo classes and then save the database using session.

Student stu=new Student(); s.save(stu);

• Then we will use Transaction statement for begin and commit the database.

Transaction tx= s.beginTransaction(); tx.commit();

01 package com.candidjava;

02  

03 //package code;

Page 39: Hibernate Introduction

04 import java.sql.*;

05 import java.io.*;

06 import org.hibernate.*;

07 import org.hibernate.cfg.*;

08  

09 public class AddStudent {

10     private static SessionFactory sessionFactory;

11  

12     public static void main(String args[]) throws Exception {

13  

14         DataInputStream d = new DataInputStream(System.in);

15         System.out.println("ENTER YOUR NAME");

16         String name = d.readLine();

17         System.out.println("ENTER YOUR DEGREE");

18         String degree = d.readLine();

19         System.out.println("ENTER YOUR PHONE");

20         String phone = d.readLine();

21         System.out.println("Name: " + name);

22         System.out.println("Degree: " + degree);

23         System.out.println("Phone: " + phone);

24         if ((name.equals("") || degree.equals("") || phone.equals(""))) {

25             System.out.println("All informations are Required");

26         } else {

27             try {

Page 40: Hibernate Introduction

28                 // begin try

29                 sessionFactory = new Configuration().configure(

30                        "com\\xml\\hibernate.cfg.xml").buildSessionFactory();

31             } catch (Exception e) {

32                 System.out.println(e.getMessage());

33                System.err.println("Initial SessionFactory creation failed."

34                         + e);

35             }

36             Session s = sessionFactory.openSession();

37             Transaction tx = s.beginTransaction();

38             Student stu = new Student();

39             stu.setName(name);

40             stu.setDegree(degree);

41             stu.setPhone(phone);

42             s.save(stu);

43             tx.commit();

44             System.out.println("Added to Database");

45             if (s != null)

46                 s.close();

47         }

48     }

49 }

Page 41: Hibernate Introduction

Student.java:

•  It is a Pojo class. This class Must be a Serializable. Then here we will use get and set method.

o  private long id;

o  public long getId() {return id;}

o  public void setId(long String) {id = String;}

01 package com.candidjava;

02  

03 //package code;

04 import java.io.*;

05  

06 public class Student implements Serializable {

07     private long id;

08     private String name;

09     private String degree;

10     private String phone;

11  

12     public long getId() {

13         return id;

14     }

15  

16     public String getName() {

17         return name;

18     }

19  

20     public String getDegree() {

21         return degree;

Page 42: Hibernate Introduction

22     }

23  

24     public String getPhone() {

25         return phone;

26     }

27  

28     public void setId(long string) {

29         id = string;

30     }

31  

32     public void setName(String string) {

33         name = string;

34     }

35  

36     public void setDegree(String string) {

37         degree = string;

38     }

39  

40     public void setPhone(String string) {

41         phone = string;

42     }

43  

44     public String toString() {

Page 43: Hibernate Introduction

45         return name;

46     }

47 }

Step – 5  Creating hibernate.cfg.xml:

hibernate.cfg.xml:

<property name=”connection.driver_class”> oracle.jdbc.OracleDriver</property>

Here we will use this property for load the driver in oracle database

<property name=”connection.url”> jdbc:oracle:thin:@localhost:1521:XE</property>

Then this property will use for open connetion inoracle database.

<property name=”connection.username”> system</property>

This property will use for username in oracle database.

<property name=”connection.password”> system</property>

This property will use for password in oracle database

<property name=”dialect”> org.hibernate.dialect.OracleDialect </property>

Here we will use oracle Dialect for generating query.

01 <!DOCTYPE hibernate-configuration PUBLIC

02     "-//Hibernate/Hibernate Configuration DTD//EN"

03     "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

04 <hibernate-configuration>

05     <session-factory name="studentFactory">

06         <property name="connection.driver_class">

07             oracle.jdbc.OracleDriver

08             </property>

09         <property name="connection.url">

10             jdbc:oracle:thin:@localhost:1521:XE

11         </property>

Page 44: Hibernate Introduction

12         <property name="connection.username">

13             system

14         </property>

15         <property name="connection.password">

16             system

17         </property>

18         <property name="connection.pool_size">5</property>

19         <!-- SQL dialect -->

20         <property name="dialect">

21             org.hibernate.dialect.OracleDialect

22             </property>

23         <!-- Echo all executed SQL to stdout -->

24         <property name="show_sql">true</property>

25         <property name="hbm2ddl.auto">create</property>

26         <mapping resource="\com\\xml\\Student.hbm.xml" />

27     </session-factory>

28 </hibernate-configuration>

Step – 6  Creating Student.hbm.xml:

Student.hbm.xml:

<class name=”com.candidjava.Student” table=”student” >

This tag will use for mapping Pojo class name and table name.

 <id name=”id” type=”long” column =”ID”><generator class=”increment”/></id>

This tag will use for generating primary key id and also increment the id value.

<property name=”name” column=”name” not-null=”true”/>

This property will use for mapping the pojo class veriable name(name=”name” ) and table attribute

name(column=”name”).

Page 45: Hibernate Introduction

 </class>”/>

End of the class.

01 <?xml version="1.0"?>

02 <!DOCTYPE hibernate-mapping PUBLIC

03     "-//Hibernate/Hibernate Mapping DTD//EN"

04     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

05 <hibernate-mapping>

06     <class name="com.candidjava.Student" table="Studentdemo">

07         <id name="id" type="long" column="ID">

08             <generator class="increment" />

09         </id>

10         <property name="name" column="name" not-null="true" />

11         <property name="degree" column="degree" />

12         <property name="phone" column="phone" />

13     </class>

14 </hibernate-mapping>

Page 46: Hibernate Introduction

OUTPUT:

 Step – 7   OutPut  for  Running  Console hiberanate application:

Step – 8  OutPut  for  Running Oracle hibernate application:

Page 47: Hibernate Introduction

Hibernate Hbm2.ddl.auto create Example Program In Eclipse

Page 49: Hibernate Introduction

 

 

 

 

 

 

 

 

JAR Files:

Page 50: Hibernate Introduction

 

 

 

 

 

 

 

 

 

 

SaveStudent.java:

01 package com.candidjava;

02  

03 public class SaveStudent {

04     public static void main(String[] args) {

05         try {

06             SaveStudentControl ctrl = new SaveStudentControl();

07             ctrl.addNewStudent(args);

08         } catch (Exception e) {

09             System.out.println(e.getMessage());

10         }

11     }

12 }

SaveStudentControl.java:

01 package com.candidjava;

Page 51: Hibernate Introduction

02  

03 import java.sql.*;

04 import java.util.List;

05 import java.io.*;

06 import org.hibernate.*;

07 import org.hibernate.cfg.*;

08  

09 public class SaveStudentControl {

10  

11     private static SessionFactory sessionFactory;

12  

13     private Session getSession() {

14         Session s = null;

15         try {

16             sessionFactory = new Configuration().configure(

17                     "com\\xml\\hibernate.cfg.xml").buildSessionFactory();

18             s = sessionFactory.openSession();

19         } catch (HibernateException e) {

20             System.out.println(e.getMessage());

21         }

22         return s;

23     }

24  

25     public void addNewStudent(String[] args) throws Exception {

Page 52: Hibernate Introduction

26         try {

27             Session s = getSession();

28             Transaction tx = s.beginTransaction();

29             Student stu = new Student();

30             DataInputStream ds = new DataInputStream(System.in);

31             System.out.println("*********************\n");

32             System.out.println("Enter the Student Id");

33             String stuid = ds.readLine();

34             System.out.println("Enter the Student RegNo");

35             String sturegno = ds.readLine();

36             System.out.println("Enter the Student name");

37             String stuname = ds.readLine();

38             System.out.println("Enter the Student mark1");

39             String stumark1 = ds.readLine();

40             System.out.println("Enter the Student mark2");

41             String stumark2 = ds.readLine();

42             System.out.println("Enter the Degree");

43             String degree = ds.readLine();

44             System.out.println("Enter the mobile No");

45             String mobileno = ds.readLine();

46             System.out.println("Enter the Student mail Id");

47             String mailid = ds.readLine();

48             stu.setId(3);

49             stu.setStuid(stuid);

50             stu.setSturegno(sturegno);

Page 53: Hibernate Introduction

51             stu.setStuname(stuname);

52             stu.setStumark1(stumark1);

53             stu.setStumark2(stumark2);

54             stu.setStuid(stuid);

55             stu.setDegree(degree);

56             stu.setMobileno(mobileno);

57             stu.setMailid(mailid);

58             s.save(stu);

59             // Query q =

60            // s.createQuery("select stu.stuid, avg(stu.stuid) from Student stu group by stu.stuid having avg(stu.stuid) > 10 ");

61             // List li=q.list();

62             // System.out.println(li);

63             // Student ss=new Student();

64             tx.commit();

65             System.out.println("\n\n Details Added \n");

66         } catch (HibernateException e) {

67             System.out.println(e.getMessage());

68             System.out.println("error");

69         }

70  

71     }

72  

73 }// end of class

Page 54: Hibernate Introduction

Student.java:

01 package com.candidjava;

02  

03 import java.io.*;

04  

05 public class Student implements Serializable {

06     private long id;

07     private String stuid;

08     private String sturegno;

09     private String stuname;

10     private String stumark1;

11     private String stumark2;

12     private String degree;

13     private String mobileno;

14     private String mailid;

15  

16     public long getId() {

17         return id;

18     }

19  

20     public String getStuid() {

21         return stuid;

22     }

23  

Page 55: Hibernate Introduction

24     public String getSturegno() {

25         return sturegno;

26     }

27  

28     public String getStuname() {

29         return stuname;

30     }

31  

32     public String getStumark1() {

33         return stumark1;

34     }

35  

36     public String getStumark2() {

37         return stumark2;

38     }

39  

40     public String getDegree() {

41         return degree;

42     }

43  

44     public String getMobileno() {

45         return mobileno;

46     }

47  

Page 56: Hibernate Introduction

48     public String getMailid() {

49         return mailid;

50     }

51  

52     public void setId(long string) {

53         id = string;

54     }

55  

56     public void setStuid(String string) {

57         stuid = string;

58     }

59  

60     public void setSturegno(String string) {

61         sturegno = string;

62     }

63  

64     public void setStuname(String string) {

65         stuname = string;

66     }

67  

68     public void setStumark1(String string) {

69         stumark1 = string;

70     }

71  

Page 57: Hibernate Introduction

72     public void setStumark2(String string) {

73         stumark2 = string;

74     }

75  

76     public void setDegree(String string) {

77         degree = string;

78     }

79  

80     public void setMobileno(String string) {

81         mobileno = string;

82     }

83  

84     public void setMailid(String string) {

85         mailid = string;

86     }

87  

88 }

hibernate.cfg.xml:

01 <!DOCTYPE hibernate-configuration PUBLIC

02     "-//Hibernate/Hibernate Configuration DTD//EN"

03     "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

04 <hibernate-configuration>

05     <session-factory name="studentFactory">

06         <property name="connection.driver_class">

07             oracle.jdbc.OracleDriver

Page 58: Hibernate Introduction

08             </property>

09         <property name="connection.url">

10             jdbc:oracle:thin:@localhost:1521:XE

11         </property>

12         <property name="connection.username">

13             system

14         </property>

15         <property name="connection.password">

16             system

17         </property>

18         <property name="connection.pool_size">5</property>

19         <!-- SQL dialect -->

20         <property name="dialect">

21             org.hibernate.dialect.OracleDialect

22             </property>

23         <!-- Echo all executed SQL to stdout -->

24         <property name="show_sql">false</property>

25         <property name="hbm2ddl.auto">create</property>

26         <mapping resource="com\\xml\\Student.hbm.xml" />

27     </session-factory>

28 </hibernate-configuration>

Student.hbm.xml:

01 <?xml version="1.0"?>

Page 59: Hibernate Introduction

02 <!DOCTYPE hibernate-mapping PUBLIC

03     "-//Hibernate/Hibernate Mapping DTD//EN"

04     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

05 <hibernate-mapping>

06     <class name="com.candidjava.Student" table="Student">

07         <id name="id" type="long" column="Id">

08             <generator class="increment" />

09         </id>

10         <property name="stuid" column="StuId" not-null="true" />

11         <property name="sturegno" column="StuRegNO" />

12         <property name="stuname" column="StuName" />

13         <property name="stumark1" column="StuMark1" />

14         <property name="stumark2" column="StuMark2" />

15         <property name="degree" column="Degree" />

16  

17         <property name="mobileno" column="MobileNo" />

18         <property name="mailid" column="MailId" />

19     </class>

20 </hibernate-mapping>

web.xml:

01<?xml version="1.0" encoding="UTF-8"?>

02

<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http:// java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

03     <display-name>Create</display-name>

Page 60: Hibernate Introduction

04     <welcome-file-list>

05         <welcome-file>index.html</welcome-file>

06         <welcome-file>index.htm</welcome-file>

07         <welcome-file>index.jsp</welcome-file>

08         <welcome-file>default.html</welcome-file>

09         <welcome-file>default.htm</welcome-file>

10         <welcome-file>default.jsp</welcome-file>

11     </welcome-file-list>

12 </web-app>

OUTPUT:

Page 62: Hibernate Introduction

Hbm2ddl.auto Hbm2ddl.auto update Example Program In Eclipse

ProjectStructure: 

 

 

 

 

 

 

 

 

 

 

 

 

 

JarFiles:

Page 63: Hibernate Introduction

 

 

 

 

 

 

 

 

  

 

   

 

 

SaveStudent.java:

01 package com.candidjava;

02  

03 public class SaveStudent {

04     public static void main(String[] args) {

05         try {

Page 64: Hibernate Introduction

06             SaveStudentControl ctrl = new SaveStudentControl();07             ctrl.addNewStudent();

08         } catch (Exception e) {

09             System.out.println(e.getMessage());

10         }

11     }

12 }

SaveStudentControl.java:

 

01 package com.candidjava;

02  

03 import java.sql.*;

04 import java.util.List;

05 import java.io.*;

06 import org.hibernate.*;07 import org.hibernate.cfg.*;

08  

09 public class SaveStudentControl {

10  

11     private static SessionFactory sessionFactory;

12  

13     private Session getSession() {

14         Session s = null;

15         try {

16             sessionFactory = new Configuration().configure(17                     "com\\xml\\hibernate.cfg.xml").buildSessionFactory();

18             s = sessionFactory.openSession();

19         } catch (HibernateException e) {

20             System.out.println(e.getMessage());

21         }

22         return s;

Page 65: Hibernate Introduction

23     }

24  

25     public void addNewStudent() throws Exception {

26         try {

27             Session s = getSession();

28             Transaction tx = s.beginTransaction();

29             Student stu = new Student();

30             DataInputStream ds = new DataInputStream(System.in);31             System.out.println("*********************\n");

32             System.out.println("Enter the Student Id");

33             String stuid = ds.readLine();

34             System.out.println("Enter the Student RegNo");

35             String sturegno = ds.readLine();

36             System.out.println("Enter the Student name");

37             String stuname = ds.readLine();

38             System.out.println("Enter the Student mark1");

39             String stumark1 = ds.readLine();

40             System.out.println("Enter the Student mark2");

41             String stumark2 = ds.readLine();

42             System.out.println("Enter the Degree");

43             String degree = ds.readLine();

44             System.out.println("Enter the mobile No");

45             String mobileno = ds.readLine();

46             System.out.println("Enter the Student mail Id");47             String mailid = ds.readLine();

48             stu.setId(3);

49             stu.setStuid(stuid);

50             stu.setSturegno(sturegno);

51             stu.setStuname(stuname);

52             stu.setStumark1(stumark1);53             stu.setStumark2(stumark2);

54             stu.setStuid(stuid);

Page 66: Hibernate Introduction

55             stu.setDegree(degree);

56             stu.setMobileno(mobileno);57             stu.setMailid(mailid);

58             s.save(stu);

59             // Query q =

60             // s.createQuery("select stu.stuid, avg(stu.stuid) from Student stu group by stu.stuid having avg(stu.stuid) > 10 ");

61             // List li=q.list();

62             // System.out.println(li);63             // Student ss=new Student();

64             tx.commit();

65             System.out.println("\n\n Details Added \n");

66         } catch (HibernateException e) {

67             System.out.println(e.getMessage());

68             System.out.println("error");

69         }

70  

71     }

72  

73 }// end of class

Student.java:

 

01 package com.candidjava;

02  

03 import java.io.*;

04  

05 public class Student implements Serializable {

06     private long id;

07     private String stuid;

08     private String sturegno;

09     private String stuname;

10     private String stumark1;

Page 67: Hibernate Introduction

11     private String stumark2;

12     private String degree;

13     private String mobileno;

14     private String mailid;

15  

16     public long getId() {17         return id;

18     }

19  

20     public String getStuid() {21         return stuid;

22     }

23  

24     public String getSturegno() {25         return sturegno;

26     }

27  

28     public String getStuname() {29         return stuname;

30     }

31  

32     public String getStumark1() {33         return stumark1;

34     }

35  

36     public String getStumark2() {37         return stumark2;

38     }

39  

40     public String getDegree() {

Page 68: Hibernate Introduction

41         return degree;

42     }

43  

44     public String getMobileno() {45         return mobileno;

46     }

47  

48     public String getMailid() {49         return mailid;

50     }

51  

52     public void setId(long string) {53         id = string;

54     }

55  

56     public void setStuid(String string) {57         stuid = string;

58     }

59  

60     public void setSturegno(String string) {61         sturegno = string;

62     }

63  

64     public void setStuname(String string) {65         stuname = string;

66     }

67  

68     public void setStumark1(String string) {69         stumark1 = string;

70     }

Page 69: Hibernate Introduction

71  

72     public void setStumark2(String string) {73         stumark2 = string;

74     }

75  

76     public void setDegree(String string) {77         degree = string;

78     }

79  

80     public void setMobileno(String string) {81         mobileno = string;

82     }

83  

84     public void setMailid(String string) {85         mailid = string;

86     }

87  

88 }

Web.xml: 

01 <?xml version="1.0" encoding="UTF-8"?>

02 <web-app id="WebApp_ID" version="2.4"

03     xmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

04    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/ xml/ns/j2ee/web-app_2_4.xsd">

05     <display-name>Create</display-name>

06     <welcome-file-list>

07         <welcome-file>index.html</welcome-file>

08         <welcome-file>index.htm</welcome-file>

09         <welcome-file>index.jsp</welcome-file>

10         <welcome-file>default.html</welcome-file>11         <welcome-file>default.htm</welcome-file>12         <welcome-file>default.jsp</welcome-file>13     </welcome-file-list>

Page 70: Hibernate Introduction

14 </web-app>

Hibernate.cfg.xml:

01 <!DOCTYPE hibernate-configuration PUBLIC

02     "-//Hibernate/Hibernate Configuration DTD//EN"03     "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

04 <hibernate-configuration>

05     <session-factory name="studentFactory">

06         <property name="connection.driver_class">07             oracle.jdbc.OracleDriver

08             </property>

09         <property name="connection.url">

10             jdbc:oracle:thin:@localhost:1521:XE

11         </property>

12         <property name="connection.username">

13             system

14         </property>

15         <property name="connection.password">

16             system

17         </property>

18         <property name="connection.pool_size">5</property>

19         <!-- SQL dialect -->

20         <property name="dialect">21             org.hibernate.dialect.OracleDialect

22             </property>

23         <!-- Echo all executed SQL to stdout -->

24         <property name="show_sql">true</property>

25         <property name="hbm2ddl.auto">update</property>

26         <mapping resource="com\\xml\\Student.hbm.xml" />

27     </session-factory>

28 </hibernate-configuration>

Page 71: Hibernate Introduction

Student.hbm.xml: 

01 <?xml version="1.0"?>

02 <!DOCTYPE hibernate-mapping PUBLIC

03     "-//Hibernate/Hibernate Mapping DTD//EN"

04     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

05 <hibernate-mapping>

06     <class name="com.candidjava.Student" table="Student2">07         <id name="id" type="long" column="Id">

08             <generator class="increment" />

09         </id>

10         <property name="stuid" column="StuId" not-null="true" />11         <property name="sturegno" column="StuRegNO" />

12         <property name="stuname" column="StuName" />

13         <property name="stumark1" column="StuMark1" />14         <property name="stumark2" column="StuMark2" />

15         <property name="degree" column="Degree" />

16         <property name="mobileno" column="MobileNo" />17         <property name="mailid" column="MailId" />

18     </class>

19 </hibernate-mapping>

 OUTPUT:

Page 73: Hibernate Introduction

 

 

 

 

 

 

 

 

 

 

 

 

 

 

                                                                                                              

 

 

Page 74: Hibernate Introduction

                                                                                                                                     

  

Hibernate Session Object 

Hibernate Session.Save() Example Program in EclipseHibernate + session.save()+Eclipse

Save:

This Program Explain how to save field values into table (Oracle) Using Hibernate.

session.save()—–Saves the given details into database.

It always saves only new data.

If the given ID already exists, it takes new ID and save data’s for that ID as a new record.

Page 75: Hibernate Introduction

 

 Project Structure:

 

 

 

 

 

 

 

 

 

JAR Files:

Page 76: Hibernate Introduction

 

 

 

 

 

 

 

 

 

 

 

 

SaveStudent.java:

01 package com.candidjava;

02  

Page 77: Hibernate Introduction

03 public class SaveStudent {

04     public static void main(String[] args) {

05         try {

06             SaveStudentControl ctrl = new SaveStudentControl();

07             ctrl.addNewStudent(args);

08         } catch (Exception e) {

09             System.out.println(e.getMessage());

10         }

11     }

12 }

SaveStudentControl.java:

01 package com.candidjava;

02  

03 import java.sql.*;

04 import java.io.*;

05 import org.hibernate.*;

06 import org.hibernate.cfg.*;

07  

08 public class SaveStudentControl {

09  

10     private static SessionFactory sessionFactory;

11  

12     private Session getSession() {

13         Session s = null;

Page 78: Hibernate Introduction

14         try {

15             sessionFactory = new Configuration().configure(

16                     "com\\xml\\hibernate.cfg.xml").buildSessionFactory();

17             s = sessionFactory.openSession();

18         } catch (HibernateException e) {

19             System.out.println(e.getMessage());

20         }

21         return s;

22     }

23  

24     public void addNewStudent(String[] args) throws Exception {

25         try {

26             Session s = getSession();

27             Transaction tx = s.beginTransaction();

28             Student stu = new Student();

29             DataInputStream ds = new DataInputStream(System.in);

30             System.out.println("*********************\n");

31             System.out.println("Enter the Student Id");

32             String stuid = ds.readLine();

33             System.out.println("Enter the Student RegNo");

34             String sturegno = ds.readLine();

35             System.out.println("Enter the Student name");

36             String stuname = ds.readLine();

37             System.out.println("Enter the Student mark1");

Page 79: Hibernate Introduction

38             String stumark1 = ds.readLine();

39             System.out.println("Enter the Student mark2");

40             String stumark2 = ds.readLine();

41             System.out.println("Enter the Degree");

42             String degree = ds.readLine();

43             System.out.println("Enter the mobile No");

44             String mobileno = ds.readLine();

45             System.out.println("Enter the Student mail Id");

46             String mailid = ds.readLine();

47             stu.setId(3);

48             stu.setStuid(stuid);

49             stu.setSturegno(sturegno);

50             stu.setStuname(stuname);

51             stu.setStumark1(stumark1);

52             stu.setStumark2(stumark2);

53             stu.setStuid(stuid);

54             stu.setDegree(degree);

55             stu.setMobileno(mobileno);

56             stu.setMailid(mailid);

57             s.save(stu);

58             Student ss = new Student();

59             tx.commit();

60             System.out.println("\n\n Details Added \n");

61         } catch (HibernateException e) {

62             System.out.println(e.getMessage());

Page 80: Hibernate Introduction

63             System.out.println("error");

64         }

65  

66     }

67  

68 }// end of class

Student.java:

01 package com.candidjava;

02  

03 import java.io.*;

04  

05 public class Student implements Serializable {

06     private long id;

07     private String stuid;

08     private String sturegno;

09     private String stuname;

10     private String stumark1;

11     private String stumark2;

12     private String degree;

13     private String mobileno;

14     private String mailid;

15  

16     public long getId() {

17         return id;

Page 81: Hibernate Introduction

18     }

19  

20     public String getStuid() {

21         return stuid;

22     }

23  

24     public String getSturegno() {

25         return sturegno;

26     }

27  

28     public String getStuname() {

29         return stuname;

30     }

31  

32     public String getStumark1() {

33         return stumark1;

34     }

35  

36     public String getStumark2() {

37         return stumark2;

38     }

39  

40     public String getDegree() {

41         return degree;

Page 82: Hibernate Introduction

42     }

43  

44     public String getMobileno() {

45         return mobileno;

46     }

47  

48     public String getMailid() {

49         return mailid;

50     }

51  

52     public void setId(long string) {

53         id = string;

54     }

55  

56     public void setStuid(String string) {

57         stuid = string;

58     }

59  

60     public void setSturegno(String string) {

61         sturegno = string;

62     }

63  

64     public void setStuname(String string) {

65         stuname = string;

Page 83: Hibernate Introduction

66     }

67  

68     public void setStumark1(String string) {

69         stumark1 = string;

70     }

71  

72     public void setStumark2(String string) {

73         stumark2 = string;

74     }

75  

76     public void setDegree(String string) {

77         degree = string;

78     }

79  

80     public void setMobileno(String string) {

81         mobileno = string;

82     }

83  

84     public void setMailid(String string) {

85         mailid = string;

86     }

87  

88 }

Page 84: Hibernate Introduction

hibernate.cfg.xml:

01 <!DOCTYPE hibernate-configuration PUBLIC

02     "-//Hibernate/Hibernate Configuration DTD//EN"

03     "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

04 <hibernate-configuration>

05     <session-factory name="studentFactory">

06         <property name="connection.driver_class">

07             oracle.jdbc.OracleDriver

08             </property>

09         <property name="connection.url">

10             jdbc:oracle:thin:@localhost:1521:XE

11         </property>

12         <property name="connection.username">

13             system

14         </property>

15         <property name="connection.password">

16             system

17         </property>

18         <property name="connection.pool_size">5</property>

19         <!-- SQL dialect -->

20         <property name="dialect">

21             org.hibernate.dialect.OracleDialect

22             </property>

23         <!-- Echo all executed SQL to stdout -->

24         <property name="show_sql">false</property>

Page 85: Hibernate Introduction

25         <property name="hbm2ddl.auto">update</property>

26         <mapping resource="com\\xml\\Student.hbm.xml" />

27     </session-factory>

28 </hibernate-configuration>

Student.hbm.xml:

01 <?xml version="1.0"?>

02 <!DOCTYPE hibernate-mapping PUBLIC

03     "-//Hibernate/Hibernate Mapping DTD//EN"

04     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

05 <hibernate-mapping>

06     <class name="com.candidjava.Student" table="Student">

07         <id name="id" type="long" column="Id">

08             <generator class="increment" />

09         </id>

10         <property name="stuid" column="StuId" not-null="true" />

11         <property name="sturegno" column="StuRegNO" />

12         <property name="stuname" column="StuName" />

13         <property name="stumark1" column="StuMark1" />

14         <property name="stumark2" column="StuMark2" />

15         <property name="degree" column="Degree" />

16  

17         <property name="mobileno" column="MobileNo" />

18         <property name="mailid" column="MailId" />

Page 86: Hibernate Introduction

19     </class>

20 </hibernate-mapping>

web.xml:

01 <?xml version="1.0" encoding="UTF-8"?>

02 <web-app id="WebApp_ID" version="2.4"

03    xmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

04    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/ xml/ns/j2ee/web-app_2_4.xsd">

05     <display-name>SessionObjectSave</display-name>

06     <welcome-file-list>

07         <welcome-file>index.html</welcome-file>

08         <welcome-file>index.htm</welcome-file>

09         <welcome-file>index.jsp</welcome-file>

10         <welcome-file>default.html</welcome-file>

11         <welcome-file>default.htm</welcome-file>

12         <welcome-file>default.jsp</welcome-file>

13     </welcome-file-list>

14 </web-app>

Page 87: Hibernate Introduction

OUTPUT:

Page 90: Hibernate Introduction

Hibernate Session.SaveOrUpdate() Example Program in EclipseHibernate + session.saveOrUpdate() + Eclipse

SaveOrUpdate:

This Program Explains the use of  SaveOrUpdate method in session object.

session.saveOrUpdate()—-Updates the Given details for the given Id number.

If ID doesn’t exist it doesn’t change any values.

Project Structure:

Page 92: Hibernate Introduction

 

 

 

 

 

 

 

 

JAR Files:

Page 93: Hibernate Introduction

 

 

 

 

 

 

 

 

 

 

 

 

 

SaveOrUpdateStudent.java:

01 package com.candidjava;

02  

03 public class SaveOrUpdateStudent {

04     public static void main(String[] args) {

05         try {

06            SaveOrUpdateStudentControl ctrl = newSaveOrUpdateStudentControl();

07             ctrl.addNewStudent(args);

08         } catch (Exception e) {

09             System.out.println(e.getMessage());

10         }

11     }

Page 94: Hibernate Introduction

12 }

SaveOrUpdateStudentControl.java:

01 package com.candidjava;

02  

03 import java.sql.*;

04 import java.io.*;

05 import org.hibernate.*;

06 import org.hibernate.cfg.*;

07  

08 public class SaveOrUpdateStudentControl {

09  

10     private static SessionFactory sessionFactory;

11  

12     private Session getSession() {

13         Session s = null;

14         try {

15             sessionFactory = new Configuration().configure(

16                     "com\\xml\\hibernate.cfg.xml").buildSessionFactory();

17             s = sessionFactory.openSession();

18         } catch (HibernateException e) {

19             System.out.println(e.getMessage());

20         }

21         return s;

22     }

Page 95: Hibernate Introduction

23  

24     public void addNewStudent(String[] args) throws Exception {

25         try {

26             Session s = getSession();

27             Transaction tx = s.beginTransaction();

28             Student stu = new Student();

29             DataInputStream ds = new DataInputStream(System.in);

30             System.out.println("*********************\n");

31             System.out.println("Enter ID to Change");

32             Long id = Long.parseLong(ds.readLine());

33             System.out.println("Enter the Student Id");

34             String stuid = ds.readLine();

35             System.out.println("Enter the Student RegNo");

36             String sturegno = ds.readLine();

37             System.out.println("Enter the Student name");

38             String stuname = ds.readLine();

39             System.out.println("Enter the Student mark1");

40             String stumark1 = ds.readLine();

41             System.out.println("Enter the Student mark2");

42             String stumark2 = ds.readLine();

43             System.out.println("Enter the Degree");

44             String degree = ds.readLine();

45             System.out.println("Enter the mobile No");

46             String mobileno = ds.readLine();

47             System.out.println("Enter the Student mail Id");

Page 96: Hibernate Introduction

48             String mailid = ds.readLine();

49             stu.setId(id);

50             stu.setStuid(stuid);

51             stu.setSturegno(sturegno);

52             stu.setStuname(stuname);

53             stu.setStumark1(stumark1);

54             stu.setStumark2(stumark2);

55             stu.setStuid(stuid);

56             stu.setDegree(degree);

57             stu.setMobileno(mobileno);

58             stu.setMailid(mailid);

59             s.saveOrUpdate(stu);

60             tx.commit();

61             System.out.println("\n\n Details Added \n");

62         } catch (HibernateException e) {

63             System.out.println(e.getMessage());

64             System.out.println("error");

65         }

66  

67     }

68  

69 }

Student.java:

01 package com.candidjava;

Page 97: Hibernate Introduction

02  

03 import java.io.*;

04  

05 public class Student implements Serializable {

06     private long id;

07     private String stuid;

08     private String sturegno;

09     private String stuname;

10     private String stumark1;

11     private String stumark2;

12     private String degree;

13     private String mobileno;

14     private String mailid;

15  

16     public long getId() {

17         return id;

18     }

19  

20     public String getStuid() {

21         return stuid;

22     }

23  

24     public String getSturegno() {

25         return sturegno;

Page 98: Hibernate Introduction

26     }

27  

28     public String getStuname() {

29         return stuname;

30     }

31  

32     public String getStumark1() {

33         return stumark1;

34     }

35  

36     public String getStumark2() {

37         return stumark2;

38     }

39  

40     public String getDegree() {

41         return degree;

42     }

43  

44     public String getMobileno() {

45         return mobileno;

46     }

47  

48     public String getMailid() {

49         return mailid;

Page 99: Hibernate Introduction

50     }

51  

52     public void setId(long string) {

53         id = string;

54     }

55  

56     public void setStuid(String string) {

57         stuid = string;

58     }

59  

60     public void setSturegno(String string) {

61         sturegno = string;

62     }

63  

64     public void setStuname(String string) {

65         stuname = string;

66     }

67  

68     public void setStumark1(String string) {

69         stumark1 = string;

70     }

71  

72     public void setStumark2(String string) {

73         stumark2 = string;

Page 100: Hibernate Introduction

74     }

75  

76     public void setDegree(String string) {

77         degree = string;

78     }

79  

80     public void setMobileno(String string) {

81         mobileno = string;

82     }

83  

84     public void setMailid(String string) {

85         mailid = string;

86     }

87  

88 }

hibernate.cfg.xml:

01 <!DOCTYPE hibernate-configuration PUBLIC

02     "-//Hibernate/Hibernate Configuration DTD//EN"

03     "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

04 <hibernate-configuration>

05     <session-factory name="studentFactory">

06         <property name="connection.driver_class">

07             oracle.jdbc.OracleDriver

08             </property>

Page 101: Hibernate Introduction

09         <property name="connection.url">

10             jdbc:oracle:thin:@localhost:1521:XE

11         </property>

12         <property name="connection.username">

13             system

14         </property>

15         <property name="connection.password">

16             system

17         </property>

18         <property name="connection.pool_size">5</property>

19         <!-- SQL dialect -->

20         <property name="dialect">

21             org.hibernate.dialect.OracleDialect

22             </property>

23         <!-- Echo all executed SQL to stdout -->

24         <property name="show_sql">false</property>

25         <property name="hbm2ddl.auto">update</property>

26         <mapping resource="com\\xml\\Student.hbm.xml" />

27     </session-factory>

28 </hibernate-configuration>

Student.hbm.xml:

01 <?xml version="1.0"?>

02 <!DOCTYPE hibernate-mapping PUBLIC

03     "-//Hibernate/Hibernate Mapping DTD//EN"

Page 102: Hibernate Introduction

04     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

05 <hibernate-mapping>

06     <class name="com.candidjava.Student" table="Student">

07         <id name="id" type="long" column="Id">

08             <generator class="increment" />

09         </id>

10         <property name="stuid" column="StuId" not-null="true" />

11         <property name="sturegno" column="StuRegNO" />

12         <property name="stuname" column="StuName" />

13         <property name="stumark1" column="StuMark1" />

14         <property name="stumark2" column="StuMark2" />

15         <property name="degree" column="Degree" />

16  

17         <property name="mobileno" column="MobileNo" />

18         <property name="mailid" column="MailId" />

19     </class>

20 </hibernate-mapping>

web.xml:

01 <?xml version="1.0" encoding="UTF-8"?>

02 <web-app id="WebApp_ID" version="2.4"

03    xmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

04    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/ xml/ns/j2ee/web-app_2_4.xsd">

05     <display-name>SessionObjectSaveOrUpdate</display-name>

Page 103: Hibernate Introduction

06     <welcome-file-list>

07         <welcome-file>index.html</welcome-file>

08         <welcome-file>index.htm</welcome-file>

09         <welcome-file>index.jsp</welcome-file>

10         <welcome-file>default.html</welcome-file>

11         <welcome-file>default.htm</welcome-file>

12         <welcome-file>default.jsp</welcome-file>

13     </welcome-file-list>

14 </web-app>

Page 104: Hibernate Introduction

OUTPUT:

Page 109: Hibernate Introduction

 

Hibernate Session.Update() Example Program in EclipseHibernate + session.update() + Eclipse

Update:

This program explains how to update records in a table.

session.update()—-Updates the Given details to the given Id number.

If ID doesn’t exist it does nothing (doesn’t change any values).

Page 111: Hibernate Introduction

 

 

 

 

 

 

 

  

 

JAR Files:

Page 112: Hibernate Introduction

 

 

 

 

 

 

 

 

 

 

  

Student.java:

01 package com.candidjava;

02  

03 import java.io.*;

04  

05 public class Student implements Serializable {

06     private long id;

07     private String stuid;

08     private String sturegno;

09     private String stuname;

10     private String stumark1;

11     private String stumark2;

12     private String degree;

13     private String mobileno;

Page 113: Hibernate Introduction

14     private String mailid;

15  

16     public long getId() {

17         return id;

18     }

19  

20     public String getStuid() {

21         return stuid;

22     }

23  

24     public String getSturegno() {

25         return sturegno;

26     }

27  

28     public String getStuname() {

29         return stuname;

30     }

31  

32     public String getStumark1() {

33         return stumark1;

34     }

35  

36     public String getStumark2() {

37         return stumark2;

Page 114: Hibernate Introduction

38     }

39  

40     public String getDegree() {

41         return degree;

42     }

43  

44     public String getMobileno() {

45         return mobileno;

46     }

47  

48     public String getMailid() {

49         return mailid;

50     }

51  

52     public void setId(long string) {

53         id = string;

54     }

55  

56     public void setStuid(String string) {

57         stuid = string;

58     }

59  

60     public void setSturegno(String string) {

61         sturegno = string;

Page 115: Hibernate Introduction

62     }

63  

64     public void setStuname(String string) {

65         stuname = string;

66     }

67  

68     public void setStumark1(String string) {

69         stumark1 = string;

70     }

71  

72     public void setStumark2(String string) {

73         stumark2 = string;

74     }

75  

76     public void setDegree(String string) {

77         degree = string;

78     }

79  

80     public void setMobileno(String string) {

81         mobileno = string;

82     }

83  

84     public void setMailid(String string) {

85         mailid = string;

Page 116: Hibernate Introduction

86     }

87  

88 }

UpdateStudent.java:

01 package com.candidjava;

02  

03 public class UpdateStudent {

04     public static void main(String[] args) {

05         try {

06             UpdateStudentControl ctrl = new UpdateStudentControl();

07             ctrl.addNewStudent(args);

08         } catch (Exception e) {

09             System.out.println(e.getMessage());

10         }

11     }

12 }

UpdateStudentControl.java:

01 package com.candidjava;

02  

03 import java.sql.*;

04 import java.io.*;

05 import org.hibernate.*;

06 import org.hibernate.cfg.*;

07  

Page 117: Hibernate Introduction

08 public class UpdateStudentControl {

09  

10     private static SessionFactory sessionFactory;

11  

12     private Session getSession() {

13         Session s = null;

14         try {

15             sessionFactory = new Configuration().configure(

16                     "com\\xml\\hibernate.cfg.xml").buildSessionFactory();

17             s = sessionFactory.openSession();

18         } catch (HibernateException e) {

19             System.out.println(e.getMessage());

20         }

21         return s;

22     }

23  

24     public void addNewStudent(String[] args) throws Exception {

25         try {

26             Session s = getSession();

27             Transaction tx = s.beginTransaction();

28             Student stu = new Student();

29             DataInputStream ds = new DataInputStream(System.in);

30             System.out.println("*********************\n");

31             System.out.println("Enter ID to Update");

Page 118: Hibernate Introduction

32             Long id = Long.parseLong(ds.readLine());

33             System.out.println("Enter the Student Id");

34             String stuid = ds.readLine();

35             System.out.println("Enter the Student RegNo");

36             String sturegno = ds.readLine();

37             System.out.println("Enter the Student name");

38             String stuname = ds.readLine();

39             System.out.println("Enter the Student mark1");

40             String stumark1 = ds.readLine();

41             System.out.println("Enter the Student mark2");

42             String stumark2 = ds.readLine();

43             System.out.println("Enter the Degree");

44             String degree = ds.readLine();

45             System.out.println("Enter the mobile No");

46             String mobileno = ds.readLine();

47             System.out.println("Enter the Student mail Id");

48             String mailid = ds.readLine();

49             stu.setId(id);

50             stu.setStuid(stuid);

51             stu.setSturegno(sturegno);

52             stu.setStuname(stuname);

53             stu.setStumark1(stumark1);

54             stu.setStumark2(stumark2);

55             stu.setStuid(stuid);

56             stu.setDegree(degree);

Page 119: Hibernate Introduction

57             stu.setMobileno(mobileno);

58             stu.setMailid(mailid);

59             s.update(stu);

60             tx.commit();

61             System.out.println("\n\n Details Added \n");

62         } catch (HibernateException e) {

63             System.out.println(e.getMessage());

64             System.out.println("error");

65         }

66  

67     }

68  

69 }// end of class

hibernate.cfg.xml:

01 <!DOCTYPE hibernate-configuration PUBLIC

02     "-//Hibernate/Hibernate Configuration DTD//EN"

03     "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

04 <hibernate-configuration>

05     <session-factory name="studentFactory">

06         <property name="connection.driver_class">

07             oracle.jdbc.OracleDriver

08             </property>

09         <property name="connection.url">

10             jdbc:oracle:thin:@localhost:1521:XE

Page 120: Hibernate Introduction

11         </property>

12         <property name="connection.username">

13             system

14         </property>

15         <property name="connection.password">

16             system

17         </property>

18         <property name="connection.pool_size">5</property>

19         <!-- SQL dialect -->

20         <property name="dialect">

21             org.hibernate.dialect.OracleDialect

22             </property>

23         <!-- Echo all executed SQL to stdout -->

24         <property name="show_sql">false</property>

25         <property name="hbm2ddl.auto">update</property>

26         <mapping resource="com\\xml\\Student.hbm.xml" />

27     </session-factory>

28 </hibernate-configuration>

Student.hbm.xml:

01 <?xml version="1.0"?>

02 <!DOCTYPE hibernate-mapping PUBLIC

03     "-//Hibernate/Hibernate Mapping DTD//EN"

04     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

05 <hibernate-mapping>

Page 121: Hibernate Introduction

06     <class name="com.candidjava.Student" table="Student">

07         <id name="id" type="long" column="Id">

08             <generator class="increment" />

09         </id>

10         <property name="stuid" column="StuId" not-null="true" />

11         <property name="sturegno" column="StuRegNO" />

12         <property name="stuname" column="StuName" />

13         <property name="stumark1" column="StuMark1" />

14         <property name="stumark2" column="StuMark2" />

15         <property name="degree" column="Degree" />

16  

17         <property name="mobileno" column="MobileNo" />

18         <property name="mailid" column="MailId" />

19     </class>

20 </hibernate-mapping>

web.xml:

01<?xml version="1.0" encoding="UTF-8"?>

02

<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http:// java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

03     <display-name>SessionObjectUpdate</display-name>

04     <welcome-file-list>

05         <welcome-file>index.html</welcome-file>

06         <welcome-file>index.htm</welcome-file>

Page 122: Hibernate Introduction

07         <welcome-file>index.jsp</welcome-file>

08         <welcome-file>default.html</welcome-file>

09         <welcome-file>default.htm</welcome-file>

10         <welcome-file>default.jsp</welcome-file>

11     </welcome-file-list>

12 </web-app>

Page 123: Hibernate Introduction

OUTPUT:

Page 128: Hibernate Introduction

Hibernate Session.Load() Example Program in Eclipse

Hibernate + session.load() + Eclipse

Load:

This program explains how to process only one field in a record using Load.

Load()– Method is used to  process only one record at a time.

If ID doesn’t exist it provides No row with the given identifier exists: [com.candidjava.Student#9]

Page 130: Hibernate Introduction

 

 

 

 

 

 

 

 

 JAR Files:

Page 131: Hibernate Introduction

 

 

 

 

 

 

 

 

 

 

LoadStudent.java:

01 package com.candidjava;

02  

03 public class LoadStudent {

04     public static void main(String[] args) {

05         try {

06             LoadStudentControl ctrl = new LoadStudentControl();

07             ctrl.addNewStudent(args);

08         } catch (Exception e) {

09             System.out.println(e.getMessage());

10         }

11     }

12 }

LoadStudentControl.java:

01 package com.candidjava;

Page 132: Hibernate Introduction

02  

03 import java.sql.*;

04 import java.io.*;

05 import org.hibernate.*;

06 import org.hibernate.cfg.*;

07  

08 public class LoadStudentControl {

09  

10     private static SessionFactory sessionFactory;

11  

12     private Session getSession() {

13         Session s = null;

14         try {

15             sessionFactory = new Configuration().configure(

16                     "com\\xml\\hibernate.cfg.xml").buildSessionFactory();

17             s = sessionFactory.openSession();

18         } catch (HibernateException e) {

19             System.out.println(e.getMessage());

20         }

21         return s;

22     }

23  

24     public void addNewStudent(String[] args) throws Exception {

25         try {

Page 133: Hibernate Introduction

26             Session s = getSession();

27             Transaction tx = s.beginTransaction();

28             Student stu = new Student();

29  

30             DataInputStream d = new DataInputStream(System.in);

31             System.out.println("Enter ID You want Change");

32  

33             int id = Integer.parseInt(d.readLine());

34             System.out.println("Enter Degree to Change");

35             String newDegree = d.readLine();

36             Student ss = new Student();

37             Student get = (Student) s.load(Student.class, new Long(id));

38             get.setDegree(newDegree);

39             tx.commit();

40             System.out.println("\n\n Details Added \n");

41         } catch (HibernateException e) {

42             System.out.println(e.getMessage());

43             System.out.println("error");

44         }

45  

46     }

47  

48 }// end of class

Student.java:

01 package com.candidjava;

Page 134: Hibernate Introduction

02  

03 import java.io.*;

04  

05 public class Student implements Serializable {

06     private long id;

07     private String stuid;

08     private String sturegno;

09     private String stuname;

10     private String stumark1;

11     private String stumark2;

12     private String degree;

13     private String mobileno;

14     private String mailid;

15  

16     public long getId() {

17         return id;

18     }

19  

20     public String getStuid() {

21         return stuid;

22     }

23  

24     public String getSturegno() {

Page 135: Hibernate Introduction

25         return sturegno;

26     }

27  

28     public String getStuname() {

29         return stuname;

30     }

31  

32     public String getStumark1() {

33         return stumark1;

34     }

35  

36     public String getStumark2() {

37         return stumark2;

38     }

39  

40     public String getDegree() {

41         return degree;

42     }

43  

44     public String getMobileno() {

45         return mobileno;

46     }

47  

48     public String getMailid() {

Page 136: Hibernate Introduction

49         return mailid;

50     }

51  

52     public void setId(long string) {

53         id = string;

54     }

55  

56     public void setStuid(String string) {

57         stuid = string;

58     }

59  

60     public void setSturegno(String string) {

61         sturegno = string;

62     }

63  

64     public void setStuname(String string) {

65         stuname = string;

66     }

67  

68     public void setStumark1(String string) {

69         stumark1 = string;

70     }

71  

72     public void setStumark2(String string) {

73         stumark2 = string;

Page 137: Hibernate Introduction

74     }

75  

76     public void setDegree(String string) {

77         degree = string;

78     }

79  

80     public void setMobileno(String string) {

81         mobileno = string;

82     }

83  

84     public void setMailid(String string) {

85         mailid = string;

86     }

87  

88 }

hibernate.cfg.xml:

01 <!DOCTYPE hibernate-configuration PUBLIC

02     "-//Hibernate/Hibernate Configuration DTD//EN"

03     "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

04 <hibernate-configuration>

05     <session-factory name="studentFactory">

06         <property name="connection.driver_class">

07             oracle.jdbc.OracleDriver

Page 138: Hibernate Introduction

08             </property>

09         <property name="connection.url">

10             jdbc:oracle:thin:@localhost:1521:XE

11         </property>

12         <property name="connection.username">

13             system

14         </property>

15         <property name="connection.password">

16             system

17         </property>

18         <property name="connection.pool_size">5</property>

19         <!-- SQL dialect -->

20         <property name="dialect">

21             org.hibernate.dialect.OracleDialect

22             </property>

23         <!-- Echo all executed SQL to stdout -->

24         <property name="show_sql">false</property>

25         <property name="hbm2ddl.auto">update</property>

26         <mapping resource="com\\xml\\Student.hbm.xml" />

27     </session-factory>

28 </hibernate-configuration>

Student.hbm.xml:

01 <?xml version="1.0"?>

02 <!DOCTYPE hibernate-mapping PUBLIC

Page 139: Hibernate Introduction

03     "-//Hibernate/Hibernate Mapping DTD//EN"

04     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

05 <hibernate-mapping>

06     <class name="com.candidjava.Student" table="Student">

07         <id name="id" type="long" column="Id">

08             <generator class="increment" />

09         </id>

10         <property name="stuid" column="StuId" not-null="true" />

11         <property name="sturegno" column="StuRegNO" />

12         <property name="stuname" column="StuName" />

13         <property name="stumark1" column="StuMark1" />

14         <property name="stumark2" column="StuMark2" />

15         <property name="degree" column="Degree" />

16  

17         <property name="mobileno" column="MobileNo" />

18         <property name="mailid" column="MailId" />

19     </class>

20 </hibernate-mapping>

web.xml:

01<?xml version="1.0" encoding="UTF-8"?>

02

<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http:// java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

03     <display-name>SessionObjectLoad</display-name>

Page 140: Hibernate Introduction

04     <welcome-file-list>

05         <welcome-file>index.html</welcome-file>

06         <welcome-file>index.htm</welcome-file>

07         <welcome-file>index.jsp</welcome-file>

08         <welcome-file>default.html</welcome-file>

09         <welcome-file>default.htm</welcome-file>

10         <welcome-file>default.jsp</welcome-file>

11     </welcome-file-list>

12 </web-app>

Page 141: Hibernate Introduction

OUTPUT:

Page 144: Hibernate Introduction

Hibernate Session.get() Example Program in Eclipse

Hibernate + session.get() + Eclipse

Get:

This program explains how to retrive single row  in a record using sessio.get() method

session.get()–  Method is used to  process only one record at a time.

If ID doesn’t exist it provides null 

Page 146: Hibernate Introduction

 

 

 

 

 

 

 

 

JAR Files:

Page 147: Hibernate Introduction

 

 

 

 

 

 

 

 

 

 

 

 

GetStudent.java:

01 package com.candidjava;

02  

03 public class GetStudent {

04     public static void main(String[] args) {

05         try {

06             GetStudentControl ctrl = new GetStudentControl();

07             ctrl.addNewStudent(args);

08         } catch (Exception e) {

09             System.out.println(e.getMessage());

10         }

11     }

12 }

Page 148: Hibernate Introduction

GetStudentControl.java:

01 package com.candidjava;

02  

03 import java.sql.*;

04 import java.io.*;

05 import org.hibernate.*;

06 import org.hibernate.cfg.*;

07  

08 public class GetStudentControl {

09  

10     private static SessionFactory sessionFactory;

11  

12     private Session getSession() {

13         Session s = null;

14         try {

15             sessionFactory = new Configuration().configure(

16                     "com\\xml\\hibernate.cfg.xml").buildSessionFactory();

17             s = sessionFactory.openSession();

18         } catch (HibernateException e) {

19             System.out.println(e.getMessage());

20         }

21         return s;

22     }

23  

Page 149: Hibernate Introduction

24     public void addNewStudent(String[] args) throws Exception {

25         try {

26             Session s = getSession();

27             Transaction tx = s.beginTransaction();

28             Student stu = new Student();

29             DataInputStream d = new DataInputStream(System.in);

30             System.out.println("Enter ID to Change Values");

31             Long id = Long.parseLong(d.readLine());

32             Student ss = new Student();

33             String stud = ss.getStuid();

34             Student get = (Student) s.get(Student.class, id);

35             System.out.println("Enter Degree to Change Values");

36             String degree = d.readLine();

37             get.setDegree(degree);

38             tx.commit();

39             System.out.println("\n\n Details Added \n");

40         } catch (HibernateException e) {

41             System.out.println(e.getMessage());

42             System.out.println("error");

43         }

44  

45     }

46  

47 }// end of class

Page 150: Hibernate Introduction

Student.java:

01 package com.candidjava;

02  

03 import java.io.*;

04  

05 public class Student implements Serializable {

06     private long id;

07     private String stuid;

08     private String sturegno;

09     private String stuname;

10     private String stumark1;

11     private String stumark2;

12     private String degree;

13     private String mobileno;

14     private String mailid;

15  

16     public long getId() {

17         return id;

18     }

19  

20     public String getStuid() {

21         return stuid;

22     }

23  

Page 151: Hibernate Introduction

24     public String getSturegno() {

25         return sturegno;

26     }

27  

28     public String getStuname() {

29         return stuname;

30     }

31  

32     public String getStumark1() {

33         return stumark1;

34     }

35  

36     public String getStumark2() {

37         return stumark2;

38     }

39  

40     public String getDegree() {

41         return degree;

42     }

43  

44     public String getMobileno() {

45         return mobileno;

46     }

47  

Page 152: Hibernate Introduction

48     public String getMailid() {

49         return mailid;

50     }

51  

52     public void setId(long string) {

53         id = string;

54     }

55  

56     public void setStuid(String string) {

57         stuid = string;

58     }

59  

60     public void setSturegno(String string) {

61         sturegno = string;

62     }

63  

64     public void setStuname(String string) {

65         stuname = string;

66     }

67  

68     public void setStumark1(String string) {

69         stumark1 = string;

70     }

71  

Page 153: Hibernate Introduction

72     public void setStumark2(String string) {

73         stumark2 = string;

74     }

75  

76     public void setDegree(String string) {

77         degree = string;

78     }

79  

80     public void setMobileno(String string) {

81         mobileno = string;

82     }

83  

84     public void setMailid(String string) {

85         mailid = string;

86     }

87  

88 }

hibernate.cfg.xml:

01 <!DOCTYPE hibernate-configuration PUBLIC

02     "-//Hibernate/Hibernate Configuration DTD//EN"

03     "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

04 <hibernate-configuration>

05     <session-factory name="studentFactory">

06         <property name="connection.driver_class">

Page 154: Hibernate Introduction

07             oracle.jdbc.OracleDriver

08             </property>

09         <property name="connection.url">

10             jdbc:oracle:thin:@localhost:1521:XE

11         </property>

12         <property name="connection.username">

13             system

14         </property>

15         <property name="connection.password">

16             system

17         </property>

18         <property name="connection.pool_size">5</property>

19         <!-- SQL dialect -->

20         <property name="dialect">

21             org.hibernate.dialect.OracleDialect

22             </property>

23         <!-- Echo all executed SQL to stdout -->

24         <property name="show_sql">false</property>

25         <property name="hbm2ddl.auto">update</property>

26         <mapping resource="com\\xml\\Student.hbm.xml" />

27     </session-factory>

28 </hibernate-configuration>

Student.hbm.xml:

01 <?xml version="1.0"?>

Page 155: Hibernate Introduction

02 <!DOCTYPE hibernate-mapping PUBLIC

03     "-//Hibernate/Hibernate Mapping DTD//EN"

04     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

05 <hibernate-mapping>

06     <class name="com.candidjava.Student" table="Student">

07         <id name="id" type="long" column="Id">

08             <generator class="increment" />

09         </id>

10         <property name="stuid" column="StuId" not-null="true" />

11         <property name="sturegno" column="StuRegNO" />

12         <property name="stuname" column="StuName" />

13         <property name="stumark1" column="StuMark1" />

14         <property name="stumark2" column="StuMark2" />

15         <property name="degree" column="Degree" />

16  

17         <property name="mobileno" column="MobileNo" />

18         <property name="mailid" column="MailId" />

19     </class>

20 </hibernate-mapping>

web.xml:

01 <?xml version="1.0" encoding="UTF-8"?>

02

<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/   j2ee/web-app_2_4.xsd">

Page 156: Hibernate Introduction

03     <display-name>SessionObjectGet</display-name>

04     <welcome-file-list>

05         <welcome-file>index.html</welcome-file>

06         <welcome-file>index.htm</welcome-file>

07         <welcome-file>index.jsp</welcome-file>

08         <welcome-file>default.html</welcome-file>

09         <welcome-file>default.htm</welcome-file>

10         <welcome-file>default.jsp</welcome-file>

11     </welcome-file-list>

12 </web-app>

Page 157: Hibernate Introduction

OUTPUT:

Page 160: Hibernate Introduction

Hibernate Session.Delete() Example Program in Eclipse

Page 162: Hibernate Introduction

 

 

 

 

 

 

 

 

 

 

 

JAR Files:

Page 163: Hibernate Introduction

 

 

 

 

 

 

 

 

 

 

DeleteStudent.java:

01 package com.candidjava;

02  

03 import java.io.*;

04  

05 public class DeleteStudent {

06     public static void main(String[] args) throws Exception {

07  

08         DataInputStream ds = new DataInputStream(System.in);

09         System.out.println("Enter the ID You want to Delete");

10         int i = Integer.parseInt(ds.readLine());

11         try {

12             DeleteStudentControll delete = new DeleteStudentControll();

13             delete.DeleteUser(i);

14         } catch (Exception e) {

Page 164: Hibernate Introduction

15             System.out.println(e.getMessage());

16         }

17     }

18 }

DeleteStudentControll.java:

01 package com.candidjava;

02  

03 import java.util.*;

04 import org.hibernate.*;

05 import org.hibernate.cfg.*;

06 import java.io.*;

07 import org.hibernate.Transaction;

08  

09 public class DeleteStudentControll {

10     private static SessionFactory sessionFactory;

11  

12     private Session getSession() {

13         Session s = null;

14         try {

15             sessionFactory = new Configuration().configure(

16                    "com\\xmlFiles\\hibernate.cfg.xml").buildSessionFactory();

17             s = sessionFactory.openSession();

18         } catch (HibernateException e) {

Page 165: Hibernate Introduction

19             System.out.println(e.getMessage());

20         }

21         return s;

22     }

23  

24     public void DeleteUser(int id) {

25         try {

26  

27             System.out.println(id);

28             Session s = getSession();

29             Transaction tx = s.beginTransaction();

30             Student u = (Student) s.load(Student.class, new Long(id));// difference between get() and load()

31             s.delete(u);

32             tx.commit();

33             System.out.println("\n\n Record Deleted \n");

34         } catch (HibernateException e) {

35             System.out.println(e.getMessage());

36         }

37     }

38  

39 }

Student.java:

01 package com.candidjava;

02 import java.io.*;

Page 166: Hibernate Introduction

03 public class Student implements Serializable

04 {

05     private long id;

06     private String stuid;

07     private String sturegno;

08     private String stuname;

09     private String stumark1;

10     private String stumark2;

11     private String degree;

12     private String mobileno;

13     private String mailid;

14  

15     public long getId() {

16         return id;

17     }

18  

19     public String getStuid() {

20         return stuid;

21     }

22     public String getSturegno() {

23         return sturegno;

24     }

25     public String getStuname() {

26         return stuname;

Page 167: Hibernate Introduction

27     }

28     public String getStumark1() {

29         return stumark1;

30     }

31     public String getStumark2() {

32         return stumark2;

33     }

34  

35     public String getDegree() {

36         return degree;

37     }

38  

39     public String getMobileno() {

40         return mobileno;

41     }

42     public String getMailid() {

43         return mailid;

44     }

45     public void setId(long string) {

46         id = string;

47     }

48  

49     public void setStuid(String string) {

50         stuid = string;

Page 168: Hibernate Introduction

51     }

52     public void setSturegno(String string) {

53         sturegno = string;

54     }

55     public void setStuname(String string) {

56         stuname = string;

57     }

58     public void setStumark1(String string) {

59         stumark1 = string;

60     }

61     public void setStumark2(String string) {

62         stumark2 = string;

63     }

64     public void setDegree(String string) {

65         degree = string;

66     }

67  

68     public void setMobileno(String string) {

69         mobileno = string;

70     }

71     public void setMailid(String string) {

72         mailid = string;

73     }

74  

Page 169: Hibernate Introduction

75 }

hibernate.cfg.xml:

01 <!DOCTYPE hibernate-configuration PUBLIC

02     "-//Hibernate/Hibernate Configuration DTD//EN"

03     "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

04 <hibernate-configuration>

05     <session-factory name="studentFactory">

06         <property name="connection.driver_class">

07             oracle.jdbc.OracleDriver

08             </property>

09         <property name="connection.url">

10             jdbc:oracle:thin:@localhost:1521:XE

11         </property>

12         <property name="connection.username">

13             system

14         </property>

15         <property name="connection.password">

16             system

17         </property>

18         <property name="connection.pool_size">5</property>

19         <!-- SQL dialect -->

20         <property name="dialect">

21             org.hibernate.dialect.OracleDialect

22             </property>

Page 170: Hibernate Introduction

23         <!-- Echo all executed SQL to stdout -->

24         <property name="show_sql">false</property>

25         <property name="hbm2ddl.auto">update</property>

26         <mapping resource="\com\\xmlFiles\\Student.hbm.xml" />

27     </session-factory>

28 </hibernate-configuration>

Student.hbm.xml:

01 <?xml version="1.0"?>

02 <!DOCTYPE hibernate-mapping PUBLIC

03     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

04     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

05 <hibernate-mapping>

06     <class name="com.candidjava.Student" table="Student">

07         <id name="id" type="long" column="ID">

08             <generator class="increment" />

09         </id>

10         <property name="stuid" column="STUID" not-null="true" />

11         <property name="sturegno" column="STUREGNO" not-null="true" />

12         <property name="stuname" column="STUNAME" not-null="true" />

13         <property name="stumark1" column="STUMARK1" not-null="true" />

14         <property name="stumark2" column="STUMARK2" not-null="true" />

15         <property name="degree" column="DEGREE" not-null="true" />

16         <property name="mobileno" column="MOBILENO" not-null="true" />

17         <property name="mailid" column="MAILID" not-null="true" />

Page 171: Hibernate Introduction

18  

19     </class>

20 </hibernate-mapping>

web.xml:

01 <?xml version="1.0" encoding="UTF-8"?>

02 <web-app id="WebApp_ID" version="2.4"

03    xmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

04    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/ xml/ns/j2ee/web-app_2_4.xsd">

05     <display-name>SessionwithDeleteQuery</display-name>

06     <welcome-file-list>

07         <welcome-file>index.html</welcome-file>

08         <welcome-file>index.htm</welcome-file>

09         <welcome-file>index.jsp</welcome-file>

10         <welcome-file>default.html</welcome-file>

11         <welcome-file>default.htm</welcome-file>

12         <welcome-file>default.jsp</welcome-file>

13     </welcome-file-list>

14 </web-app>

Page 174: Hibernate Introduction

Hibernate Struts Inserting Data Using Eclipse This application will use  join struts and hibernate applications.

Here First  we will  get input from front view html or Jsp page.

Then it will pass model and cotroller using struts application.

Then In controller page input will save the database using Hibernate application.

After save the inputs will pass the front view.

Page 176: Hibernate Introduction

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

JarFiles:

Page 177: Hibernate Introduction
Page 178: Hibernate Introduction

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

AddStudentControl.java:

Here we will use sessionFactory for create connetion Oracle database.

sessionFactory = new Configuration().configure

(“com\\xml\\hibernate.cfg.xml”).buildSessionFactory();

Then we will get input from AddController.java through arguments and then save the database using session.

stu.setStuname(stuname);

s.save(stu);

Then we will use  Transaction statement for  begin and commit the database.

Transaction tx= s.beginTransaction();

tx.commit();01 package com.candidjava;

02  

03 import java.sql.*;

04 import java.io.*;

Page 179: Hibernate Introduction

05 import org.hibernate.*;

06 import org.hibernate.cfg.*;

07  

08 public class AddStudentControl {

09  

10     private static SessionFactory sessionFactory;

11  

12     private Session getSession() {

13         Session s = null;

14         try {

15             sessionFactory = new Configuration().configure(

16                     "com\\xml\\hibernate.cfg.xml").buildSessionFactory();

17             s = sessionFactory.openSession();

18         } catch (HibernateException e) {

19             System.out.println(e.getMessage());

20         }

21         return s;

22     }

23  

24     public void addNewStudent(String args, String args1, String args2)

25             throws Exception {

26         try {

27             Session s = getSession();

28             Transaction tx = s.beginTransaction();

Page 180: Hibernate Introduction

29             Student stu = new Student();

30  

31             System.out.println("Student name");

32             String stuname = args;

33             String studegree = args1;

34             String stumobileno = args2;

35  

36             stu.setStuname(stuname);

37             stu.setStudegree(studegree);

38             stu.setStumobileno(stumobileno);

39  

40             s.save(stu);

41  

42             tx.commit();

43  

44             System.out.println("\n\n Details Added \n");

45         } catch (HibernateException e) {

46             System.out.println("error1" + e.getMessage());

47             System.out.println("error1");

48         }

49  

50     }

51  

52 }// end of class

Page 181: Hibernate Introduction

MyController.java: 

It is a Controller class .It will extends Action class.

MyModel m = (MyModel)fm;//Here model class object will store in the variable name m.

String s=m.getName();String s1=m.getDegree();String s2=m.getMobileno();//Here get input from model class.

AddStudentControl ctrl = new AddStudentControl();//Here we will create object for AddStudentControl class

ctrl.addNewStudent(s,s1,s2);//Here pass the input to AddNewStudent method through Arguments from

MyController class.01 package com.candidjava;

02  

03 import javax.servlet.http.*;

04 import org.apache.struts.action.Action;

05 import org.apache.struts.action.ActionForm;

06 import org.apache.struts.action.ActionForward;

07 import org.apache.struts.action.ActionMapping;

08  

09 public class MyController extends Action {

10     public ActionForward execute(ActionMapping map, ActionForm fm,

11            HttpServletRequest req, HttpServletResponse

res) throws Exception {

12         HttpSession ses = req.getSession(true);

13  

14         MyModel m = (MyModel) fm;

15         String s = m.getName();

16         String s1 = m.getDegree();

17         String s2 = m.getMobileno();

18  

19         System.out.println(s);

20         System.out.println(s1);

Page 182: Hibernate Introduction

21         System.out.println(s2);

22  

23         AddStudentControl ctrl = new AddStudentControl();

24         ctrl.addNewStudent(s, s1, s2);

25  

26         ses.setAttribute("s", s);

27         ses.setAttribute("s1", s1);

28         ses.setAttribute("s2", s2);

29         if (s.equals("")) {

30             return map.findForward("error");

31         }

32         return map.findForward("success");

33  

34     }

35 }

MyModel.java:

It is a Model class. It will extends ActionForm class which is  a Serializable class. Then here we will use

get and set method.

private String name;

public String getName(){return name;}

public void setName(String n){name=n;      }01 package com.candidjava;

02  

03 import org.apache.struts.action.ActionForm;

04  

05 public class MyModel extends ActionForm {

Page 183: Hibernate Introduction

06     private String name;

07     private String degree;

08     private String mobileno;

09  

10     public void setName(String n) {

11         name = n;

12     }

13  

14     public String getName() {

15         return name;

16     }

17  

18     public void setDegree(String n) {

19         degree = n;

20     }

21  

22     public String getDegree() {

23         return degree;

24     }

25  

26     public void setMobileno(String n) {

27         mobileno = n;

28     }

29  

Page 184: Hibernate Introduction

30     public String getMobileno() {

31         return mobileno;

32     }

33  

34 }

Student.java:

It is a Pojo class. This class Must be a Serializable. Then here we will use get and set method.

private long id;

public long getId() {return id;}

public void setId(long String) {id = String;}01 package com.candidjava;

02  

03 import java.io.*;

04  

05 public class Student implements Serializable {

06     private long id;

07     private String stuname;

08     private String studegree;

09     private String stumobileno;

10  

11     public long getId() {

12         return id;

13     }

14  

15     public String getStuname() {

Page 185: Hibernate Introduction

16         return stuname;

17     }

18  

19     public String getStudegree() {

20         return studegree;

21     }

22  

23     public String getStumobileno() {

24         return stumobileno;

25     }

26  

27     public void setId(long string) {

28         id = string;

29     }

30  

31     public void setStuname(String string) {

32         stuname = string;

33     }

34  

35     public void setStudegree(String string) {

36         studegree = string;

37     }

38  

39     public void setStumobileno(String string) {

Page 186: Hibernate Introduction

40         stumobileno = string;

41     }

42  

43 }

web.xml:

<servlet-name>action</servlet-name>

<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>//This tag will load the ActionServlet class.

<init-param><param-name>config</param-name>

<param-value>/WEB-INF/struts-config.xml</param-value> </init-param>//This tag will use configure the struts-

config.xml.

</servlet>

<servlet-mapping><servlet-name>action</servlet-name>

<url-pattern>*.do</url-pattern> </servlet-mapping>//here *.do invoke the ActionServlet class.

01<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">

02 <web-app>

03  

04     <servlet>

05         <servlet-name>action</servlet-name>

06        <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>

07         <init-param>

08             <param-name>config</param-name>

09             <param-value>/WEB-INF/struts-config.xml</param-value>

10         </init-param>

11  

12         <init-param>

13             <param-name>debug</param-name>

Page 187: Hibernate Introduction

14             <param-value>2</param-value>

15         </init-param>

16         <init-param>

17             <param-name>detail</param-name>

18             <param-value>2</param-value>

19         </init-param>

20         <load-on-startup>2</load-on-startup>

21     </servlet>

22  

23     <servlet-mapping>

24         <servlet-name>action</servlet-name>

25         <url-pattern>*.do</url-pattern>

26     </servlet-mapping>

27  

28     <welcome-file-list>

29         <welcome-file>Login.jsp</welcome-file>

30     </welcome-file-list>

31  

32     <taglib>

33         <taglib-uri>/struts-html</taglib-uri>

34         <taglib-location>/WEB-INF/struts-html.tld</taglib-location>

35     </taglib>

36  

37     <security-constraint>

Page 188: Hibernate Introduction

38         <web-resource-collection>

39             <web-resource-name>My secure resources</web-resource-name>

40            <description>Resources to be placed under security control.</description>

41             <url-pattern>/private/*</url-pattern>

42             <url-pattern>/registered/*</url-pattern>

43         </web-resource-collection>

44         <auth-constraint>

45             <role-name>guest</role-name>

46         </auth-constraint>

47     </security-constraint>

48  

49     <login-config>

50         <auth-method>BASIC</auth-method>

51         <realm-name>WebApp</realm-name>

52     </login-config>

53  

54     <security-role>

55         <description>The role allowed to access our content</description>

56         <role-name>guest</role-name>

57     </security-role>

58  

59 </web-app>

struts-config.xml:

<form-beans><form-bean type=”com.candidjava.MyModel” name =”model” />

Page 189: Hibernate Introduction

</form-beans>//Beans tag will use for model class.Here name=”model” which is same as the controller

name=”model”.

<action-mappings><action path=”/myActionForm” type=”com.candidjava.MyController” name=”model”

input=”/Login.jsp”>//Action mapping tag will use both view and controller.Action tag will use for controller

class.Here path=”/myActionForm” which is same as the front view Login.jsp action=”/myActionForm”.

<forward name=”success” path=”/Success.jsp” />//Forward tag will use for view page.It will forward  input to the

view page

<forward name=”error” path=”/Fail.jsp” /></action></action-mappings>01 <?xml version="1.0" encoding="ISO-8859-1" ?>

02  

03 <!DOCTYPE struts-config PUBLIC

04           "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"

05           "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">

06 <struts-config>

07     <form-beans>

08         <form-bean type="com.candidjava.MyModel" name="model" />

09     </form-beans>

10     <action-mappings>

11         <action path="/myActionForm" type="com.candidjava.MyController"

12             name="model" input="/Login.jsp">

13             <forward name="success" path="/Success.jsp" />

14             <forward name="error" path="/Fail.jsp" />

15  

16         </action>

17     </action-mappings>

18 </struts-config>

Page 190: Hibernate Introduction

hibernate.cfg.xml:

01 <!DOCTYPE hibernate-configuration PUBLIC

02     "-//Hibernate/Hibernate Configuration DTD//EN"

03     "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

04 <hibernate-configuration>

05     <session-factory name="studentFactory">

06         <property name="connection.driver_class">

07             oracle.jdbc.OracleDriver

08             </property>

09         <property name="connection.url">

10             jdbc:oracle:thin:@localhost:1521:XE

11         </property>

12         <property name="connection.username">

13             system

14         </property>

15         <property name="connection.password">

16             system

17         </property>

18         <property name="connection.pool_size">5</property>

19         <!-- SQL dialect -->

20         <property name="dialect">

21             org.hibernate.dialect.OracleDialect

22             </property>

23         <!-- Echo all executed SQL to stdout -->

24         <property name="show_sql">false</property>

Page 191: Hibernate Introduction

25         <property name="hbm2ddl.auto">update</property>

26         <mapping resource="com\\xml\\Student.hbm.xml" />

27     </session-factory>

28 </hibernate-configuration>

Student.hbm.xml:

<class name=”com.candidjava.Student” table=”student” >//This tag will use for mapping Pojo class name and 

table name.

<id name=”id” type=”long” column =”ID”><generator class=”increment”/></id>//This tag will use for generating

primary key id and also increment the id value.

<property name=”name” column=”name” not-null=”true”/>//This property will use for mapping the pojo class

veriable name(name=”name” ) and  table attribute name(column=”name”).

</class>”/>//End of the class.01 <?xml version="1.0"?>

02 <!DOCTYPE hibernate-mapping PUBLIC

03     "-//Hibernate/Hibernate Mapping DTD//EN"

04     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

05 <hibernate-mapping>

06     <class name="com.candidjava.Student" table="StudentDet">

07         <id name="id" type="long" column="Id">

08             <generator class="increment" />

09         </id>

10         <property name="stuname" column="StuName" not-null="true" />

11         <property name="studegree" column="Degree" />

12         <property name="stumobileno" column="MobileNo" />

13  

14     </class>

15 </hibernate-mapping>