gpt4 book ai didi

java - JPA/EclipseLink 未返回任何结果

转载 作者:行者123 更新时间:2023-11-30 09:54:50 26 4
gpt4 key购买 nike

我是 Java 和 JPA 的新手。我正在尝试连接到数据库并从表中返回一些结果,但是当我运行查询时,结果是一个空列表,即使该表有超过 100,000 行。

这是我的 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_1_0.xsd"
version="1.0">

<persistence-unit name="tDocc-PG" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<!--<exclude-unlisted-classes>false</exclude-unlisted-classes>-->
<properties>
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://172.16.100.111/tDoccTEST" />
<property name="javax.persistence.jdbc.user" value="REMOVED" />
<property name="javax.persistence.jdbc.password" value="REMOVED" />
</properties>
</persistence-unit>

这是我的实体:

package com.tdocc.core.entities;

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@Table(name="Documents")
public class Document {
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
private int documentID;
private String name;
private int folderID;
private String hash;
private String link;
private String fileType;
private long fileSize;
private String owner;
private String modifier;
private String description;
private String referenceNumber;
@Temporal(TemporalType.DATE)
private Date dateAdded;
@Temporal(TemporalType.DATE)
private Date dateModified;
private boolean readOnly;
private int superseded;
private int category;
private String ocr;
private boolean deleted;

public int getDocumentID() {
return documentID;
}
public void setDocumentID(int documentID) {
this.documentID = documentID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getOwner() {
return owner;
}
public void setOwner(String owner) {
this.owner = owner;
}
}

这是我用来运行查询的测试类:

package com.tdocc.core.tests;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import com.tdocc.core.entities.Document;

import junit.framework.TestCase;

public class DocumentTest extends TestCase {
public void testConnection() {
EntityManagerFactory factory = Persistence.createEntityManagerFactory("tDocc-PG");
EntityManager em = factory.createEntityManager();
Query q = em.createQuery("SELECT d FROM Document d");
q.setMaxResults(20);

List<Document> documents = q.getResultList();
for (Document doc : documents) {
System.out.println(doc.getName());
}

assertTrue(true);
}
}

有什么想法吗?谢谢。

最佳答案

每个看起来都适合只读查询,但是,这还不足以证明任何事情。我将通过在 persistence.xml 中设置以下属性来激活一些日志记录:

  <property name="eclipselink.logging.level" value="FINEST" />
<property name="eclipselink.logging.level.sql" value="FINEST" />

除其他事项外,这将记录 SQL 请求。尝试使用完全相同 连接字符串和凭据在基于 JDBC 的 SQL 客户端(例如 Eclipse 或 Squirrel)中执行它。还要检查整个日志是否有任何意外。

关于java - JPA/EclipseLink 未返回任何结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3096814/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com