Announcement Announcement Module
Collapse
No announcement yet.
Spring 2 + Tomcat 6 + OpenJPA Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring 2 + Tomcat 6 + OpenJPA

    Hi,

    I am having problem putting Spring 2, Tomcat 6 and OpenJPA together. Test application that I have written but I can't get it to work:-

    import java.util.Date;
    import javax.persistence.Basic;
    import javax.persistence.Entity;
    import javax.persistence.Id;

    Message.java:-

    package com.kzen.test.jpa

    @Entity
    public class Message
    {
    @Id
    private long id = System.currentTimeMillis();
    @Basic
    private String message;
    @Basic
    private Date created = new Date();

    public Message()
    {
    super();
    }
    public Date getCreated()
    {
    return created;
    }
    public void setCreated(Date created)
    {
    this.created = created;
    }
    public long getId()
    {
    return id;
    }
    public void setId(long id) {
    this.id = id;
    }
    public String getMessage() {
    return message;
    }
    public void setMessage(String message) {
    this.message = message;
    }
    }

    MessageDao.java:-

    package com.kzen.test.jpa

    import javax.persistence.EntityManagerFactory;
    import org.springframework.orm.jpa.JpaTemplate;

    public class MessageDao
    {
    private JpaTemplate jpaTemplate;

    public void setEntityManagerFactory(EntityManagerFactory emf)
    {
    this.jpaTemplate = new JpaTemplate(emf);
    }

    public void createNewMessage(String message)
    {
    Message msg = new Message();
    msg.setMessage(message);

    jpaTemplate.persist(msg);
    jpaTemplate.flush();
    }

    }

    JPAServlet:-

    package com.kzen.test.jpa

    import java.io.IOException;
    import java.io.PrintWriter;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlAp plicationContext;

    public class JPAServlet extends HttpServlet
    {
    private MessageDao messageDao;

    @Override
    public void init() throws ServletException
    {
    super.init();

    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext .xml");
    messageDao = (MessageDao) context.getBean("messageDao");
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
    PrintWriter out = response.getWriter();
    response.setContentType("text/html");

    messageDao.createNewMessage("Message on " + System.currentTimeMillis());

    out.println("OK");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
    doGet(request, response);
    }
    }

    And I have the following configurations file:-

    applicationContext.xml :-

    <?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/schem...-beans-2.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schem...ing-tx-2.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

    <bean id="persistentUnitManager" class="org.springframework.orm.jpa.persistenceunit .DefaultPersistenceUnitManager">
    <property name="persistenceXmlLocations">
    <list>
    <value>kz-persistent.xml</value>
    </list>
    </property>
    <property name="dataSources">
    <map>
    <entry key="testDataSource" value-ref="testDataSource"/>
    </map>
    </property>
    <property name="defaultDataSource" ref="testDataSource"/>
    <property name="loadTimeWeaver">
    <bean class="org.springframework.instrument.classloading .ReflectiveLoadTimeWeaver"/>
    </property>
    </bean>

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerE ntityManagerFactoryBean">
    <property name="persistenceUnitManager" ref="persistentUnitManager" />
    </bean>

    <bean id="testDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/test"/>
    <property name="username" value="root"/>
    <property name="password" value="root"/>
    <property name="initialSize" value="10"/>
    <property name="maxIdle" value="10"/>
    <property name="minIdle" value="10"/>
    <property name="maxActive" value="10"/>
    <property name="maxWait" value="-1"/>
    </bean>

    <bean id="messageDao" class="com.kzen.test.jpa.MessageDao">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <bean id="myTxManager" class="org.springframework.orm.jpa.JpaTransactionM anager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>

    <aop:config>
    <aopointcut id="messageDaoMethods" expression="execution(* com.kzen.test.jpa.MessageDao.*(..))"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="messageDaoMethods"/>
    </aop:config>

    <tx:advice id="txAdvice" transaction-manager="myTxManager">
    <tx:attributes>
    <tx:method name="createNewMessage*" propagation="REQUIRED"/>
    </tx:attributes>
    </tx:advice>

    </beans>

    kz-persistent.xml :-

    <?xml version="1.0" encoding="utf-8"?>
    <persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
    <persistence-unit name="kz-persistent-unit">
    <provider>org.apache.openjpa.persistence.Persisten ceProviderImpl</provider>
    <non-jta-data-source>testDataSource</non-jta-data-source>
    <class>com.kzen.test.jpa.Message</class>
    </persistence-unit>
    </persistence>

    When I access the JPAServlet, I can't get away from the following exception:-

    org.springframework.dao.InvalidDataAccessApiUsageE xception: Attempt to cast instance "com.kzen.test.jpa.Message@2b2af3" to PersistenceCapable failed. Ensure that it has been enhanced.; nested exception is <0.9.7-incubating nonfatal user error> org.apache.openjpa.persistence.ArgumentException: Attempt to cast instance "com.kzen.test.jpa.Message@2b2af3" to PersistenceCapable failed. Ensure that it has been enhanced.
    FailedObject: com.kzen.test.jpa.Message@2b2af3
    org.springframework.orm.jpa.EntityManagerFactoryUt ils.convertJpaAccessExceptionIfPossible(EntityMana gerFactoryUtils.java:229)
    org.springframework.orm.jpa.DefaultJpaDialect.tran slateExceptionIfPossible(DefaultJpaDialect.java:11 3)
    org.springframework.dao.support.DataAccessUtils.tr anslateIfNecessary(DataAccessUtils.java:212)
    org.springframework.orm.jpa.JpaAccessor.translateI fNecessary(JpaAccessor.java:152)
    org.springframework.orm.jpa.JpaTemplate.execute(Jp aTemplate.java:196)
    org.springframework.orm.jpa.JpaTemplate.persist(Jp aTemplate.java:261)
    com.kzen.test.jpa.MessageDao.createNewMessage(Mess ageDao.java:21)
    com.kzen.test.jpa.MessageDao$$FastClassByCGLIB$$44 3eca31.invoke(<generated>)
    net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy. java:149)
    org.springframework.aop.framework.Cglib2AopProxy$C glibMethodInvocation.invokeJoinpoint(Cglib2AopProx y.java:695)
    org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :144)
    org.springframework.transaction.interceptor.Transa ctionInterceptor.invoke(TransactionInterceptor.jav a:107)
    org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :166)
    org.springframework.aop.framework.Cglib2AopProxy$D ynamicAdvisedInterceptor.intercept(Cglib2AopProxy. java:630)
    com.kzen.test.jpa.MessageDao$$EnhancerByCGLIB$$9a0 61d47.createNewMessage(<generated>)
    com.kzen.test.jpa.JPAServlet.doGet(JPAServlet.java :33)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:690)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:803)

    root cause

    <0.9.7-incubating nonfatal user error> org.apache.openjpa.persistence.ArgumentException: Attempt to cast instance "com.kzen.test.jpa.Message@2b2af3" to PersistenceCapable failed. Ensure that it has been enhanced.
    FailedObject: com.kzen.test.jpa.Message@2b2af3
    org.apache.openjpa.kernel.BrokerImpl.assertPersist enceCapable(BrokerImpl.java:4271)
    org.apache.openjpa.kernel.BrokerImpl.persist(Broke rImpl.java:2373)
    org.apache.openjpa.kernel.BrokerImpl.persist(Broke rImpl.java:2233)
    org.apache.openjpa.kernel.DelegatingBroker.persist (DelegatingBroker.java:1010)
    org.apache.openjpa.persistence.EntityManagerImpl.p ersist(EntityManagerImpl.java:541)
    org.springframework.orm.jpa.JpaTemplate$5.doInJpa( JpaTemplate.java:263)
    org.springframework.orm.jpa.JpaTemplate.execute(Jp aTemplate.java:191)
    org.springframework.orm.jpa.JpaTemplate.persist(Jp aTemplate.java:261)
    com.kzen.test.jpa.MessageDao.createNewMessage(Mess ageDao.java:21)
    com.kzen.test.jpa.MessageDao$$FastClassByCGLIB$$44 3eca31.invoke(<generated>)
    net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy. java:149)
    org.springframework.aop.framework.Cglib2AopProxy$C glibMethodInvocation.invokeJoinpoint(Cglib2AopProx y.java:695)
    org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :144)
    org.springframework.transaction.interceptor.Transa ctionInterceptor.invoke(TransactionInterceptor.jav a:107)
    org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :166)
    org.springframework.aop.framework.Cglib2AopProxy$D ynamicAdvisedInterceptor.intercept(Cglib2AopProxy. java:630)
    com.kzen.test.jpa.MessageDao$$EnhancerByCGLIB$$9a0 61d47.createNewMessage(<generated>)
    com.kzen.test.jpa.JPAServlet.doGet(JPAServlet.java :33)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:690)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:803)


    Is it the load time weaver not working? Or I have a wrong configuration? Please kindly advise. Thanks in advance.

  • #2
    You have to enhance the compiled code before use it for OpenJPA.

    Comment


    • #3
      Load Time Weaver

      Sorry please correct me if I am wrong, isn't the load time weaver that should enhance it during runtime?

      Comment


      • #4
        Spring provides a runtime load time weaver (thanks to aspectj).

        I think zhijiang is refering to the OpenJPA bytecode enhancer : like Spring, OpenJPA does some bytecode manipulation, but it's doing it with another framework.

        This probably comes from Kodo JDO (the ancestor of OpenJPA), I remember you needed to run some kind of post compilator to enhance your code before being able to use it.

        Comment

        Working...
        X