-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Hibernate 3.3.2 / Struts 2.1.6 / Spring 2.5.3
PostPosted: Fri Nov 13, 2009 4:19 pm 
Beginner
Beginner

Joined: Fri Nov 13, 2009 4:05 pm
Posts: 30
I am still a hibernate newbie so I am trying to get a good grasp on the best way I can leverage Hibernate with my Struts2 application. The web application will primarily be used for querying database tables for records and displaying the results to the web browser. In the past I have used Struts1.2 and JDBC directly and I've read that Hibernate really is a good way to go to avoid the unnecessary use of the JDBC API in my Java manager classes.

All the examples I have seen appear to say that I want to create several types of class objects that resemble this pattern:

com\company\appname\hibernate\dao\UserDAO.java
com\company\appname\hibernate\dao\GenericDAO.java
com\company\appname\hibernate\model\User.java
com\company\appname\hibernate\service\UserService.java
com\company\appname\hibernate\service\GenericService.java

Working with the Generic DAO pattern, the DAO and Service classes looks like:

Code:
public abstract class Generic<T, ID extends Serializable> {
  private Class<T>        persistenceClass;
  private EntityManager em;

  @PersistenceContext
  public void setEntityManager(EntityManager em) {
    this.em = em;
  }
  public EntityManager getEntityManager() {
    return em;
  }
  public void save(T entity) {
    getEntityManager().persist(entity);
  }
  public void delete(T entity) {
    getEntityManager().remove(getEntityManager().merge(entity));
  }
  public T getById(Object id) {
    return(getEntityManager.find(persistenceClass, id));
  }
}

public interface GenericService<T> {
  T create(T entity);
  void delete(T entity);
  T update(T entity);
  T getById(Object id);
  List<T>  list(int page, int size);
}



From here the UserDAO would extend this generic class above and add any necessary methods it needs. The UserService class would implement the GenericService<User> and be annotated as a @Transactional class and function calls that call the UserDAO object's member functions. As an example:

Code:
public User create(User entity) {
  authorDAO.save(entity);
  return(entity);
}


This seems to make a little sense, but I then find examples that refer to hbm.xml mapping files and so on which I don't have in my application presently. So I am very confused on what is the proper way to try and marry all three of these frameworks in a solid solution that will enable me to leverage the features of hibernate for doing primarily queries with some insert/modification/delete operations on specific objects only. And one thing to keep in mind is that the searches would be across various numbers of tables in the RDBMS and from what I have seen, it appears that I can create multiple Entity objects that represent each table and based on annotations/mappings link these all together?

I suppose if someone could take time to help walk me through this process with a very basic simple yet fundamental example, it would by far ease my confusion and uncertainty on my direction. I'll be willing to provide whatever code samples or whatnot needed. Just any help is more than greatly appreciated.


Top
 Profile  
 
 Post subject: Re: Hibernate 3.3.2 / Struts 2.1.6 / Spring 2.5.3
PostPosted: Fri Nov 13, 2009 10:58 pm 
Beginner
Beginner

Joined: Fri Nov 13, 2009 4:05 pm
Posts: 30
Is there any special handling I need to take into account when I use JPA/Hibernate with Spring and Struts with regard to the session? I seem to have no problem with my DAO objects querying the table and getting a list of results or creating objects. But the remove was throwing an exception until I changed the code from getEntityManager().remove(entity); to getEntityManager().remove(getEntityManager().merge(entity));. Now that seemed to resolve the issue but now find throws an exception. Here's the code:

Code:
public T findById(Object id) {
  return(getEntityManager().find(clazz, id));
}


The exception stack is:
java.lang.NullPointerException

org.hibernate.impl.SessionImpl.get(SessionImpl.java:836)
org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:182)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:194)
$Proxy9.find(Unknown Source)
com.setech.sampleapp.hibernate.dao.GenericDAO.findById(GenericDAO.java:71)
com.setech.sampleapp.hibernate.service.AuthorService.findById(AuthorService.java:45)
com.setech.sampleapp.hibernate.service.AuthorService.findById(AuthorService.java:1)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:310)
org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
$Proxy12.findById(Unknown Source)
com.setech.sampleapp.struts2.AuthorAction.edit(AuthorAction.java:41)

Can someone explain to me what i have overlooked with respect to configuration?

applicationContext.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.0.xsd">

   <!--
      This is the BeanPostProcessor object that processes PersistenceUnit
      and PersistenceContext annotations, for injection of the corresponding
      JPA resources EntityManager and EntityManagerFactory.  Any SPring managed
      objects with these annotations will be injected.
      
      Did not see this referenced here
      http://www.springbyexample.org/examples/one-to-many-jpa-template-config.html
    -->
    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
   
    <!-- Import -->
   <import resource="sampleappContext.xml"/>
   
   <!--
       Many references here have pointed to using 
       org.springframework.orm.hibernate3.LocalSessionFactoryBean
       
       But examples located here
       http://www.springbyexample.org/examples/one-to-many-jpa-template-config.html
       recommend that you use the LocalContainerEntityManagerFactoryBean object
       instead.
     -->
    <bean   id="entityManagerFactory"
           class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
           <property name="dataSource" ref="dataSource" />
           <!-- <property name="persistenceUnitName" value="simple-jpa-template"/> -->
           <property name="jpaVendorAdapter">
               <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                      <property name="database" value="MYSQL" />
                      <!-- <property name="database" value="SQL_SERVER" /> -->
                      <property name="showSql" value="true" />
                      <property name="generateDdl" value="true" />
               </bean>
           </property>
    </bean>
   
   <!--  Local MySQL Installation -->
    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3308/sampleapp2" />
        <property name="username" value="root" />
        <property name="password" value="w3bl0g1c" />
    </bean>       
     
   <!--
      Some examples have shown for the transactionManager to reference class
      org.springframework.orm.hibernate3.HibernateTransactionManager
      
      But it appears that JPA/Hibernate have been integrated and the following
      definition is expected for this framework to work.
    -->
   <bean    id="transactionManager"
         class="org.springframework.orm.jpa.JpaTransactionManager">
         <property name="entityManagerFactory" ref="entityManagerFactory" />
   </bean>
           
   
   <!--  Defines our transaction manager for transaction annotated objects -->
   <tx:annotation-driven transaction-manager="transactionManager" />
   
</beans>


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.