How to Connect SQL System by using Hibernate Framework in Composition Environment 7.2 using Webdynpro JAVA
Hello Friends,
Agenda:-
How to Create Webdynpro JAVA application in Composition Environment 7.2 using Hibernate Framework
How to Connect SQL System using Hibernate Framework in Composition Environment 7.2.
Prerequisites
You need the following:
SAP NetWeaver Developer Studio 7.2
Library files (you can download it from http://www.hibernate.org)
Overview if Hibernate
Hibernate is an Object-Relational Mapping(ORM) solution for JAVA and it raised as an open source persistent framework created by Gavin King in 2001. It is a powerful, high performance Object-Relational Persistence and Query service for any Java Application.
Hibernate maps Java classes to database tables and from Java data types to SQL data types and relieve the developer from 95% of common data persistence related programming tasks.
Hibernate sits between traditional Java objects and database server to handle all the work in persisting those objects based on the appropriate O/R mechanisms and patterns.
Hibernate Advantages:
- Hibernate takes care of mapping Java classes to database tables using XML files and without writing any line of code.
- Provides simple APIs for storing and retrieving Java objects directly to and from the database.
- If there is change in Database or in any table then the only need to change XML file properties.
- Abstract away the unfamiliar SQL types and provide us to work around familiar Java Objects.
- Hibernate does not require an application server to operate.
- Manipulates Complex associations of objects of your database.
- Minimize database access with smart fetching strategies.
- Provides Simple querying of data.
Supported Databases:
Hibernate supports almost all the major RDBMS. Following is list of few of the database engines supported by Hibernate.
- HSQL Database Engine
- DB2/NT
- MySQL
- PostgreSQL
- FrontBase
- Oracle
- Microsoft SQL Server Database
- Sybase SQL Server
- Informix Dynamic Server
Supported Technologies:
Hibernate supports a variety of other technologies, including the following:
- XDoclet Spring
- J2EE
- Eclipse plug-ins
- Maven
Hibernate Architecture
How to Create Webdynpro JAVA application in Composition Environment 7.2 using Hibernate Framework
1. Create External Library DC, Add corresponding Jar files into “Libraries” folder
Now Create public part and add all jar files to the Public Part.
Now click on “Publish as Archive”. Its open this window. Now Click on “New” Button
And create the “Public Part “for COMPILATION and ASSEMBLY.
For ASSEMBLY.Click on Finish.
Now Select “Public Part”.
Click on Next Button
Libraries DC will be completed. Now Build the DC. After build the DC, we will get this below Structure.
2 . Create Web Application.
Select “Web Module” and Click on Next Here enter “Name” and Click on Finish.
2.1 Create “Login.Html”
Go to “File”—- Click on “Others”…..Now Create on “Login.Html” Page under this DC.
Click on “Next” and Enter the “File Name”
Click on Next.
Finish.
Open “Login.html” write below code
<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd“>
<html>
<head>
<meta http-equiv=“Content-Type” content=“text/html; charset=UTF-8”>
<title>Hibernate Simple Application</title>
</head>
<body>
<H> Example Hibernate</H>
<FORM method=“POST” action=“hibservlet”>
<table>
<tr><td>message text:</td><td><input type=text name=“text”></td></tr>
<tr><td>2nd message text:</td><td><input type=text name=“text_sec”></td></tr>
<tr><td><input type=submit name=“submit” value=“insert”></td></tr>
</table>
</FORM>
</body>
</html>
2.2 Create “hibernate.cfg.xml”
Hibernate with MySQL Database:
MySQL is one of the most popular open-source database systems available today. Let us create hibernate.cfg.xml configuration file and place it in the root of your application’s classpath. You would have to make sure that you have testdb database available in your MySQL database and you have a user test available to access the database.
We will create one more “classes” folder Under “WEB_INF” Folder.
Right click on “WEB-INF”—-> New —->Click on “Folder”. Then enter folder Name as “class” and click on Finish. Now folder is created. Please look at below screen shoot
Now create “hibernate.cfg.xml” under “class” folder.Right Click On “classes” and create xml file
Here select XML and Click on Next. Here Enter the File name as “hibernate.cg.xml”
And Click on Next Button. And Select the “Create XML file from an XML Template”
Click on Next and Click on Finish.
Write below code.
Check this XML code.
<?xml version=‘1.0’ encoding=‘utf-8’?>
<!DOCTYPE hibernate-configuration PUBLIC “-//Hibernate/Hibernate Configuration DTD 3.0//EN” “hibernate-configuration-3.0.dtd”>
<hibernate-configuration>
<session-factory>
<property name=“hibernate.connection.driver_class”>com.mysql.jdbc.Driver</property>
<property name=“hibernate.connection.url”>jdbc:mysql://localhost:3306/test</property>
<property name=“hibernate.connection.username”>root</property>
<property name=“hibernate.connection.password”>test1234</property>
<property name=“hibernate.connection.pool_size”>10</property>
<property name=“show_sql”>false</property>
<property name=“dialect”>org.hibernate.dialect.MySQLDialect</property>
<!– Mapping files –>
<mapping resource=“com/exam/hibernate/BeanClass.hbm.xml”/>
</session-factory>
</hibernate-configuration>
2.3 Create Package(com.exam.hibername) under Web Application
After that, under the package we will create servlet, javabean and javabean.htb.xml.
Creating Servlet Java class file
Right Click on Package——— Go to others
click on Next Button.
Click on Next and Finish.
Open Controller.JAVA Servlet, We ill write code in Sevlet depending upon our requirement
package com.exam.hibernate;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
/**
* Servlet implementation class for Servlet: Controller
*
*/
public class Controller extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
static final long serialVersionUID = 1L;
/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#HttpServlet()
*/
public Controller() {
super();
}
/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
Configuration cfg = new Configuration();
cfg.configure();
//cfg.addClass(sample.Message.class);
SessionFactory sf = cfg.buildSessionFactory();
String text = request.getParameter(“text”);
String text_sec = request.getParameter(“text_sec”);
Session session = (Session)sf.openSession();
Transaction tx = session.beginTransaction();
BeanClass message = new BeanClass(text);
session.save(message);
if (text_sec!=null) {
BeanClass message_sec = new BeanClass(text_sec);
message.setNextMessage(message_sec);
}
tx.commit();
session.close();
response.setContentType(“text/html”);
PrintWriter out = response.getWriter();
out.println(“<HTML><HEAD><TITLE>Hibernate</TITLE></HEAD>”);
out.println(“<BODY bgcolor=#E6E6FA><H1>Hibernate Example</H1>”);
out.println( message.getText() );
out.println(“</BODY></HTML>”);
}
/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
Now create “JavaBeans” class. Right click on Package—click on “Class”
Here enter Name as BeanClass and Click on Finish Button.
Open BeanClass .JAVA , We ill write code in BeanClass depending upon our requirement.
package com.exam.hibernate;
public class BeanClass
{
private Long id;
private String text;
private BeanClass nextMessage;
private BeanClass() {}
public BeanClass(String text)
{
this.text = text;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public BeanClass getNextMessage() {
return nextMessage;
}
public void setNextMessage(BeanClass nextMessage) {
this.nextMessage = nextMessage;
}
}
Now create javabean.xml file under same package
Click on Next. Here select Parent Folder path under folder we can create XML file
Click on next and finish.
<?xml version=“1.0” encoding=“UTF-8”?>
<!DOCTYPE hibernate-mapping PUBLIC
“-//Hibernate/Hibernate Mapping DTD//EN”
“hibernate-mapping-3.0.dtd” >
<hibernate-mapping>
<class name=“com.exam.hibernate.BeanClass” table=“TMP_MESSAGES”>
<id name=“id” column=“MESSAGE_ID”>
<generator class=“increment”/>
</id>
<property name=“text” column=“MESSAGE_TEXT”/>
<many-to-one name=“nextMessage” cascade=“all” column=“NEXT_MESSAGE_ID”/>
</class>
</hibernate-mapping>
Now adds Liberary DC Public Part to Web Application. So that time we will able to call the Hibernate Jar file to Web Application DC.
Right Click on DC— Development Component—-Show In—- Click on Component Properties.—>Click on Dependencies tab. .Add Liberary DC Liberary DC Public Part to Web Application.
3 Finally Create Enterprise Application.
Click on finish.
Now adds WebApplication DC Public Part to Enterprise Application DC. So that time we can able to call the Hibernate Jar file and WebApplication DC to Enterprise Application DC.
Right Click on DC— Development Component—-Show In—- Click on Component Properties.
Click on Finish Button.
Finally Build and Deploy the Enterprise Application.
Hope this is helfull
Best Regards
Vijay Kalluri
Nice one and useful bolg.
Its very nice and useful blog
Excellent Blog