Monday, April 18, 2011

Hibernate Notes

Hibernate Notes
1. Develop a system that displays, allows add, delete, and modifications
New Project
2. Web Application,
3. select Hibernate and Java Faces frameworks,
4. choose correct database
hibernate configuration
1. open hibernate.cfg.xml
2. add,
a. driver_class,
b. url, jdbc:mysql://localhost:3306/databaseName
c. username,
d. password if not there
3. Optional Properties
a. Configuration properties
i. Show_sql – true
4. Miscellaneous properties
a. Current_session_context_class – thread
Package
Create filviewer package in Soruce Packages
HibernateUtil
i. Use File->Hibernate->New HIbernateUtil (or somewhere in there)
ii. Put in Source Packages in package filmviewer
Reverse Engineering
i. Go to default package
ii. Open up New file->Hibernate->Hibernate reverse engineering wizard
iii. Use src/java
iv. Add tables from database
Mapping POJO’s
i. Use File->Hibernate->Hibernate Mapping Files and POJO’s from Database
ii. Use
a. JDK5.1 language,
b. hibernate.cfg.xml ,
c. hibernate.reveng.xml,
d. domain Code,
e. Hibernate XML Mappings
Session Bean
i. Use File->JavaServer Faces->JSF Managed Bean
ii. Use either Request Scoped or Session Scoped as appropriate.
iii. Edit:
a. Import Org.hibernate.Session, Query, Transaction, java.util.*
b. Cache HibernateUtil.getSessionFactory().getCurrentSession() in an instance variable
c. Helper Methods
private Session getSession() {
return HibernateUtil.getSessionFactory().getCurrentSession();
}

private Transaction beginTransaction() {
return HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction();
}

private Transaction getTransaction() {
return HibernateUtil.getSessionFactory().getCurrentSession().getTransaction();
}
View
public List view() {
try {
beginTransaction();
Query query getSession().createQuery("from Film");
List< POJO > result = (List< POJO >) query.list();
getTransaction().commit();
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
Add
public void add(String title, String description) {
try {
beginTransaction();
POJO film = new POJO ();
film.setTitle(title);
film.setDescription(description);
getSession().save(film);
getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
}
}
Delete
public void delete(int id) {
try {
beginTransaction();
Film film = (Film)getSession().get(Film.class, id);
getSession().delete(film);
getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
}
}
Update

public void update(int id, String title, String description) {
try {
beginTransaction();
Film film = (Film)m_session.get(Film.class, id);
film.setTitle(title);
film.setDescription(description);
getSession().update(film);
getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
}
}


Running Methods/Accessing View
a. Edit Web Pages/index.xhtml
b. Add xmlns:f=http://java.sun.com/jsf/core
c.














Debugging
1. Helpful to run server from console using glassfish\bin\startserv so you can see stacktraces in terminal window.
Database
Example of how to create a table with autoincremented primary key
CREATE TABLE film (
filmId INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) ,
description VARCHAR(1000)
);

No comments:

Post a Comment