jpa-2.0 - 无法从驱动程序 [null] 用户 [null] 和 URL [null] postgres获取连接

  显示原文与译文双语对照的内容
0 0

早安

我使用 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
时间:原作者:1个回答

0 0

这与你的原生查询没有任何作用,这与不指定 jpa/eclipselink如何连接到数据库有关。 你的示例似乎完全完成,只是你没有显示 persistence.xml 将被配置的位置。 查看下面的简单示例: http://wiki.eclipse.org/EclipseLink/Examples/JPA/OutsideContainer

原作者:
...