0

私は単純な URL Shortener を作成しており、いくつかの Mysql 接続が必要ですが、サーバーを起動して Entity Manager を呼び出すサーブレットにログインすると、次の例外が発生しました:

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);
        }
    }
}

Account.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;
    }


}

Persistence.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指定された属性を参照しています。accountAccount

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

名前の付いたフィールドaccountが双方向の関係を所有していることを示していますが、現在、この関係の裏側はありません。問題はそれを追加することで修正されます:

@ManyToOne
private Account account;

他のオプションは、それを一方向の関係として維持することです。これは、mappedBy属性を削除することによって行われます。

于 2013-01-26T20:20:48.797 に答える