6

早上好

我使用 Eclipse 链接和 JPA,我想测试一个小的本地查询,但我有一个错误。你能帮帮我吗,我在这个网站和其他网站上搜索过,但没有找到解决方案。谢谢你。

实体类

package entites;

import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author Minoucha
 */
@Entity
@Table(name = "protocol")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Protocol.findAll", query = "SELECT p FROM Protocol p"),
    @NamedQuery(name = "Protocol.findByProtocolid", query = "SELECT p FROM Protocol p WHERE p.protocolid = :protocolid"),
    @NamedQuery(name = "Protocol.findByProtocolname", query = "SELECT p FROM Protocol p WHERE p.protocolname = :protocolname")})
public class Protocol implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "protocolid")
    private Integer protocolid;
    @Size(max = 64)
    @Column(name = "protocolname")
    private String protocolname;

    public Protocol() {
    }

    public Protocol(Integer protocolid) {
        this.protocolid = protocolid;
    }

    public Integer getProtocolid() {
        return protocolid;
    }

    public void setProtocolid(Integer protocolid) {
        this.protocolid = protocolid;
    }

    public String getProtocolname() {
        return protocolname;
    }

    public void setProtocolname(String protocolname) {
        this.protocolname = protocolname;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (protocolid != null ? protocolid.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Protocol)) {
            return false;
        }
        Protocol other = (Protocol) object;
        if ((this.protocolid == null && other.protocolid != null) || (this.protocolid != null && !this.protocolid.equals(other.protocolid))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "entites.Protocol[ protocolid=" + protocolid + " ]";
    }

}

主班

package main;

import java.util.Iterator;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
 *
 * @author Minoucha
 */
public class Main {


        /**
         * @param args
         */
        public static void main(String[] args) {
// TODO Auto-generated method stub

            EntityManagerFactory emf = Persistence.createEntityManagerFactory("TestRestLast3PU");
            EntityManager em = emf.createEntityManager();
            try {
                EntityTransaction entr = em.getTransaction();
                entr.begin();
                Query query = em.createNativeQuery("SELECT protocolname FROM protocol");
                List stList = query.getResultList();
                Iterator stIterator = stList.iterator();
                while (stIterator.hasNext()) {
                    System.out.print("pname:" + stIterator.next());
                    System.out.println();
                }
                entr.commit();
            } finally {
                em.close();
            }
        }
    }

错误 :

run:
avr. 16, 2013 12:34:24 PM org.hibernate.validator.internal.util.Version <clinit>
INFO: HV000001: Hibernate Validator 4.3.0.Final
[EL Warning]: 2013-04-16 12:34:24.756--ServerSession(8630548)--PersistenceUnitInfo TestTP3RestPU has transactionType RESOURCE_LOCAL and therefore jtaDataSource will be ignored
[EL Info]: 2013-04-16 12:34:24.776--ServerSession(8630548)--EclipseLink, version: Eclipse Persistence Services - 2.3.2.v20111125-r10461
[EL Severe]: 2013-04-16 12:34:24.789--ServerSession(8630548)--Exception [EclipseLink-4021] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException
Exception in thread "main" javax.persistence.PersistenceException: Exception [EclipseLink-4021] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException
Exception Description: Unable to acquire a connection from driver [null], user [null] and URL [null].  Verify that you have set the expected driver class and URL.  Check your login, persistence.xml or sessions.xml resource.  The jdbc.driver property should be set to a class that is compatible with your database platform
Exception Description: Unable to acquire a connection from driver [null], user [null] and URL [null].  Verify that you have set the expected driver class and URL.  Check your login, persistence.xml or sessions.xml resource.  The jdbc.driver property should be set to a class that is compatible with your database platform
    at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:517)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getDatabaseSession(EntityManagerFactoryDelegate.java:188)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.createEntityManagerImpl(EntityManagerFactoryDelegate.java:277)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:294)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:272)
    at main.Main.main(Main.java:23)
Caused by: Exception [EclipseLink-4021] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException
Exception Description: Unable to acquire a connection from driver [null], user [null] and URL [null].  Verify that you have set the expected driver class and URL.  Check your login, persistence.xml or sessions.xml resource.  The jdbc.driver property should be set to a class that is compatible with your database platform
    at org.eclipse.persistence.exceptions.DatabaseException.unableToAcquireConnectionFromDriverException(DatabaseException.java:376)
    at org.eclipse.persistence.sessions.DefaultConnector.connect(DefaultConnector.java:91)
    at org.eclipse.persistence.sessions.DatasourceLogin.connectToDatasource(DatasourceLogin.java:162)
    at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.loginAndDetectDatasource(DatabaseSessionImpl.java:584)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.login(EntityManagerFactoryProvider.java:206)
    at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:488)
    ... 5 more
Java Result: 1
4

2 回答 2

3

我得到了同样的例外。

按照https://tomcat.apache.org/tomcat-7.0-doc/jndi-datasource-examples-howto.html#MySQL_DBCP_Example上的文档,如果您使用 JPA 的持久性单元,则缺少一个步骤。

像这样声明一个 persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
  <persistence-unit name="TestPU" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>java:comp/env/jdbc/TestDB</jta-data-source>
    <class>com.foo.Entity</class>
  </persistence-unit>
</persistence>

有两件事使它对我有用:

  • 在元素transaction-type="JTA"的声明中添加属性。persistence-unit
  • 预置java:comp/env/在数据源定义中。
于 2017-03-26T07:39:58.083 回答
2

这与您的本机查询不起作用无关,它与未指定 JPA/EclipseLink 如何连接到数据库有关。您的示例似乎很完整,只是您没有显示将在其中配置的 persistence.xml。在此处查看简单示例:http: //wiki.eclipse.org/EclipseLink/Examples/JPA/OutsideContainer

于 2013-04-16T14:33:04.890 回答