我是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);
}
}
有什么想法吗?感谢。
答案 0 :(得分:1)
对于只读查询,每个看起来都很好,但是,这还不足以证明任何事情。我会通过在persistence.xml
:
<property name="eclipselink.logging.level" value="FINEST" />
<property name="eclipselink.logging.level.sql" value="FINEST" />
除其他外,这将记录SQL请求。尝试使用完全相同连接字符串和凭据在基于JDBC的SQL客户端(例如在Eclipse或Squirrel中)执行它。还要检查整个日志中是否有任何意外情况
答案 1 :(得分:1)
罗斯
我们在6月23日发布的EclipseLink 2.1(Helios)中增强了我们的元数据处理,以解决一些区分大小写的问题。
如果您指定了表名,它将在生成的SQL中使用您的大小写。
如果在生成的SQL中需要引号,则可以将表名指定为:
@Table(名称= “\” 文档\ “”)
道格