This document explains in details how to setup a derby database connection using Hibernate and caching. This document assumes you are using Red5 0.8 and the latest Hibernate Release 3.3.1.
This update requires very latest Red5 svn trunk
The demo application is built using the Red5Plugin, simply import the project into eclipse.
http://www.hibernate.org/ DAO takes a similar approach to the Spring DAO JDBC setup but adds first and second level caching and example is described here docs:Red5 and Spring JDBC Hibernate.
Required Libraries
* aspectjweaver.jar - Found in Spring with dependancies distribution.
* c3p0-0.9.1.2.jar
* dom4j-1.6.1.jar - Hibernate dependancy, found in distribution
* javassist-3.4.GA.jar - Hibernate dependancy, found in distribution
* antlr-3.1.1.jar - Hibernate dependancy, found in distribution. Already included with Red5.
* commons-collections-3.2.1.jar - Hibernate dependancy, found in distribution. Already included with Red5.
* jta-1.1.jar - Hibernate dependancy, found in distribution. Already included with Red5.
* slf4j-api-1.5.2.jar - Hibernate dependancy, found in distribution. Already included with Red5.
* ehcache-1.6.0-beta1.jar - The latest ehcache which removes backport concurrent dependancy.
* jsr107cache-1.0.jar - EHCache 1.6 dependancy.
* derby-10.4.2.0.jar
* hibernate3.jar - Get the latest which is 3.3.1.
* spring-aop-2.5.5.jar - Already included with Red5.
* spring-orm-2.5.5.jar - Already included with Red5.
* spring-jdbc-2.5.5.jar - Found in spring distribution.
* spring-tx-2.5.5.jar - Found in spring distribution.
There is still an issue getting aspectjweaver.jar to resolve in the classpath which is needed for the AOP style of the transaction setup therefore it is required to be included in the classpath to the Red5 startup script like so
The following configurations explain how to configure an existing Red5 application configuration files. To learn about creating the application first visit Create New Applications In Red5?.
red5-hibernate.xml
1. Create a config file called red5-hibernate.xml.
2. Add the following code
public class UsersDaoImpl extends HibernateDaoSupport implements IUsersDao {
public Users getUser(Users users) {
Example example = Example.create(users);
return (Users)this.getSessionFactory().getCurrentSession().createCriteria(Users.class)
.add(example).list().iterator().next();
}
public List getUsers() {
return this.getSessionFactory().getCurrentSession()
.createQuery("FROM Users ORDER BY username")
.list();
}
public void deleteUser(Users users)
{
this.getSessionFactory().getCurrentSession().delete(getUser(users));
}
public void addUser(Users users)
{
this.getSessionFactory().getCurrentSession().save(users);
}
public class UsersServiceImpl implements IUsersDao {
private IUsersDao usersDao;
public void setUsersDao(IUsersDao users) {
this.usersDao = users;
}
public List getUsers() {
return this.usersDao.getUsers();
}
public Users getUser(Users users) {
return this.usersDao.getUser(users);
}
public Users getStreamPoint(Users users) {
return this.usersDao.getUser(users);
}
public void deleteUser(Users users)
{
this.usersDao.deleteUser(users);
}
public void addUser(Users users)
{
this.usersDao.addUser(users);
}
}
Usage
for (Iterator iter = this.usersService.getUsers().iterator(); iter.hasNext();) {
Users element = (Users) iter.next();
log.debug("User ID: {}, User Name: {}", new Object[]{element.getId(), element.getUserName()});
}
Users users = new Users();
users.setUserName("newuser");
this.usersService.addUser(users);
this.usersService.deleteUser(users);