something about programming_ eclipse + glassfish + hibernate + primefaces
TRANSCRIPT
24/02/13 Something about programming: Eclipse + Glassfish + Hibernate + Primefaces
somethingaboutprogramming.blogspot.com/2012/05/eclipse-glassfish.html 1/15
Java - Eclipse
Something about programming
Tuesday, May 8, 2012
1. Download Glassfish server and install it: http://glassfish.java.net/
2. Add this server to Eclipse:
- Help > Eclipse Marketplace > find and install "GlassFish Java EE
Application Server Plugin for Eclipse"
- Restart Eclipse
- To add GlassFish server: File > New > Server
Eclipse + Glassfish + Hibernate + Primefaces▼ 2012 (1)
▼ May (1)
Eclipse + Glassfish + Hibernate +Primefaces
► 2011 (4)
Blog Archive
Have YouWritten a
Book?
www.AuthorHous…
Learn how topublish your bookin 30 days. Getour Free Author
Guide.
Ahu Soydan
View my complete
profile
About Me
0PartagerPartager Plus Blog suivant» Créer un blog Connexion
24/02/13 Something about programming: Eclipse + Glassfish + Hibernate + Primefaces
somethingaboutprogramming.blogspot.com/2012/05/eclipse-glassfish.html 2/15
Join this siteJoin this sitew ith Google Friend Connect
There are no members yet.Be the first!
Already a member? Sign in
Followers
24/02/13 Something about programming: Eclipse + Glassfish + Hibernate + Primefaces
somethingaboutprogramming.blogspot.com/2012/05/eclipse-glassfish.html 3/15
3. Start the server
4. Open GlassFish admin console
5. Create new connection pool
24/02/13 Something about programming: Eclipse + Glassfish + Hibernate + Primefaces
somethingaboutprogramming.blogspot.com/2012/05/eclipse-glassfish.html 4/15
6. Copy the database driver jar in glassfish_path/glassfish/lib/ext
directory. ojdbc14.jar is for Oracle.
7. Hibernate is installed by using "Update Tool" in Glassfish:
8. Add Mojarra and JSF Facelets libraries into Eclipse
24/02/13 Something about programming: Eclipse + Glassfish + Hibernate + Primefaces
somethingaboutprogramming.blogspot.com/2012/05/eclipse-glassfish.html 5/15
- Mojarra libraries
- jsf-api.jar
- jsf-impl.jar
- JSF Facelets library
- jsf-facelets-1.1.15.B1.jar
24/02/13 Something about programming: Eclipse + Glassfish + Hibernate + Primefaces
somethingaboutprogramming.blogspot.com/2012/05/eclipse-glassfish.html 6/15
9. Create new dynamic web project in Eclipse:
24/02/13 Something about programming: Eclipse + Glassfish + Hibernate + Primefaces
somethingaboutprogramming.blogspot.com/2012/05/eclipse-glassfish.html 7/15
24/02/13 Something about programming: Eclipse + Glassfish + Hibernate + Primefaces
somethingaboutprogramming.blogspot.com/2012/05/eclipse-glassfish.html 8/15
Add all necessary libraries for hibernate + primefaces + database driver
into your_dynamic_web_project_path/WebContent/WEB-INF/lib. The libraries
I added here are: ant-1.6.2.jar, ant-antlr-1.6.2.jar, ant-junit-
1.6.2.jar, ant-launcher-1.6.2.jar, antlr-2.7.4.jar, ant-swing-
1.6.2.jar, c3p0-0.8.5.jar, cglib-full-
2.0.2.jar, cleanimports.jar, commons-collections-2.1.1.jar, commons-
logging-1.0.4.jar, concurrent-1.3.2.jar, connector.jar, dom4j-
1.5.2.jar, ehcache-1.1.jar, hibernate3.jar, jaas.jar, jacc-1_0-
fr.jar, jaxen-1.1-beta-4.jar, jdbc2_0-stdext.jar, jgroups-
2.2.7.jar, jta.jar, junit-3.8.1.jar, log4j-
1.2.9.jar, ojdbc14.jar, oscache-2.1.jar, primefaces-3.0.jar, primefaces-
3.0-sources.jar, proxool-0.8.3.jar, slf4j-api-1.6.1.jar, swarmcache-
1.0rc2.jar, versioncheck.jar, xerces-2.6.2.jar, xml-apis.jar. Some of
them can be unnecessary but my projects work with these libs without
error.
24/02/13 Something about programming: Eclipse + Glassfish + Hibernate + Primefaces
somethingaboutprogramming.blogspot.com/2012/05/eclipse-glassfish.html 9/15
10. I create a City table in my database that includes 2 columns: CityId
and CityColumn.
CREATE
TABLE CITY(CITYIDNUMBER(5) NOT NULL,CITYNAMEVARCHAR2(30 BYTE) NOT NULL
)
I insert cities in City table:
24/02/13 Something about programming: Eclipse + Glassfish + Hibernate + Primefaces
somethingaboutprogramming.blogspot.com/2012/05/eclipse-glassfish.html 10/15
Insert into CITY(CITYID, CITYNAME) Values (6, 'ANKARA');
Insert into CITY(CITYID, CITYNAME) Values (80, 'OSMANİYE');
Insert into CITY(CITYID, CITYNAME) Values (81, 'DÜZCE');
...
I want to create a primefaces table that includes CityId and CityName to
show in a web page.
I create a Beans package that includes City.java, TableBean.java and
city.hbm.xml files.
City.java
package Beans;
public class City {
private long cityId;
private String cityName;
public City() {
}
public City(long cityId, String cityName) {
this.cityId = cityId;
this.cityName = cityName;
}
public long getCityId() {
return cityId;
}
public void setCityId(long cityId) {
this.cityId = cityId;
}
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
}
TableBean.java
package Beans;
import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class TableBean {
/** Creates a new instance of Bean */
private List<City> item;
public List<City> getItem() {
return item;
}
public void setItem(List<City> item) {
this.item = item;
}
24/02/13 Something about programming: Eclipse + Glassfish + Hibernate + Primefaces
somethingaboutprogramming.blogspot.com/2012/05/eclipse-glassfish.html 11/15
public TableBean() {
item=new ArrayList<City>();
Session session = null;
try {
// This step will read hibernate.cfg.xml and prepare hibernate
for use
SessionFactory sessionFactory = new
Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();
String str = "from City";
Query query = (Query) session.createQuery(str);
List results = query.list();
Iterator itr = results.iterator();
while(itr.hasNext()){
City cit = (City) itr.next();
item.add(new City(cit.getCityId(),cit.getCityName()));
}
}
catch(Exception e){
System.out.println(e.getMessage());
}
finally{
// Actual contact insertion will happen at this step
session.flush();
session.close();
}
}
}
city.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Beans.City" table="CITY">
<id name="cityId" type="long" column="CITYID">
<generator class="assigned"/>
</id>
<property name="cityName">
<column name="CITYNAME"/>
</property>
</class>
</hibernate-mapping>
Then create hibernate.cfg.xml in src file but not in Beans package:
hibernate.cfg.xml
24/02/13 Something about programming: Eclipse + Glassfish + Hibernate + Primefaces
somethingaboutprogramming.blogspot.com/2012/05/eclipse-glassfish.html 12/15
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate
Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-
configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property
name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
<property
name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</prope
rty>
<property
name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:sid</pr
operty>
<property name="hibernate.connection.username">username</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.connection.pool_size">100</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.connection.autocommit">true</property>
<mapping resource="Beans/city.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Finally create an xhtml file in WebContent folder: Here, my xhtml file
is city.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>City Table</title>
</h:head>
<h:body>
<p:panel header="PrimeFaces ile listeleme">
<p:dataTable id="basic" value="#{tableBean.item}" var="items" >
<p:column>
<f:facet name="header">City Id</f:facet>
<h:outputText value="#{items.cityId}"/>
</p:column>
<p:column>
<f:facet name="header">City Name</f:facet>
<h:outputText value="#{items.cityName}"/>
</p:column>
</p:dataTable>
</p:panel>
</h:body>
</html>
Don't forget to add your bean definition in faces-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<faces-config
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.co
m/xml/ns/javaee/web-facesconfig_2_0.xsd"
24/02/13 Something about programming: Eclipse + Glassfish + Hibernate + Primefaces
somethingaboutprogramming.blogspot.com/2012/05/eclipse-glassfish.html 13/15
version="2.0">
<managed-bean>
<managed-bean-name>tableBean</managed-bean-name>
<managed-bean-class>Beans.TableBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
</faces-config>
Final view of my project with all files:
11. Run it!
24/02/13 Something about programming: Eclipse + Glassfish + Hibernate + Primefaces
somethingaboutprogramming.blogspot.com/2012/05/eclipse-glassfish.html 14/15
The view of my city table is:
Posted by Ahu Soydan at 4:42 PM
Labels: Eclipse, Glassfish, Hibernate, Primefaces
Recommend this on Google
Enter your comment...
Comment as: Google Account
Publish Preview
No comments:
Post a Comment
24/02/13 Something about programming: Eclipse + Glassfish + Hibernate + Primefaces
somethingaboutprogramming.blogspot.com/2012/05/eclipse-glassfish.html 15/15
Older PostHome
Subscribe to: Post Comments (Atom)
Java Persistence Tools
www.myeclipseide.com
OpenJPA, Toplink, Hibernate SupptNo Lock-in, Eclipse-Based
Ethereal template. Powered by Blogger.