Professional Documents
Culture Documents
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.
Its 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 datas 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 datas 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.
Hibernate Query Language.
Types:
Create Query
Create Criteria
Sql Query
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>
Hbm file:
hbm file is created for each pojo class.
It is used as mapping in hibernate.
Its like an intermediate between hibernate java class and data base table.
All contents in this file are within hibernate mapping tag.
<hibernate-mapping>
2
3
-------------------------
4
5
</hibernate-mapping>
</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.
ColumnName It may be any column name for table. It must be a primary key for the given table.
Increment Its 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.
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.*;
03
import java.io.*;
04
import org.hibernate.*;
05
import org.hibernate.cfg.*;
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
failed."
32
+ e);
33
34
Session s = sessionFactory.openSession();
35
Transaction tx = s.beginTransaction();
36
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.
01
//package code;
02
import java.io.*;
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
}
}
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.
01
02
03
04 <hibernate-configuration>
05
06
<session-factory name="studentFactory">
<property name="connection.driver_class">
07
oracle.jdbc.OracleDriver
08
</property>
09
10
<property name="connection.url">
jdbc:oracle:thin:@localhost:1521:XE
11
</property>
12
<property name="connection.username">
13
14
15
system
</property>
<property name="connection.password">
16
system
17
</property>
18
<property name="connection.pool_size">5</property>
19
20
<property name="dialect">
21
org.hibernate.dialect.OracleDialect
22
</property>
23
24
<property name="show_sql">false</property>
25
26
27
</session-factory>
</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.
01
<?xml version="1.0"?>
02
03
04
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
05
06
07
08
<hibernate-mapping >
<class name="Student" table="Stutbl" >
<id name="id" type="long" column ="ID">
<generator class="increment"/>
09
</id>
10
11
12
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
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\asmattrs.jar;lib\hsqldb.jar;lib\jta.jar;lib\commons-logging-1.0.4.jar;lib\dom4j-1.6.jar;lib\log4j1.2.9.jar;lib\wlsqlserver.jar;lib\commons-collections-2.1.1.jar;lib\ehcache-1.1.jar;lib\cglib2.1.jar;lib\asm.jar;lib\ojdbc14.jar;%classpath%;.;
OUTPUT:
Step 7:
Run the batch file.
Step 8:
1
2
3
4
5
6
7
8
Creating hibernate.cfg.xml
Creating Student.hbm.xml
OutPut for Running Console hiberanate application
OutPut for Running Oracle hibernate application
This application we will used to insert the datas in database like sql,oracle,DB2 and etc.
Here We will see how to save the datas in oracle database .
package com.candidjava;
02
03
04
05
06
07
ctrl.addNewStudent(args);
08
} catch (Exception e) {
09
10
System.out.println(e.getMessage());
}
11
12
}
}
AddStudentControl.java:
Here we will use sessionFactory for create connetion Oracle database.
01
package com.candidjava;
02
03
import java.sql.*;
04
import java.io.*;
05
import org.hibernate.*;
06
import org.hibernate.cfg.*;
07
08
09
10
11
12
13
Session s = null;
14
try {
15
16
17
18
19
"com\\xmlFiles\\hibernate.cfg.xml").buildSessionFactory
();
s = sessionFactory.openSession();
} catch (HibernateException e) {
System.out.println(e.getMessage());
20
21
22
return s;
}
23
24
25
26
27
Transaction tx = s.beginTransaction();
28
29
30
System.out.println("*********************\n");
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
61
} catch (HibernateException e) {
62
System.out.println("error1" + e.getMessage());
63
64
System.out.println("error1");
}
65
66
67
68
Student.java:
It is a Pojo class. This class Must be a Serializable. Then here we will use get and set method.
01
package com.candidjava;
02
03
import java.io.*;
04
05
06
07
08
09
10
11
12
13
14
15
16
17
return id;
18
19
20
21
return stuid;
22
23
24
25
26
return sturegno;
}
27
28
29
return stuname;
30
31
32
33
34
return stumark1;
}
35
36
37
38
return stumark2;
}
39
40
41
return degree;
42
43
44
45
46
return mobileno;
}
47
48
49
return mailid;
50
51
52
53
id = string;
54
55
56
57
58
stuid = string;
}
59
60
61
62
sturegno = string;
}
63
64
65
66
stuname = string;
}
67
68
69
70
stumark1 = string;
}
71
72
73
74
stumark2 = string;
}
75
76
77
78
degree = string;
}
79
80
81
82
mobileno = string;
}
83
84
85
86
mailid = string;
}
87
88
01
02
03
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
04 <hibernate-configuration>
05
06
<session-factory name="studentFactory">
<property name="connection.driver_class">
07
oracle.jdbc.OracleDriver
08
</property>
09
10
<property name="connection.url">
jdbc:oracle:thin:@localhost:1521:XE
11
</property>
12
<property name="connection.username">
13
14
15
system
</property>
<property name="connection.password">
16
system
17
</property>
18
<property name="connection.pool_size">5</property>
19
20
<property name="dialect">
21
org.hibernate.dialect.OracleDialect
22
</property>
23
24
<property name="show_sql">false</property>
25
<property name="hbm2ddl.auto">update</property>
26
27
</session-factory>
28
</hibernate-configuration>
01
<?xml version="1.0"?>
02
03
04
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
05 <hibernate-mapping>
06
07
08
09
</id>
10
11
12
13
14
15
16
17
18
19
20
</class>
</hibernate-mapping>
web.xml:
01
02
03
xmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/200
1/XMLSchema-instance"
04
xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://java.sun.com/
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>
OUTPUT:
Step 7
1
2
3
4
5
6
7
8
Creating hibernate.cfg.xml
Creating Student.hbm.xml
OutPut for Running Console hiberanate application
OutPut for Running Oracle hibernate application
This application we will used to insert the datas in database like sql,oracle,DB2 and etc.
Here We will see how to save the datas in oracle database .
01
package com.candidjava;
02
03
//package code;
04
import java.sql.*;
05
import java.io.*;
06
import org.hibernate.*;
07
import org.hibernate.cfg.*;
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
try {
28
// begin try
29
30
31
} catch (Exception e) {
32
33
System.out.println(e.getMessage());
System.err.println("Initial SessionFactory creation
failed."
34
+ e);
35
36
Session s = sessionFactory.openSession();
37
Transaction tx = s.beginTransaction();
38
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
}
}
}
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;
01
package com.candidjava;
02
03
//package code;
04
import java.io.*;
05
06
07
08
09
10
11
12
13
return id;
14
15
16
17
return name;
18
19
20
21
return degree;
22
23
24
25
return phone;
26
27
28
29
id = string;
30
31
32
33
34
name = string;
}
35
36
37
38
degree = string;
}
39
40
41
42
phone = string;
}
43
44
45
return name;
46
47
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
02
03
04 <hibernate-configuration>
05
06
<session-factory name="studentFactory">
<property name="connection.driver_class">
07
oracle.jdbc.OracleDriver
08
</property>
09
10
<property name="connection.url">
jdbc:oracle:thin:@localhost:1521:XE
11
</property>
12
<property name="connection.username">
13
14
15
16
system
</property>
<property name="connection.password">
system
17
</property>
18
<property name="connection.pool_size">5</property>
19
20
<property name="dialect">
21
org.hibernate.dialect.OracleDialect
22
</property>
23
24
<property name="show_sql">true</property>
25
<property name="hbm2ddl.auto">create</property>
26
27
</session-factory>
28
</hibernate-configuration>
01
<?xml version="1.0"?>
02
03
04
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
05 <hibernate-mapping>
06
07
08
09
</id>
10
11
12
13
14
</class>
</hibernate-mapping>
OUTPUT:
Step 7
JAR Files:
SaveStudent.java:
01
package com.candidjava;
02
03
04
05
06
07
ctrl.addNewStudent(args);
08
} catch (Exception e) {
09
10
System.out.println(e.getMessage());
}
11
12
}
}
SaveStudentControl.java:
01
02
package com.candidjava;
03
import java.sql.*;
04
import java.util.List;
05
import java.io.*;
06
import org.hibernate.*;
07
import org.hibernate.cfg.*;
08
09
10
11
12
13
14
15
16
17
18
19
"com\\xml\\hibernate.cfg.xml").buildSessionFactory();
s = sessionFactory.openSession();
} catch (HibernateException e) {
20
System.out.println(e.getMessage());
21
22
return s;
23
24
25
26
27
Session s = getSession();
28
Transaction tx = s.beginTransaction();
29
30
31
System.out.println("*********************\n");
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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);
55
stu.setDegree(degree);
56
stu.setMobileno(mobileno);
57
stu.setMailid(mailid);
58
s.save(stu);
59
60
// Query q =
// 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
64
tx.commit();
65
66
67
System.out.println(e.getMessage());
68
System.out.println("error");
69
70
71
72
73
Student.java:
01
package com.candidjava;
02
03
import java.io.*;
04
05
06
07
08
09
10
11
12
13
14
15
16
17
return id;
18
19
20
21
return stuid;
22
23
24
25
26
return sturegno;
}
27
28
29
return stuname;
30
31
32
33
34
return stumark1;
}
35
36
37
38
return stumark2;
}
39
40
41
return degree;
42
43
44
45
46
return mobileno;
}
47
48
49
return mailid;
50
51
52
53
id = string;
54
55
56
57
58
stuid = string;
}
59
60
61
62
sturegno = string;
}
63
64
65
66
stuname = string;
}
67
68
69
70
stumark1 = string;
}
71
72
73
74
stumark2 = string;
}
75
76
77
78
degree = string;
}
79
80
81
82
mobileno = string;
}
83
84
85
86
mailid = string;
}
87
88
hibernate.cfg.xml:
01
02
03
04 <hibernate-configuration>
05
06
<session-factory name="studentFactory">
<property name="connection.driver_class">
07
oracle.jdbc.OracleDriver
08
</property>
09
10
<property name="connection.url">
jdbc:oracle:thin:@localhost:1521:XE
11
</property>
12
<property name="connection.username">
13
14
15
system
</property>
<property name="connection.password">
16
system
17
</property>
18
<property name="connection.pool_size">5</property>
19
20
<property name="dialect">
21
org.hibernate.dialect.OracleDialect
22
</property>
23
24
<property name="show_sql">false</property>
25
<property name="hbm2ddl.auto">create</property>
26
27
</session-factory>
28
</hibernate-configuration>
Student.hbm.xml:
01
<?xml version="1.0"?>
02
03
04
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
05 <hibernate-mapping>
06
07
08
09
</id>
10
11
12
13
14
15
16
17
18
19
20
</class>
</hibernate-mapping>
web.xml:
01 <?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns=
"http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchem
0
a2
instance"xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://java.sun
.com/xml/ns/j2ee/web-app_2_4.xsd">
03
<display-name>Create</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>
OUTPUT:
JarFiles:
SaveStudent.java:
01
package com.candidjava;
02
03
04
05
06
07
08
09
10
11
12
System.out.println(e.getMessage());
}
}
}
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
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
01
package com.candidjava;
02
03
import java.io.*;
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
return id;
}
public String getStuid() {
return stuid;
}
public String getSturegno() {
return sturegno;
}
28
29
30
31
return stuname;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
}
public String getStumark1() {
return stumark1;
}
public String getStumark2() {
return stumark2;
}
public String getDegree() {
return degree;
}
public String getMobileno() {
return mobileno;
}
public String getMailid() {
return mailid;
}
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
Web.xml:
01
02
05
06
07
08
09
10
11
12
13
14
<display-name>Create</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
Hibernate.cfg.xml:
<property name="hbm2ddl.auto">update</property>
26
27
28
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
07
08
09
10
11
12
13
14
15
16
17
18
19
OUTPUT:
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 datas for that ID as a new record.
Project Structure:
JAR Files:
SaveStudent.java:
01
02
package com.candidjava;
03
04
05
06
07
ctrl.addNewStudent(args);
08
} catch (Exception e) {
09
10
System.out.println(e.getMessage());
}
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
09
10
11
12
13
Session s = null;
14
try {
15
16
"com\\xml\\hibernate.cfg.xml").buildSessionFactory();
17
18
s = sessionFactory.openSession();
} catch (HibernateException e) {
19
20
System.out.println(e.getMessage());
}
21
22
return s;
}
23
24
25
26
27
Transaction tx = s.beginTransaction();
28
29
30
System.out.println("*********************\n");
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
59
tx.commit();
60
61
} catch (HibernateException e) {
62
System.out.println(e.getMessage());
63
64
System.out.println("error");
}
65
66
67
68
Student.java:
01
package com.candidjava;
02
03
import java.io.*;
04
05
06
07
08
09
10
11
12
13
14
15
16
17
return id;
18
19
20
21
return stuid;
22
23
24
25
26
return sturegno;
}
27
28
29
return stuname;
30
31
32
33
34
return stumark1;
}
35
36
37
38
return stumark2;
}
39
40
41
return degree;
42
43
44
45
46
return mobileno;
}
47
48
49
return mailid;
50
51
52
53
54
55
56
57
58
stuid = string;
}
59
60
61
62
sturegno = string;
}
63
64
65
66
stuname = string;
}
67
68
69
70
stumark1 = string;
}
71
72
73
74
stumark2 = string;
}
75
76
77
78
79
degree = string;
}
80
81
82
mobileno = string;
}
83
84
85
86
mailid = string;
}
87
88
hibernate.cfg.xml:
01
02
03
04 <hibernate-configuration>
05
06
<session-factory name="studentFactory">
<property name="connection.driver_class">
07
oracle.jdbc.OracleDriver
08
</property>
09
10
<property name="connection.url">
jdbc:oracle:thin:@localhost:1521:XE
11
</property>
12
<property name="connection.username">
13
14
15
system
</property>
<property name="connection.password">
16
system
17
</property>
18
<property name="connection.pool_size">5</property>
19
20
<property name="dialect">
21
org.hibernate.dialect.OracleDialect
22
</property>
23
24
<property name="show_sql">false</property>
25
<property name="hbm2ddl.auto">update</property>
26
27
</session-factory>
28
</hibernate-configuration>
Student.hbm.xml:
01
<?xml version="1.0"?>
02
03
04
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
05 <hibernate-mapping>
06
07
08
09
</id>
10
11
12
13
14
15
16
17
18
19
20
</class>
</hibernate-mapping>
web.xml:
01
02
03
xmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/200
1/XMLSchema-instance"
04
xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://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>
OUTPUT:
JAR Files:
SaveOrUpdateStudent.java:
01
package com.candidjava;
02
03
04
05
06
07
ctrl.addNewStudent(args);
08
} catch (Exception e) {
09
10
System.out.println(e.getMessage());
}
11
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
09
10
11
12
13
Session s = null;
14
try {
15
16
"com\\xml\\hibernate.cfg.xml").buildSessionFactory();
17
18
s = sessionFactory.openSession();
} catch (HibernateException e) {
19
20
System.out.println(e.getMessage());
}
21
22
return s;
}
23
24
25
26
27
Transaction tx = s.beginTransaction();
28
29
30
System.out.println("*********************\n");
31
System.out.println("Enter ID to Change");
32
Long id = Long.parseLong(ds.readLine());
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
62
63
System.out.println(e.getMessage());
64
System.out.println("error");
65
66
67
68
69
Student.java:
01
package com.candidjava;
02
03
import java.io.*;
04
05
06
07
08
09
10
11
12
13
14
15
16
17
return id;
18
19
20
21
return stuid;
22
23
24
25
26
return sturegno;
}
27
28
29
return stuname;
30
31
32
33
34
return stumark1;
}
35
36
37
38
39
40
41
return degree;
42
43
44
45
46
return mobileno;
}
47
48
49
return mailid;
50
51
52
53
id = string;
54
55
56
57
58
stuid = string;
}
59
60
61
62
63
sturegno = string;
}
64
65
66
stuname = string;
}
67
68
69
70
stumark1 = string;
}
71
72
73
74
stumark2 = string;
}
75
76
77
78
degree = string;
}
79
80
81
82
mobileno = string;
}
83
84
85
86
mailid = string;
}
87
88
hibernate.cfg.xml:
01
02
03
04 <hibernate-configuration>
05
06
<session-factory name="studentFactory">
<property name="connection.driver_class">
07
oracle.jdbc.OracleDriver
08
</property>
09
10
<property name="connection.url">
jdbc:oracle:thin:@localhost:1521:XE
11
</property>
12
<property name="connection.username">
13
14
15
system
</property>
<property name="connection.password">
16
system
17
</property>
18
<property name="connection.pool_size">5</property>
19
20
<property name="dialect">
21
org.hibernate.dialect.OracleDialect
22
</property>
23
24
<property name="show_sql">false</property>
25
<property name="hbm2ddl.auto">update</property>
26
27
</session-factory>
28
</hibernate-configuration>
Student.hbm.xml:
01
<?xml version="1.0"?>
02
03
04
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
05 <hibernate-mapping>
06
07
08
09
</id>
10
11
12
13
14
15
16
17
18
19
20
</class>
</hibernate-mapping>
web.xml:
01
02
03
xmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/200
1/XMLSchema-instance"
04
xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://java.sun.com/
xml/ns/j2ee/web-app_2_4.xsd">
05
<display-name>SessionObjectSaveOrUpdate</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>
OUTPUT:
Project Structure:
JAR Files:
Student.java:
01
package com.candidjava;
02
03
import java.io.*;
04
05
06
07
08
09
10
11
12
13
14
15
16
17
return id;
18
19
20
21
return stuid;
22
23
24
25
26
return sturegno;
}
27
28
29
return stuname;
30
31
32
33
34
return stumark1;
}
35
36
37
38
return stumark2;
}
39
40
41
return degree;
42
43
44
45
46
return mobileno;
}
47
48
49
return mailid;
50
51
52
53
id = string;
54
55
56
57
58
stuid = string;
}
59
60
61
62
sturegno = string;
}
63
64
65
66
stuname = string;
}
67
68
69
70
stumark1 = string;
}
71
72
73
74
stumark2 = string;
}
75
76
77
78
degree = string;
}
79
80
81
82
mobileno = string;
}
83
84
85
86
mailid = string;
}
87
88
UpdateStudent.java:
01
package com.candidjava;
02
03
04
05
06
07
ctrl.addNewStudent(args);
08
} catch (Exception e) {
09
10
System.out.println(e.getMessage());
}
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
08
09
10
11
12
13
Session s = null;
14
try {
15
16
17
18
19
20
21
22
return s;
}
23
24
25
26
27
Transaction tx = s.beginTransaction();
28
29
30
System.out.println("*********************\n");
31
System.out.println("Enter ID to Update");
32
Long id = Long.parseLong(ds.readLine());
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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.update(stu);
60
tx.commit();
61
62
63
System.out.println(e.getMessage());
64
System.out.println("error");
65
66
67
68
69
hibernate.cfg.xml:
01
02
03
04 <hibernate-configuration>
05
06
<session-factory name="studentFactory">
<property name="connection.driver_class">
07
oracle.jdbc.OracleDriver
08
</property>
09
10
<property name="connection.url">
jdbc:oracle:thin:@localhost:1521:XE
11
</property>
12
<property name="connection.username">
13
14
15
system
</property>
<property name="connection.password">
16
system
17
</property>
18
<property name="connection.pool_size">5</property>
19
20
<property name="dialect">
21
org.hibernate.dialect.OracleDialect
22
</property>
23
24
<property name="show_sql">false</property>
25
<property name="hbm2ddl.auto">update</property>
26
27
</session-factory>
28
</hibernate-configuration>
Student.hbm.xml:
01
<?xml version="1.0"?>
02
03
04
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
05 <hibernate-mapping>
06
07
08
09
</id>
10
11
12
13
14
15
16
17
18
19
20
</class>
</hibernate-mapping>
web.xml:
01 <?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns=
"http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchem
0
a2
instance"xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://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>
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:
Project Structure:
JAR Files:
LoadStudent.java:
01
package com.candidjava;
02
03
04
05
06
07
ctrl.addNewStudent(args);
08
} catch (Exception e) {
09
10
System.out.println(e.getMessage());
}
11
12
}
}
LoadStudentControl.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
09
10
11
12
13
Session s = null;
14
try {
15
16
"com\\xml\\hibernate.cfg.xml").buildSessionFactory();
17
18
s = sessionFactory.openSession();
} catch (HibernateException e) {
19
20
System.out.println(e.getMessage());
}
21
22
return s;
}
23
24
25
26
27
Transaction tx = s.beginTransaction();
28
29
30
31
32
33
int id = Integer.parseInt(d.readLine());
34
35
36
37
38
get.setDegree(newDegree);
39
tx.commit();
40
41
} catch (HibernateException e) {
42
System.out.println(e.getMessage());
43
44
System.out.println("error");
}
45
46
47
48
Student.java:
01
package com.candidjava;
02
03
import java.io.*;
04
05
06
07
08
09
10
11
12
13
14
15
16
17
return id;
18
19
20
21
return stuid;
22
23
24
25
26
return sturegno;
}
27
28
29
return stuname;
30
31
32
33
34
35
36
37
38
return stumark2;
}
39
40
41
return degree;
42
43
44
45
46
return mobileno;
}
47
48
49
return mailid;
50
51
52
53
id = string;
54
55
56
57
58
59
stuid = string;
}
60
61
62
sturegno = string;
}
63
64
65
66
stuname = string;
}
67
68
69
70
stumark1 = string;
}
71
72
73
74
stumark2 = string;
}
75
76
77
78
degree = string;
}
79
80
81
82
mobileno = string;
}
83
84
85
86
mailid = string;
}
87
88
hibernate.cfg.xml:
01
02
03
04 <hibernate-configuration>
05
06
<session-factory name="studentFactory">
<property name="connection.driver_class">
07
oracle.jdbc.OracleDriver
08
</property>
09
10
<property name="connection.url">
jdbc:oracle:thin:@localhost:1521:XE
11
</property>
12
<property name="connection.username">
13
14
15
system
</property>
<property name="connection.password">
16
system
17
</property>
18
<property name="connection.pool_size">5</property>
19
20
<property name="dialect">
21
org.hibernate.dialect.OracleDialect
22
</property>
23
24
<property name="show_sql">false</property>
25
<property name="hbm2ddl.auto">update</property>
26
27
</session-factory>
28
</hibernate-configuration>
Student.hbm.xml:
01
<?xml version="1.0"?>
02
03
04
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
05 <hibernate-mapping>
06
07
08
09
</id>
10
11
12
13
14
15
16
17
18
19
20
</class>
</hibernate-mapping>
web.xml:
01 <?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns=
"http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchem
0
a2
instance"xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://java.sun
.com/xml/ns/j2ee/web-app_2_4.xsd">
03
<display-name>SessionObjectLoad</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>
OUTPUT:
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 doesnt exist it provides null
Project Structure:
JAR Files:
GetStudent.java:
01
package com.candidjava;
02
03
04
05
06
07
ctrl.addNewStudent(args);
08
} catch (Exception e) {
09
10
System.out.println(e.getMessage());
}
11
12
}
}
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
09
10
11
12
13
Session s = null;
14
try {
15
16
"com\\xml\\hibernate.cfg.xml").buildSessionFactory();
17
18
s = sessionFactory.openSession();
} catch (HibernateException e) {
19
20
System.out.println(e.getMessage());
}
21
22
return s;
}
23
24
25
26
27
Transaction tx = s.beginTransaction();
28
29
30
31
Long id = Long.parseLong(d.readLine());
32
33
34
35
36
37
get.setDegree(degree);
38
tx.commit();
39
40
41
System.out.println(e.getMessage());
42
System.out.println("error");
43
44
45
46
47
Student.java:
01
package com.candidjava;
02
03
import java.io.*;
04
05
06
07
08
09
10
11
12
13
14
15
16
17
return id;
18
19
20
21
return stuid;
22
23
24
25
26
return sturegno;
}
27
28
29
return stuname;
30
31
32
33
34
return stumark1;
}
35
36
37
38
return stumark2;
}
39
40
41
return degree;
42
43
44
45
46
return mobileno;
}
47
48
49
return mailid;
50
51
52
53
id = string;
54
55
56
57
58
stuid = string;
}
59
60
61
62
sturegno = string;
}
63
64
65
66
stuname = string;
}
67
68
69
70
stumark1 = string;
}
71
72
73
74
stumark2 = string;
}
75
76
77
78
degree = string;
}
79
80
81
82
mobileno = string;
}
83
84
85
86
mailid = string;
}
87
88
hibernate.cfg.xml:
01
02
03
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd ">
04 <hibernate-configuration>
05
06
<session-factory name="studentFactory">
<property name="connection.driver_class">
07
oracle.jdbc.OracleDriver
08
</property>
09
10
<property name="connection.url">
jdbc:oracle:thin:@localhost:1521:XE
11
</property>
12
<property name="connection.username">
13
14
15
system
</property>
<property name="connection.password">
16
system
17
</property>
18
<property name="connection.pool_size">5</property>
19
20
<property name="dialect">
21
org.hibernate.dialect.OracleDialect
22
</property>
23
24
<property name="show_sql">false</property>
25
<property name="hbm2ddl.auto">update</property>
26
27
</session-factory>
28
</hibernate-configuration>
Student.hbm.xml:
01
<?xml version="1.0"?>
02
03
04
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">
05 <hibernate-mapping>
06
07
08
09
</id>
10
11
12
13
14
15
16
17
18
19
20
</class>
</hibernate-mapping>
web.xml:
01 <?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns=
"http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchem
0
a2
instance"xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://java.sun.co
m/xml/ns/j2ee/web-app_2_4.xsd">
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>
OUTPUT:
Project Structure:
JAR Files:
DeleteStudent.java:
01
package com.candidjava;
02
03
import java.io.*;
04
05 public class DeleteStudent {
06
07
08
09
10
int i = Integer.parseInt(ds.readLine());
11
try {
12
13
delete.DeleteUser(i);
14
} catch (Exception e) {
15
16
System.out.println(e.getMessage());
}
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
10
11
12
13
Session s = null;
14
try {
15
16
"com\\xmlFiles\\hibernate.cfg.xml").buildSessionFactory
();
17
18
s = sessionFactory.openSession();
} catch (HibernateException e) {
19
20
System.out.println(e.getMessage());
}
21
22
return s;
}
23
24
25
try {
26
27
System.out.println(id);
28
Session s = getSession();
29
30
Transaction tx = s.beginTransaction();
Student u = (Student) s.load(Student.class, new Long(id));//
difference between get() and load()
31
s.delete(u);
32
tx.commit();
33
34
} catch (HibernateException e) {
35
System.out.println(e.getMessage());
36
37
38
39
Student.java:
01
package com.candidjava;
02
import java.io.*;
03
04
05
06
07
08
09
10
11
12
13
14
15
16
return id;
17
18
19
20
return stuid;
21
22
23
return sturegno;
24
25
}
public String getStuname() {
26
return stuname;
27
28
29
30
31
return stumark1;
}
public String getStumark2() {
32
return stumark2;
33
34
35
36
return degree;
37
38
39
40
return mobileno;
41
42
43
return mailid;
44
45
46
id = string;
47
48
49
50
stuid = string;
51
52
53
sturegno = string;
54
55
}
public void setStuname(String string) {
56
stuname = string;
57
58
59
60
stumark1 = string;
}
61
62
stumark2 = string;
63
64
65
66
degree = string;
}
67
68
69
70
71
72
mobileno = string;
}
public void setMailid(String string) {
mailid = string;
73
74
75
hibernate.cfg.xml:
01
02
03
04 <hibernate-configuration>
05
06
<session-factory name="studentFactory">
<property name="connection.driver_class">
07
oracle.jdbc.OracleDriver
08
</property>
09
10
<property name="connection.url">
jdbc:oracle:thin:@localhost:1521:XE
11
</property>
12
<property name="connection.username">
13
14
15
system
</property>
<property name="connection.password">
16
system
17
</property>
18
<property name="connection.pool_size">5</property>
19
20
<property name="dialect">
21
org.hibernate.dialect.OracleDialect
22
</property>
23
24
<property name="show_sql">false</property>
25
<property name="hbm2ddl.auto">update</property>
26
27
</session-factory>
28
</hibernate-configuration>
Student.hbm.xml:
01
<?xml version="1.0"?>
02
03
04
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
05 <hibernate-mapping>
06
07
08
09
</id>
10
11
12
13
14
15
16
17
18
19
20
</class>
</hibernate-mapping>
web.xml:
01
02
03
xmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/200
1/XMLSchema-instance"
04
xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://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
14
</welcome-file-list>
</web-app>
OUTPUT:
Here First we will get input from front view html or Jsp page.
Then In controller page input will save the database using Hibernate application.
Project Structure:
JarFiles:
AddStudentControl.java:
Here we will use sessionFactory for create connetion Oracle database.
(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.
01
package com.candidjava;
02
03
import java.sql.*;
04
import java.io.*;
05
import org.hibernate.*;
06
import org.hibernate.cfg.*;
07
08
09
10
11
12
13
Session s = null;
14
try {
15
16
"com\\xml\\hibernate.cfg.xml").buildSessionFactory();
17
18
s = sessionFactory.openSession();
} catch (HibernateException e) {
19
20
System.out.println(e.getMessage());
}
21
22
return s;
}
23
24
25
26
27
Session s = getSession();
28
Transaction tx = s.beginTransaction();
29
30
31
System.out.println("Student name");
32
33
34
35
36
stu.setStuname(stuname);
37
stu.setStudegree(studegree);
38
stu.setStumobileno(stumobileno);
39
40
s.save(stu);
41
42
tx.commit();
43
44
45
} catch (HibernateException e) {
46
System.out.println("error1" + e.getMessage());
47
System.out.println("error1");
48
49
50
51
52
MyController.java:
MyModel m = (MyModel)fm;//Here model class object will store in the variable name m.
AddStudentControl ctrl = new AddStudentControl();//Here we will create object for AddStudentControl 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
11
12
13
14
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);
21
System.out.println(s2);
22
23
24
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
01
package com.candidjava;
02
03
import org.apache.struts.action.ActionForm;
04
05
06
07
08
09
10
11
12
name = n;
}
13
14
15
return name;
16
17
18
19
degree = n;
20
21
22
23
return degree;
24
25
26
27
mobileno = n;
28
29
30
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.
01
package com.candidjava;
02
03
import java.io.*;
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
return studegree;
21
22
23
24
25
26
27
28
id = string;
29
30
31
32
stuname = string;
33
34
35
36
studegree = string;
37
38
39
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>
<servlet-mapping><servlet-name>action</servlet-name>
01
02 <web-app>
03
04
<servlet>
05
06
07
08
09
10
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servletclass>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
11
12
<init-param>
13
<param-name>debug</param-name>
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
38
39
40
<security-constraint>
<web-resource-collection>
<web-resource-name>My secure resources</web-resource-name>
<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
53
</login-config>
54
<security-role>
55
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 />
</form-beans>//Beans tag will use for model class.Here name=model which is same as the controller
name=model.
<forward name=success path=/Success.jsp />//Forward tag will use for view page.It will forward input to
the view page
01
02
03 <!DOCTYPE struts-config PUBLIC
04
05
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
06 <struts-config>
07
08
<form-beans>
<form-bean type="com.candidjava.MyModel" name="model" />
09
</form-beans>
10
<action-mappings>
11
12
13
14
15
16
17
18
</action>
</action-mappings>
</struts-config>
hibernate.cfg.xml:
01
02
03
04 <hibernate-configuration>
05
06
<session-factory name="studentFactory">
<property name="connection.driver_class">
07
oracle.jdbc.OracleDriver
08
</property>
09
10
<property name="connection.url">
jdbc:oracle:thin:@localhost:1521:XE
11
</property>
12
<property name="connection.username">
13
14
15
system
</property>
<property name="connection.password">
16
system
17
</property>
18
<property name="connection.pool_size">5</property>
19
20
<property name="dialect">
21
org.hibernate.dialect.OracleDialect
22
</property>
23
24
<property name="show_sql">false</property>
25
<property name="hbm2ddl.auto">update</property>
26
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).
01
<?xml version="1.0"?>
02
03
04
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
05 <hibernate-mapping>
06
07
08
09
</id>
10
11
12
13
14
</class>
15
</hibernate-mapping>
OUTPUT: