收录日期:2019/06/20 01:22:12 时间:2010-06-22 20:20:11 标签:java,jpa,eclipselink

I am new to Java and JPA. I am trying to connect to a database and return some results from a table, but when I run the query, I get an empty list as a result even though the table has over 100,000 rows.

Here is my 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>    

This is my Entity:

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;
}
}

And this is my test class that I am using to run the Query:

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);
}
}

Any ideas? Thanks.

Every looks fine for a read-only query but, well, this is not enough to certify anything. I would activate a bit of logging by setting the following properties in the persistence.xml:

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

This will, amongst other things, log the SQL request. Try to execute it in a JDBC based SQL client (for example in Eclipse or Squirrel) using the exact same connection string and credentials. Also check the whole log for anything unexpected.

Ross,

We have enhanced our metadata processing in EclipseLink 2.1 (Helios) released June 23rd to address some case sensitivity issues.

If you specify the table name as you have it will use your casing in generated SQL.

If you require quotes in the generated SQL you can specify the table name as:

@Table(name="\"Documents\"")

Doug