OpenJPA:此配置不允许运行时优化,但列出的类型在构建时或使用javaagent的类加载时未得到增强

时间:2015-12-11 09:49:47

标签: java mysql maven java-ee openjpa

所以,经过大量的搜索(徒劳),我决定发布这个。所以,我正在使用Open JPA(2.2.0)开发这个Web应用程序。执行应用程序会引发以下错误:

控制台打印输出:

<openjpa-2.3.0-r422266:1540826 nonfatal user error> org.apache.openjpa.persistence.ArgumentException: This configuration disallows runtime optimization, but the following listed types were not enhanced at build time or at class load time with a javaagent: "
Entities.User".
    org.apache.openjpa.enhance.ManagedClassSubclasser.prepareUnenhancedClasses(ManagedClassSubclasser.java:115)
    org.apache.openjpa.kernel.AbstractBrokerFactory.loadPersistentTypes(AbstractBrokerFactory.java:312)
    org.apache.openjpa.kernel.AbstractBrokerFactory.initializeBroker(AbstractBrokerFactory.java:236)
    org.apache.openjpa.kernel.AbstractBrokerFactory.newBroker(AbstractBrokerFactory.java:212)
    org.apache.openjpa.kernel.DelegatingBrokerFactory.newBroker(DelegatingBrokerFactory.java:155)
    org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:226)
    org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:153)
    org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:59)
    Servlet.Registration.doPost(Registration.java:78)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

配置:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
  xmlns="http://java.sun.com/xml/ns/persistence" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/persistence 
                  http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
  <persistence-unit name="persistenceUnit">
    <class>Entities.User</class>
    <properties>
      <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/alda" />
      <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
      <property name="javax.persistence.jdbc.user" value="root" />
      <property name="javax.persistence.jdbc.password" value="" />
      <property name="openjpa.jdbc.DBDictionary" value="mysql" />
      <!-- Cette propriété active la log des requêtes SQL réalisées par OpenJPA -->
      <property name="openjpa.Log" value="SQL=Trace" />
      <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>
    </properties>
  </persistence-unit>
</persistence>

注册课程:

public class Registration extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }


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

        String name = request.getParameter("name");
        String firstName = request.getParameter("firstName");
        String email = request.getParameter("email");
        String password = request.getParameter("password");
        String confirmation = request.getParameter("confirmation");
        String phone = request.getParameter("phone");
        String address = request.getParameter("address");

        System.out.println("Ajout d'un utilisateur:"+name+","+firstName+","+email+","+password+","+confirmation);


        EntityManagerFactory emf = Persistence.createEntityManagerFactory("persistenceUnit");
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();

        User u = new User();

        u.setName(name);
        u.setFirstName(firstName);
        u.setPassword(password);
        u.setEmail(email);
        u.setAddress(address);
        u.setPhone(phone);
        em.persist(u);
        em.getTransaction().commit();



        this.getServletContext().getRequestDispatcher("/Home.jsp").forward(request, response);

    }
}

3 个答案:

答案 0 :(得分:1)

您是否配置了构建时增强功能? OpenJPA需要修改类文件才能使用它们,因此您应该允许它在某些时候执行它。这个doc page很好地概述了如何做到这一点。例如,它可以用maven完成。另一种方法是运行时增强。

答案 1 :(得分:0)

我没有配置增强功能,在您的日志中,我看到了这个“ Entities.User”。我说的是什么意思?答案:例如双重检查您的代码和我的代码(如果其中存在一些错误)。在我的情况下,它是列ID和用户名的两倍@Id:!那是魔术。

答案 2 :(得分:0)

只需在您的 persistence.xml 文件中添加一个 Provider,例如:

<persistence-unit name="persistenceUnit">
    **<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>**
    <class>Entities.User</class>