早上好
我使用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
答案 0 :(得分:3)
我得到了同样的例外。
如果你正在使用JPA的持久性单元,那么在https://tomcat.apache.org/tomcat-7.0-doc/jndi-datasource-examples-howto.html#MySQL_DBCP_Example的文档后面缺少一步。
声明一个像这样的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/
。答案 1 :(得分:1)
这与您的本机查询无关,与未指定JPA / EclipseLink如何连接到数据库有关。您的示例似乎已完成,但您没有显示将在其中配置的persistence.xml。看看这里的简单示例: http://wiki.eclipse.org/EclipseLink/Examples/JPA/OutsideContainer