EVENT DRIVEN GWT RPC USING HIBERNATE ANNOTATION
Hi Reader,
I am continuing from the previous blog GWT RPC using Hibernate Annotation using SQLServer 2008.
Depending upon the delegator we are delegating the request to the server. We are implementing all the methods from the service interface into serviceImpl class.
Step 1 :
Depending upon the event type RoleDelegator will call appropriate methods in the RoleServiceImpl.
RoleService will include all the methods that RoleServiceImpl class will call.
RoleService :
public interface RoleService extends SreeMandiraRPCService {
/**
* Delete the selected roles
* @param pDeleteRoleRequest contains role list to be deleted
*/
DeleteRoleResponse deleteRoles(DeleteRoleRequest pDeleteRoleRequest) throws RequesterException;
}
RoleServiceImpl :
public class RoleServiceImpl extends BaseRemoteService implements RoleService {
private static final long serialVersionUID = -8381083410066173866L;
private static Logger logger = Logger.getLogger(RoleServiceImpl.class);
/**
* Delete the selected roles
* @param pDeleteRoleRequest contains role list to be deleted
* @return DeleteRoleResponse
*/
public DeleteRoleResponse deleteRoles(final DeleteRoleRequest pDeleteRoleRequest) throws RequesterException {
DeleteRoleResponse deleteRoleResponse = new DeleteRoleResponse();
TransactionContext transactionContext = null;
try {
transactionContext = getTransactionContext((Integer) getSessionProperty(SessionEnum.USERLOGINID), getRequestMethod());
transactionContext.getConnection();
transactionContext.beginTransaction();
RoleBO roleBO = new RoleBO(transactionContext);
List<Integer> roleIDs = new ArrayList<Integer>();
for (RoleDetailDTO roleDetailDTO : pDeleteRoleRequest.getRoles()) {
roleIDs.add(roleDetailDTO.getRoleId());
}
} catch (RequesterException pException) {
} catch (Exception pException) {
} finally {
transactionContext.closeConnection();
logger.debug(IALoggingConstant.SERV_CONN_CLOSE);
}
return deleteRoleResponse;
}
Step 2 :
RoleServiceImpl will call RoleBO depending upon appropriate method call from RoleServiceImpl.
RoleBO :
public class RoleBO extends BaseBO {
private static Logger logger = Logger.getLogger(RoleBO.class);
public RoleBO(final TransactionContext pTransactionContext) {
super(pTransactionContext);
}
/**
* Delete roles
*
* @param pRoles
* contains roles
* @return List<RoleDetailDTO>
* @throws RequesterException
*/
public void deleteRoles(final List<Integer> pRoleIDs)
throws RequesterException {
logger.log(IATraceLevel.PERFORMANCE_CHECK,
IATraceLevel.MSG_METHOD_START);
RoleDAO roleDAO = new RoleDAO(transactionContext);
try {
if (((roleDAO.getUserGroups(pRoleIDs).size() > 0) || (roleDAO
.getUsers(pRoleIDs).size() > 0)))
{
roleDAO.deleteAuthEntityRole(pRoleIDs);
roleDAO.deleteRoleActions(pRoleIDs);
roleDAO.deleteRoles(pRoleIDs);
}
} catch (RequesterException pException) {
logger.debug(IALoggingConstant.SERV_TXN_ROLLBACK);
//logger.error(pException.getMessage(), pException);
throw pException;
}
catch (Exception pException) {
logger.error(pException.getMessage(), pException);
throw new RequesterException(pException.getMessage(), pException,
FaultUtil.buildFaultInfo(pException));
}
logger.log(IATraceLevel.PERFORMANCE_CHECK, IATraceLevel.MSG_METHOD_END);
}
Step 3 :
RoleBO will call RoleDAO class to delete the role.
RoleDAO :
public class RoleDAO extends BaseDAO {
private static Logger logger = Logger.getLogger(RoleDAO.class);
private TransactionContext transactionContext;
public RoleDAO(final TransactionContext pTransactionContext) {
super(pTransactionContext.getConnection());
transactionContext = pTransactionContext;
}
/**
* Returns the Role List
*
* @return List<RoleDetailDTO>
*/
@SuppressWarnings("unchecked")
public List<RoleDetailDTO> getRoles() throws RequesterException {
logger.info("METHOD_START");
List<RoleDetailDTO> roleDetails = new ArrayList<RoleDetailDTO>();
try {
Session session = ((HibernateConnection) connection).getSession();
Date currentDate = new Date();
Query selectRoleQuery = session.createQuery("FROM IARole iaRole ORDER BY iaRole.name");
List<IARole> roles = selectRoleQuery.list();
RoleDetailDTO roleDetailDTO = null;
for (IARole iARole : roles) {
roleDetailDTO = new RoleDetailDTO();
roleDetailDTO.setRoleId(iARole.getRoleId());
roleDetailDTO.setName(iARole.getName());
roleDetailDTO.setDescription(iARole.getDescription());
roleDetailDTO.setCreatedDate(iARole.getCreatedDate());
roleDetailDTO.setModifiedDate(iARole.getModifiedDate());
if (iARole.getRoleId() == 1) {
roleDetailDTO.setIsNotEditible(true);
}
roleDetails.add(roleDetailDTO);
}
} catch (Exception pException) {
pException.printStackTrace();
}
logger.info("METHOD_END");
return roleDetails;
}
Step 4 :
We are using IARole mapping file for communicating with the database. For all the operation with the database.
/**
* IARole generated by hbm2java
*/
@Entity
@Table(name = "IARole", uniqueConstraints = @UniqueConstraint(columnNames = "Name"))
public class IARole implements java.io.Serializable {
private Integer roleId;
private String name;
private String description;
private Date createdDate;
private Date modifiedDate;
private Set<IARoleAuthEntity> IARoleAuthEntities = new HashSet<IARoleAuthEntity>(0);
private Set<IARoleAction> IARoleActions = new HashSet<IARoleAction>(0);
public IARole() {
}
public IARole(String name, Date createdDate, Date modifiedDate) {
this.name = name;
this.createdDate = createdDate;
this.modifiedDate = modifiedDate;
}
public IARole(String name, String description, Date createdDate, Date modifiedDate,
Set<IARoleEntity> IARoleEntities, Set<IARoleAction> IARoleActions) {
this.name = name;
this.description = description;
this.createdDate = createdDate;
this.modifiedDate = modifiedDate;
this.IARoleAuthEntities = IARoleAuthEntities;
this.IARoleActions = IARoleActions;
}
@TableGenerator(name = "generator", table = "Table", pkColumnValue = "ROLE_GEN", allocationSize = 1, pkColumnName = "ID_NAME", valueColumnName = "ID_VAL")
@Id
@GeneratedValue(strategy = TABLE, generator = "generator")
@Column(name = "RoleID", unique = true, nullable = false)
public Integer getRoleId() {
return this.roleId;
}
public void setRoleId(Integer roleId) {
this.roleId = roleId;
}
@Column(name = "Name", unique = true, nullable = false)
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@Column(name = "Description")
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "CreatedDate", nullable = false, length = 23)
public Date getCreatedDate() {
return this.createdDate;
}
public void setCreatedDate(Date createdDate) {
this.createdDate = createdDate;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "ModifiedDate", nullable = false, length = 23)
public Date getModifiedDate() {
return this.modifiedDate;
}
public void setModifiedDate(Date modifiedDate) {
this.modifiedDate = modifiedDate;
}
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "IARole")
public Set<IARoleAuthEntity> getIARoleAuthEntities() {
return this.IARoleAuthEntities;
}
public void setIARoleAuthEntities(Set<IARoleAuthEntity> IARoleAuthEntities) {
this.IARoleAuthEntities = IARoleAuthEntities;
}
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "IARole")
public Set<IARoleAction> getIARoleActions() {
return this.IARoleActions;
}
public void setIARoleActions(Set<IARoleAction> IARoleActions) {
this.IARoleActions = IARoleActions;
}
}
Step 5 :
We need to configure Microsoft SQLServer JDBC 3.0 driver in the configuration file.
The driver class name is com.microsoft.sqlserver.jdbc.SqlServerDriver.
The complete URL format is
jdbc:sqlserver://[serverName[\instanceName][:portNumber]][;property=value[;property=value]]
The hibernate configuration file is given below
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">
com.microsoft.sqlserver.jdbc.SQLServerDriver
</property>
<property name="connection.url">
jdbc:sqlserver://[serverName[\instanceName][:portNumber]];databaseName=<databaseName>
</property>
<property name="connection.username">sa</property>
<property name="connection.password">lal</property>
<property name="connection.pool_size>10</property>
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<mapping class="com.satya.sreemandira.IARole" />
</session-factory>
</hibernate-configuration>
Step 6 :
The SessionFactory should be configured using AnnotationConfiguration object instead of Configuration object used with XML mappings.
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
sessionFactory = new AnnotationConfiguration().configure()
.buildSessionFactory();
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
Step 7:
We need to add below three jar files in the class path for above configuration working.
hibernate-commons-annotations.jar
ejb3-persistence.jar
hibernate-annotations.jar
private static Logger logger = Logger.getLogger(RoleDAO.class);
private TransactionContext transactionContext;
public RoleDAO(final TransactionContext pTransactionContext) {
super(pTransactionContext.getConnection());
transactionContext = pTransactionContext;
}
/**
* Returns the Role List
*
* @return List<RoleDetailDTO>
*/
@SuppressWarnings("unchecked")
public List<RoleDetailDTO> getRoles() throws RequesterException {
logger.info("METHOD_START");
List<RoleDetailDTO> roleDetails = new ArrayList<RoleDetailDTO>();
try {
Session session = ((HibernateConnection) connection).getSession();
Date currentDate = new Date();
Query selectRoleQuery = session.createQuery("FROM IARole iaRole ORDER BY iaRole.name");
List<IARole> roles = selectRoleQuery.list();
RoleDetailDTO roleDetailDTO = null;
for (IARole iARole : roles) {
roleDetailDTO = new RoleDetailDTO();
roleDetailDTO.setRoleId(iARole.getRoleId());
roleDetailDTO.setName(iARole.getName());
roleDetailDTO.setDescription(iARole.getDescription());
roleDetailDTO.setCreatedDate(iARole.getCreatedDate());
roleDetailDTO.setModifiedDate(iARole.getModifiedDate());
if (iARole.getRoleId() == 1) {
roleDetailDTO.setIsNotEditible(true);
}
roleDetails.add(roleDetailDTO);
}
} catch (Exception pException) {
pException.printStackTrace();
}
logger.info("METHOD_END");
return roleDetails;
}
Step 4 :
We are using IARole mapping file for communicating with the database. For all the operation with the database.
/**
* IARole generated by hbm2java
*/
@Entity
@Table(name = "IARole", uniqueConstraints = @UniqueConstraint(columnNames = "Name"))
public class IARole implements java.io.Serializable {
private Integer roleId;
private String name;
private String description;
private Date createdDate;
private Date modifiedDate;
private Set<IARoleAuthEntity> IARoleAuthEntities = new HashSet<IARoleAuthEntity>(0);
private Set<IARoleAction> IARoleActions = new HashSet<IARoleAction>(0);
public IARole() {
}
public IARole(String name, Date createdDate, Date modifiedDate) {
this.name = name;
this.createdDate = createdDate;
this.modifiedDate = modifiedDate;
}
public IARole(String name, String description, Date createdDate, Date modifiedDate,
Set<IARoleEntity> IARoleEntities, Set<IARoleAction> IARoleActions) {
this.name = name;
this.description = description;
this.createdDate = createdDate;
this.modifiedDate = modifiedDate;
this.IARoleAuthEntities = IARoleAuthEntities;
this.IARoleActions = IARoleActions;
}
@TableGenerator(name = "generator", table = "Table", pkColumnValue = "ROLE_GEN", allocationSize = 1, pkColumnName = "ID_NAME", valueColumnName = "ID_VAL")
@Id
@GeneratedValue(strategy = TABLE, generator = "generator")
@Column(name = "RoleID", unique = true, nullable = false)
public Integer getRoleId() {
return this.roleId;
}
public void setRoleId(Integer roleId) {
this.roleId = roleId;
}
@Column(name = "Name", unique = true, nullable = false)
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@Column(name = "Description")
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "CreatedDate", nullable = false, length = 23)
public Date getCreatedDate() {
return this.createdDate;
}
public void setCreatedDate(Date createdDate) {
this.createdDate = createdDate;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "ModifiedDate", nullable = false, length = 23)
public Date getModifiedDate() {
return this.modifiedDate;
}
public void setModifiedDate(Date modifiedDate) {
this.modifiedDate = modifiedDate;
}
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "IARole")
public Set<IARoleAuthEntity> getIARoleAuthEntities() {
return this.IARoleAuthEntities;
}
public void setIARoleAuthEntities(Set<IARoleAuthEntity> IARoleAuthEntities) {
this.IARoleAuthEntities = IARoleAuthEntities;
}
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "IARole")
public Set<IARoleAction> getIARoleActions() {
return this.IARoleActions;
}
public void setIARoleActions(Set<IARoleAction> IARoleActions) {
this.IARoleActions = IARoleActions;
}
}
Step 5 :
We need to configure Microsoft SQLServer JDBC 3.0 driver in the configuration file.
The driver class name is com.microsoft.sqlserver.jdbc.SqlServerDriver.
The complete URL format is
jdbc:sqlserver://[serverName[\instanceName][:portNumber]][;property=value[;property=value]]
The hibernate configuration file is given below
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">
com.microsoft.sqlserver.jdbc.SQLServerDriver
</property>
<property name="connection.url">
jdbc:sqlserver://[serverName[\instanceName][:portNumber]];databaseName=<databaseName>
</property>
<property name="connection.username">sa</property>
<property name="connection.password">lal</property>
<property name="connection.pool_size>10</property>
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<mapping class="com.satya.sreemandira.IARole" />
</session-factory>
</hibernate-configuration>
Step 6 :
The SessionFactory should be configured using AnnotationConfiguration object instead of Configuration object used with XML mappings.
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
sessionFactory = new AnnotationConfiguration().configure()
.buildSessionFactory();
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
Step 7:
We need to add below three jar files in the class path for above configuration working.
hibernate-commons-annotations.jar
ejb3-persistence.jar
hibernate-annotations.jar
No comments:
Post a Comment