Results 1 to 7 of 7

Thread: webflow and pure Hibernate

Hybrid View

  1. #1

    Default webflow and pure Hibernate

    Is it a good choise to run Spring webflow with pure Hibernate

    a la:

    org.springframework.orm.hibernate4.HibernateTransa ctionManager


    org.springframework.orm.hibernate4.LocalSessionFac toryBean


    I worked a lot with Hibernate and I think Hibernate has much advantages.

    I plan a project and I would be glad if you could give me some opinions
    about what is the better solution

    Perhaps someone can give me a good code example or a good tutorial

    thank for your opinions

  2. #2

    Default

    We use webflow and really enjoy using it. Don't see why you can't use Hibernate with webflow for an orm.

    Jeff

  3. #3

    Default

    Thank's for the answer.
    But I believe I have my Question wrong formulated.
    It give's two posibilities for Hibernate in webflow or spring generaly.
    Pure Hibernate or Hibernate over JPA.
    Would you prever the solution over JPA or pure Hibernate.

    I think pure Hibernate has some facilities that make the life easier.

    for Example the JPA solution

    @PersistenceContextEntityManager entityManager;
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Employee> empl = criteriaBuilder.createQuery(Employee.class);
    Root<Employee> employee = empl.from(Employee.class);
    empl.select(employee);
    TypeQuery<Employee> typeQuery = entityManager.createQuery(empl);
    List<Employee> employees = typeQuery.getResultList();

    And the Hibernate Solution

    In Hibernate I autowire the sessionFactory and wrote

    List cats = session.getCurrentSession.createCriteria(Employee. class)
    .add( Restrictions.like("name", "theName") )
    .addOrder( Order.asc("age") )
    .list();

    Though the second solution has a WHERE and a ORDER BY statement it look's like esear

    If I think wrong here then let me know it.

  4. #4
    Join Date
    Aug 2012
    Location
    Seattle
    Posts
    5

    Default

    It is really up to your project requirements. If your project should be following the java standard API, then use JPA, otherwise, I recommend to use Hibernate which gives more flexible. The only disadvantage of Hibernate is that your project is tightly coupled with Hibernate APIs.

  5. #5

    Default

    Thank's for the replies. Is in fact a good mitivation to try it out.

    Ok !
    I tried it out with pure Hibernate and my Dog

    @Entity
    @Table(name="dogs" )
    public class Dog implements Serializable{
    private static final long serialVersionUID = 1L;

    public Dog(String name) {
    this.name = name;
    }

    private String name;

    // Getters and Setters

    }

    Here the Dao for Dog

    @Transactional
    public interface DogDao {

    @Transactional
    public void insert(Dog dog);
    }

    Here the Dao Implementation

    @Service("dogDao")
    public class DogDaoImpl implements DogDao, Serializable {
    private static final long serialVersionUID = 1L;

    @Autowired
    SessionFactory sessionFactory;

    @Override
    public void insert(Dog dog) {
    sessionFactory.getCurrentSession().persist(dog);
    }
    }


    And here my Test Class for my Dog

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations={"/WEB-INF/config/root-context.xml", "/WEB-INF/config/app/servlet-context.xml"})
    @TransactionConfiguration(transactionManager="tran sactionManager", defaultRollback=false)

    public class TTestHibernateSessions {

    private final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    DogDao dao;

    Dog dogge;

    @Before
    public void setUp(){

    }
    @Test @Transactional
    public void firstTest(){
    dogge = new Dog("wuff");
    dao.insert(dogge);
    System.out.println("ende");
    }}


    All Ok !!
    My Dog is in the Database

    But !!!!
    When run with Webflow nothing is Ok.

    Here my Webflow Config

    At first the flow Config (dog-flow.xml)

    <flow xmlns="http://www.springframework.org/schema/webflow"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/webflow http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd">

    <persistence-context />



    <view-state id="dog" view="dog.xhtml">
    <transition on="save" to="end">
    <evaluate expression="dogDao.insert(dog)" />
    </transition>
    </view-state>

    <end-state id="end" commit="true" />

    </flow>


    And here the relevant Code in the view dog.xhtml

    <ui:define name="content">
    <div>
    <h:form>
    <h:inputText id="dogName" value="#{dog.name}" />
    <h:commandButton action="save" value="Save" />
    </h:form>
    </div>
    </ui:define>



    I think to here all should be fine. Please correct me when I'm wrong.

    And my data-acces-config.xml should be fine too.

    <?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:tx="http://www.springframework.org/schema/tx"
    xmlns="http://www.springframework.org/schema/p"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schem...ring-beans.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">


    <tx:annotation-driven transaction-manager="transactionManager"/>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSes sionFactoryBean">
    <property name="dataSource" ref="mySqlDataSource"/>
    <property name="annotatedClasses">
    <list>
    <value>de.mydomain.entity.test.Dog</value>
    </list>
    </property>
    <property name="hibernateProperties">
    <value>
    <!-- e.g. validate | update | create | create-drop -->
    hibernate.hbm2ddl.auto=create
    hibernate.show_sql=true
    hibernate.current_session_context_class=org.spring framework.orm.hibernate4.SpringSessionContext
    hibernate.connection.charSet=utf8
    hibernate.hbm2ddl.import_files=/imports/kunden.sql
    hibernate.c3p0.min_size=5
    hibernate.c3p0.max_size=20
    hibernate.c3p0.timeout=1800
    hibernate.c3p0.max_statements=50
    hibernate.dialect=org.hibernate.dialect.MySQL5Inno DBDialect
    </value>
    </property>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.hibernate4.Hibernat eTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    </beans>


    But when I try to load my "Dog" in my browser I receive the following Exception

    java.lang.NoSuchMethodError: org.hibernate.SessionFactory.openSession()Lorg/hibernate/classic/Session;
    org.springframework.webflow.persistence.HibernateF lowExecutionListener.createSession(HibernateFlowEx ecutionListener.java:187)
    org.springframework.webflow.persistence.HibernateF lowExecutionListener.sessionStarting(HibernateFlow ExecutionListener.java:118)
    org.springframework.webflow.engine.impl.FlowExecut ionListeners.fireSessionStarting(FlowExecutionList eners.java:117)
    org.springframework.webflow.engine.impl.FlowExecut ionImpl.start(FlowExecutionImpl.java:365)
    org.springframework.webflow.engine.impl.FlowExecut ionImpl.start(FlowExecutionImpl.java:222)
    org.springframework.webflow.executor.FlowExecutorI mpl.launchExecution(FlowExecutorImpl.java:140)
    org.springframework.webflow.mvc.servlet.FlowHandle rAdapter.handle(FlowHandlerAdapter.java:193)
    org.springframework.faces.webflow.JsfFlowHandlerAd apter.handle(JsfFlowHandlerAdapter.java:48)
    org.springframework.web.servlet.DispatcherServlet. doDispatch(DispatcherServlet.java:923)
    org.springframework.web.servlet.DispatcherServlet. doService(DispatcherServlet.java:852)
    org.springframework.web.servlet.FrameworkServlet.p rocessRequest(FrameworkServlet.java:882)
    org.springframework.web.servlet.FrameworkServlet.d oGet(FrameworkServlet.java:778)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:621)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:722)



    When I remove the <persistence-context /> from my dog-flow.xml the <var name="dog" class="de.mydomain.entity.test.Dog"/>
    could not be initialized.
    Here a piece of the root cause


    java.lang.NoSuchMethodException: de.mydomain.entity.test.Dog.<init>()
    java.lang.Class.getConstructor0(Class.java:2723)
    java.lang.Class.getDeclaredConstructor(Class.java: 2002)
    org.springframework.beans.factory.support.SimpleIn stantiationStrategy.instantiate(SimpleInstantiatio nStrategy.java:67)
    org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.instantiateBean(Abstrac tAutowireCapableBeanFactory.java:990)
    org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBeanInstance(Abst ractAutowireCapableBeanFactory.java:943)
    org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.doCreateBean(AbstractAu towireCapableBeanFactory.java:485)
    org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:456)
    org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:286)
    org.springframework.webflow.engine.support.BeanFac toryVariableValueFactory.createInitialValue(BeanFa ctoryVariableValueFactory.java:52)
    org.springframework.webflow.engine.FlowVariable.cr eate(FlowVariable.java:79)
    org.springframework.webflow.engine.Flow.createVari ables(Flow.java:627)
    org.springframework.webflow.engine.Flow.start(Flow .java:527)


    So I'm thinking Hibernate work's fine because: without the <persistence-context /> entry im my flow
    my Entity annotation isn't detected per annotation scanning.

    I inserted then set
    hibernate.current_session_context_class=org.spring framework.orm.hibernate4.SpringSessionContext
    in my hibernate Properties but nothing is better.

    Is it possible that Spring webflow in the current release does not suppoert hibernate in the 4.x.x version,
    because this org/hibernate/classic/Session look's like hibernate 3.x.x

    Here my version's that I used:

    <spring.version>3.1.2.RELEASE</spring.version>
    <spring.webflow.version>2.3.1.RELEASE</spring.webflow.version>
    <hibernate.version>4.1.5.Final</hibernate.version>
    <tomcat-version>7.0.25</tomcat-version>

    I would be glad if someone can give me suggestions what is the problem in my new project.

  6. #6
    Join Date
    Aug 2012
    Location
    Seattle
    Posts
    5

    Default

    Did you have empty constructor in the Dog class ? When I see the example above, there might be missing Dog() constructor in the class ?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •