0

我正在制作一个简单的 URL Shortener,我需要一些 Mysql 连接,但是当我启动服务器并登录到调用实体管理器的 servlet 时,我遇到了这个异常:

javax.persistence.PersistenceException:[PersistenceUnit:jpa-suplink] 无法构建 EntityManagerFactory

我的项目是这样构建的:

Project name
+ Java Resources
++ src
+++ com.suplink.entity
++++ Account.java
+++ com.suplink.filter
++++ redirectFilter.java
+++ com.suplink.servlet
++++ Login.java
+++ META-INF
++++ persistence.xml
+ WebContent
++ login.jsp

这是我访问 localhost:8080/Suplink 时的控制台日志:

janv. 16, 2013 7:32:07 PM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jre7\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files (x86)\AMD APP\bin\x86_64;C:\Program Files (x86)\AMD APP\bin\x86;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;.
janv. 16, 2013 7:32:07 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:Suplink' did not find a matching property.
janv. 16, 2013 7:32:07 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
janv. 16, 2013 7:32:07 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
janv. 16, 2013 7:32:07 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 717 ms
janv. 16, 2013 7:32:07 PM org.apache.catalina.core.StandardService startInternal
INFO: Démarrage du service Catalina
janv. 16, 2013 7:32:07 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.34
janv. 16, 2013 7:32:08 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
janv. 16, 2013 7:32:08 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
janv. 16, 2013 7:32:08 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 997 ms
janv. 16, 2013 7:32:13 PM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
janv. 16, 2013 7:32:13 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.1.9.Final}
janv. 16, 2013 7:32:13 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
janv. 16, 2013 7:32:13 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
javax.persistence.PersistenceException: [PersistenceUnit: jpa-suplink] Unable to build EntityManagerFactory

还有我的 Login.java、Account.java 和 persistence.xml

登录.java

package com.suplink.servlet;

import java.io.IOException;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.suplink.entity.Account;

/**
 * Servlet implementation class Login
 */
@WebServlet(
        description = "Login form & Index page.", 
        urlPatterns = {
            "/login",
            "/index.html"
        })
public class Login extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public Login() {
        super();
        // TODO Auto-generated constructor stub
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("login.jsp").forward(request, response);

        try {
        EntityManagerFactory emf = null;
        emf = Persistence.createEntityManagerFactory("jpa-suplink");
        EntityManager em = emf.createEntityManager();

        Query query = em.createQuery("SELECT email FROM account");

        List<Account> accounts = (List<Account>)query.getResultList();
        System.out.println(query);

        for (Account a : accounts) {
            System.out.println(a);
        }

        em.close();
        emf.close();
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}

账户.java

package com.suplink.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

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

    @Id @GeneratedValue
    private long id;
    private String email;
    private String password;

    @OneToMany(mappedBy="account")
    private Collection<Account> accounts = new ArrayList <Account> ();

    public Account() {
    }

    public long getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }


}

持久性.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence 
    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"
    version="2.0">
    <persistence-unit name="jpa-suplink" transaction-type="RESOURCE_LOCAL">
        <properties>
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
            <property name="hibernate.connection.url" value="jdbc:mysql://127.0.0.1/suplink"/>
            <property name="hibernate.connection.username" value="root"/>
            <property name="hibernate.connection.password" value=""/>

            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
            <property name="hibernate.hbm2ddl.auto" value="update" />
        </properties>
    </persistence-unit>
</persistence>
4

1 回答 1

0

在以下代码mappedBy引用实体中命名account的属性:Account

@OneToMany(mappedBy="account")
private Collection<Account> accounts = new ArrayList <Account> ();

它告诉字段namedaccount拥有双向关系,但目前没有这种关系的反面。通过添加它来解决问题:

@ManyToOne
private Account account;

其他选择是将其保持为单向关系。这是通过删除 mappedBy 属性来完成的。

于 2013-01-26T20:20:48.797 回答