Adding package structure
First we must know that maven (while we are creating the archetype) dont need a package structure because it will create that when we build the archetype. For this reason we only create the dao and dao / hibernate folder in the src / main / java directory.
Example:
Adding source files
Now we can add the source files, we will using a GenericDAO and GenericDAOHibernate implementation to the more common methods. Add the AppUserDAO and AppRoleDAO with their hibernate implementations too.
Check the imports in the header of the file, the ${groupId} expression must be there, because maven, using Velocity, will replace with your package convention name when you build this artifact.
Add the following in the domain folder:
The AppUser entity:
/**
File: AppUser.java
Created at: Jun 18, 2010
Author: Tomas de Priede
Project: fusion
Builted by: Maven Fusion Archetype 1.0.0
*/
package ${groupId}.domain;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import ${groupId}.domain.AppRole;
/**
* System user of the web application
*
* @author Tomas de Priede
* @since v1.0.0
*
*/
@Entity
public class AppUser{
/**
* Auto generated unique identifier of the user
*
* @author Tomas de Priede
* @since v1.0.0
*/
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column
private Integer id;
@Column(nullable=false)
/**
* Username of the user
*
* @author Tomas de Priede
* @since v1.0.0
*/
private String username;
@Column(nullable=false)
/**
* A string password of the user to log in the application
*
* @author Tomas de Priede
* @since v1.0.0
*/
private String password;
@Column(nullable=false)
/**
* A user mail
*
* @author Tomas de Priede
* @since v1.0.0
*/
private String mail;
@ManyToMany(
cascade=CascadeType.ALL,
fetch=FetchType.LAZY)
/**
* The users roles of the app
*
* @author Tomas de Priede
* @since v1.0.0
*/
private List roles;
/**
* Returns the auto generated unique identifier of the user
*
* @author Tomas de Priede
* @since v1.0.0
*
* @return the id
*/
public Integer getId() {
return id;
}
/**
* Sets the auto generated unique identifier of the user
*
* @author Tomas de Priede
* @since v1.0.0
*
* @param id the id to set
*/
public void setId(Integer id) {
this.id = id;
}
/**
* Returns the username of the user
*
* @author Tomas de Priede
* @since v1.0.0
*
* @return the username
*/
public String getUsername() {
return username;
}
/**
* Sets the username of the user
*
* @author Tomas de Priede
* @since v1.0.0
*
* @param username the username to set
*/
public void setUsername(String username) {
this.username = username;
}
/**
* Returns the string password of the user to log in the application
*
* @author Tomas de Priede
* @since v1.0.0
*
* @return the password
*/
public String getPassword() {
return password;
}
/**
* Sets a string password of the user to log in the application
*
* @author Tomas de Priede
* @since v1.0.0
*
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}
/**
* Returns the user mail
*
* @author Tomas de Priede
* @since v1.0.0
*
* @return the mail
*/
public String getMail() {
return mail;
}
/**
* Sets a user mail
*
* @author Tomas de Priede
* @since v1.0.0
*
* @param mail the mail to set
*/
public void setMail(String mail) {
this.mail = mail;
}
/**
* Returns the users roles of the app
*
* @author Tomas de Priede
* @since v1.0.0
*
* @return the roles
*/
public List getRoles() {
return roles;
}
/**
* Sets the users roles of the app
*
* @author Tomas de Priede
* @since v1.0.0
*
* @param roles the roles to set
*/
public void setRoles(List roles) {
this.roles = roles;
}
}
/**
File: AppRole.java
Created at: Jun 19, 2010
Author: Tomas de Priede
Project: fusion
Builted by: Maven Fusion Archetype 1.0.0
*/
package ${groupId}.domain;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
/**
* System role of the web application
*
* @author Tomas de Priede
* @since v1.0.0
*
*/
@Entity
public class AppRole{
/**
* Auto generated unique identifier of the role
*
* @author Tomas de Priede
* @since v1.0.0
*/
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column
private Integer id;
/**
* Description of the role
*
* @author Tomas de Priede
* @since v1.0.0
*/
@Column
private String roleName;
/**
* Users who has this role in the app
*
* @author Tomas de Priede
* @since v1.0.0
*/
@ManyToMany(mappedBy="roles",
cascade=CascadeType.ALL,
fetch=FetchType.LAZY)
private List users;
/**
* Returns the auto generated unique identifier of the role
*
* @author Tomas de Priede
* @since v1.0.0
*
* @return the id
*/
public Integer getId() {
return id;
}
/**
* Sets the auto generated unique identifier of the role
*
* @author Tomas de Priede
* @since v1.0.0
*
* @param id the id to set
*/
public void setId(Integer id) {
this.id = id;
}
/**
* Returns the description of the role
*
* @author Tomas de Priede
* @since v1.0.0
*
* @return the roleName
*/
public String getRoleName() {
return roleName;
}
/**
* Sets the description of the role
*
* @author Tomas de Priede
* @since v1.0.0
*
* @param roleName the roleName to set
*/
public void setRoleName(String roleName) {
this.roleName = roleName;
}
/**
* Returns the users who has this role in the app
*
* @author Tomas de Priede
* @since v1.0.0
*
* @return the users
*/
public List getUsers() {
return users;
}
/**
* Sets the users who has this role in the app
*
* @author Tomas de Priede
* @since v1.0.0
*
* @param users the users to set
*/
public void setUsers(List users) {
this.users = users;
}
}
Add the following in the dao folder:
The GenericDAO interface:
/**
File: GenericDAO.java
Created at: Jun 19, 2010
Author: Tomas de Priede
Project: fusion
Builted by: Maven Fusion Archetype 1.0.0
*/
package ${groupId}.dao;
import java.io.Serializable;
/**
* Generic interface to all daos in the app.
*
* @author Tomas de Priede
* @since v1.0.0
*
*/
public interface GenericDAO {
/**
* Returns the object with the serializable id looked
*
* @param id el id del objeto a buscar
* @return el objeto del tipo genérico
* @author Tomas de Priede
* @since v1.0.0
*/
public T findById(Class clazz,Serializable id);
/**
* Stores an object in the hibernate session
* @author Tomas de Priede
* @since v1.0.0
*
* @param t the object to store
* @return the objects id
*/
public Serializable save(T t);
/**
* Merge an object in the hibernate session
* @author Tomas de Priede
* @since v1.0.0
*
* @param t the object
*/
public void merge(T t);
/**
* Updates an object in the database
*
* @author Tomas de Priede
* @since v1.0.0
*
* @param t the object
*/
public void update(T t);
/**
* Try to save an object in the database, if fails
* try to update its.
*
* @author Tomas de Priede
* @since v1.0.0
*
* @param t the object
*/
public void saveOrUpdate(T t);
/**
*
* Deletes an object from the database
*
* @author Tomas de Priede
* @since v1.0.0
*
* @param t the object
*/
public void delete(T t);
}
The AppUserDAO interface:
/**
File: AppUserDAO.java
Created at: Jun 19, 2010
Author: Tomas de Priede
Project: fusion
Builted by: Maven Fusion Archetype 1.0.0
*/
package ${groupId}.dao;
import ${groupId}.domain.AppUser;
import ${groupId}.domain.Page;
/**
* DAO interface for AppUser
*
* @author Tomas de Priede
* @since v1.0.0
*
*/
public interface AppUserDAO extends GenericDAO{
/**
* Returns the AppUser using his id
*
* @author Tomas de Priede
* @since v1.0.0
*
* @param id the id of the appuser
* @return the appuser
*/
public AppUser findById(Integer id);
/**
* Returns the AppUser looked by the username
*
* @author Tomas de Priede
* @since v1.0.0
*
* @param username
* @return the AppUser looked
*/
public AppUser findByUsername(String username);
}
The AppUserDAO interface:
/**
File: AppRoleDAO.java
Created at: Jun 19, 2010
Author: Tomas de Priede
Project: fusion
Builted by: Maven Fusion Archetype 1.0.0
*/
package ${groupId}.dao;
import ${groupId}.domain.AppRole;
/**
* DAO interface for AppRole
*
* @author Tomas de Priede
* @since v1.0.0
*
*/
public interface AppRoleDAO extends GenericDAO{
/**
* Returns the AppRole using his id
*
* @author Tomas de Priede
* @since v1.0.0
*
* @param id the id of the approle
* @return the approle
*/
public AppRole findById(Integer id);
}
Add the following in the dao / hibernate folder:
The GenericDAOHibernate implementation:
/**
File: GenericDAOHibernate.java
Created at: Jun 19, 2010
Author: Tomas de Priede
Project: fusion
Builted by: Maven Fusion Archetype 1.0.0
*/
package ${groupId}.dao.hibernate;
import org.hibernate.SessionFactory;
import ${groupId}.dao.GenericDAO;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import ${groupId}.domain.Page;
import ${groupId}.domain.PageHibernate;
import ${groupId}.utils.PageUtils;
/**
* Hibernate implementation of the generic dao.
*
* @author Tomas de Priede
* @since v1.0.0
*
*/
public abstract class GenericDAOHibernate implements GenericDAO {
/**
* Hibernate's session factory
*
* @author Tomas de Priede
* @since v1.0.0
*/
protected SessionFactory sessionFactory;
/**
* Use the hibernate session.get to find the object
*
* @author Tomas de Priede
* @since v1.0.0
*
* @see ${groupId}.dao.GenericDAO#findById(java.lang.Class, java.io.Serializable)
*/
public T findById(Class clazz, Serializable id) {
Session session = sessionFactory.getCurrentSession();
T obj = (T) session.get(clazz, id);
return obj;
}
/**
* Use the hibernate session.save to store the object
* and retrieve the id generated.
*
* @author Tomas de Priede
* @since v1.0.0
*
* @see ar.com.consultoriaglobal.intranet.daos.GenericDAO#save(java.lang.Object)
*/
public Serializable save(T t) {
Session session = sessionFactory.getCurrentSession();
Serializable id = session.save(t);
return id;
}
/**
* Use hibernate session save or update to stores or update
* the object
*
* @author Tomas de Priede
* @since v1.0.0
*
* @see ${groupId}.dao.GenericDAO#saveOrUpdate(java.lang.Object)
*/
public void saveOrUpdate(T t) {
Session session = sessionFactory.getCurrentSession();
session.saveOrUpdate(t);
}
/**
* Use the hibernate session.update to update the object
*
* @author Tomas de Priede
* @since v1.0.0
*
* @see ${groupId}.dao.GenericDAO#update(java.lang.Object)
*/
public void update(T t) {
Session session = sessionFactory.getCurrentSession();
session.update(t);
}
/**
* Use the hibernate session.delete to delete the object
* @author Tomas de Priede
* @since v1.0.0
*
* @see ${groupId}.dao.GenericDAO#delete(java.lang.Object)
*/
public void delete(T t) {
Session session = sessionFactory.getCurrentSession();
session.delete(t);
}
/**
* Use the hibernate session.merge to merge the object
* with the current hibernate session.
*
* @author Tomas de Priede
* @since v1.0.0
*
* @see ${groupId}.dao.GenericDAO#merge(java.lang.Object)
*/
@Override
public void merge(T t) {
Session session = sessionFactory.getCurrentSession();
session.merge(t);
}
/**
* Simplificates the build query process.
* Return a query from the current hibernate session
*
* @author Tomas de Priede
* @since v1.0.0
*
* @param hql
* @return
*/
public Query buildHQLQuery(String hql){
Session session = getSessionFactory().getCurrentSession();
Query query = session.createQuery(hql);
return query;
}
/**
* Getter for spring lookup
*
* @return la sessionFactory de Hibernate
* @author Tomas de Priede
* @since v1.0.0
*/
public SessionFactory getSessionFactory() {
return sessionFactory;
}
/**
* Setter for Spring Injection
*
* @author Tomas de Priede
* @since v1.0.0
*
* @param sessionFactory
* the sessionFactory to set
*/
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
}
The AppUserDAOHibernate implementation:
/**
File: AppUserDAOHibernate.java
Created at: Jun 19, 2010
Author: Tomas de Priede
Project: Fusion
Builted by: Maven Fusion Archetype 1.0.0
*/
package ${groupId}.dao.hibernate;
import org.hibernate.Query;
import ${groupId}.dao.AppUserDAO;
import ${groupId}.domain.AppUser;
import ${groupId}.domain.Page;
import static ${groupId}.utils.ValidatorUtils.validateNull;
import java.util.Map;
import java.util.HashMap;
/**
* Hibernate implementation for AppUserDAO
*
* @author Tomas de Priede
* @since v1.0.0
*
*/
public class AppUserDAOHibernate extends GenericDAOHibernate
implements AppUserDAO{
/**
* Calls the findById with User.class and
* the serializable integer id
*
* @author Tomas de Priede
* @since v1.0.0
* @see ${groupId}.dao.AppUserDAO#findById(Integer)
*
* @param id the id of the AppUser looked
* @return the AppUser looked
*/
public AppUser findById(Integer id){
return this.findById(AppUser.class,id);
}
/**
* Returns the AppUser looked by the username
*
* @author Tomas de Priede
* @since v1.0.0
*
* @param username
* @return the AppUser looked
*/
public AppUser findByUsername(String username){
validateNull(username,"username");
String hql = "from AppUser a where username = :username";
Query query = this.buildHQLQuery(hql);
query.setString("username", username);
return (AppUser)query.uniqueResult();
}
}
The AppRoleDAOHibernate implementation:
/**
File: AppRoleDAOHibernate.java
Created at: Jun 19, 2010
Author: Tomas de Priede
Project: Fusion
Builted by: Maven Fusion Archetype 1.0.0
*/
package ${groupId}.dao.hibernate;
import ${groupId}.dao.AppRoleDAO;
import ${groupId}.domain.AppRole;
/**
* Hibernate implementation for AppRoleDAO
*
* @author Tomas de Priede
* @since v1.0.0
*
*/
public class AppRoleDAOHibernate extends GenericDAOHibernate
implements AppRoleDAO{
/**
* Calls the findById with Role.class and
* the serializable integer id
*
* @author Tomas de Priede
* @since v1.0.0
* @see ${groupId}.dao.AppRoleDAO#findById(Integer)
*
* @param id the id of the AppRole looked
* @return the AppRole looked
*/
public AppRole findById(Integer id){
return this.findById(AppRole.class,id);
}
}
Adding to archetype.xml
Maven will be adding all the resources in the archetype.xml so we must add all of them.
Dont worry for the package structure, maven works well.
<archetype xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype/1.0.0" xsi:schemalocation="http://maven.apache.org/plugins/maven-archetype-plugin/archetype/1.0.0 http://maven.apache.org/xsd/archetype-1.0.0.xsd">
<id>fusion</id>
<sources>
<source encoding="utf-8">src/main/java/dao/AppRoleDAO.java</source>
<source encoding="utf-8">src/main/java/dao/AppUserDAO.java</source>
<source encoding="utf-8">src/main/java/dao/GenericDAO.java</source>
<source encoding="utf-8">src/main/java/dao/hibernate/AppRoleDAOHibernate.java</source>
<source encoding="utf-8">src/main/java/dao/hibernate/AppUserDAOHibernate.java</source>
<source encoding="utf-8">src/main/java/dao/hibernate/GenericDAOHibernate.java</source>
<source encoding="utf-8">src/main/java/domain/AppRole.java</source>
<source encoding="utf-8">src/main/java/domain/AppUser.java</source>
</sources>
<testSources></testSources>
<resources></resources>
</archetype>
Installing the archetype
Now we can install the archetype:
mvn archetype:create -DarchetypeGroupId=ar.com.fusion -DarchetypeArtifactId=fusion -DarchetypeVersion=1.0.0 -DgroupId=ar.com.prueba -DartifactId=prueba -Dversion=1.0
And import the project into the eclipse to see the results
Conclusion
We learned how to add sources files to the archetype and put them into our package structure. We have seen a generic dao with some utils methods to persists/retrieve objects from the database. We mapped the domain entities with hibernate annotations.
In the part two of this post we will add some test classes to test the User and Role daos.
See you in the next post!
Tom.

No comments:
Post a Comment