views:

251

answers:

2

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.

+1  A: 

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.

Pascal Thivent
I don't know if this is related, but I did try using Eclipse's database workspace to connect to my database, but it couldn't list any tables. So I just decided to not mess with Eclipse's GUI database tools and do it all in code.
Ross Peoples
Maybe the user you're using doesn't have enough rights (just in case, I use Eclipse Database support with many databases including Derby, Mysql, Oracle, etc without any problem).
Pascal Thivent
I used PostgreSQL and the 'postgres' user, which has full access to all databases, so I don't think that's the problem. I am able to connect the the same database with the same credentials using pgAdmin3, just not Eclipse Database. All I know is that it didn't list my tables after a successful connection, so my initial impression was not one of reliability. Though I really want it to work. Granted, I'm probably doing something wrong, but with next to zero documentation, there's no way to know.
Ross Peoples
@Ross I suggest trying Squirrel which is very close to jdbc (I guess activating logging didn't help...).
Pascal Thivent
I kind of "went back to the drawing board", so to speak. After looking around and finding that there is only minimal documentation for anything related to EclipseLink, I decided to pick up a book: Java Persistence with Hibernate. I am currently reading this book to get a complete understanding of JDBC, JPA, and Hibernate. I couldn't really find anything on EclipseLink, so I figure that by using Hibernate, I can go into #hibernate in Freenode and ask questions, rather than hoping someone in #java uses EclipseLink.
Ross Peoples
@Ross Hibernate has definitely a wider community. But I don't expect your code to work "better" with Hibernate, EclipseLink itself is not the problem (I'm constantly switching between Hibernate, EclipseLink and OpenJPA on a pet project with success).
Pascal Thivent
I turned on the logging and tried again. This is what I got: Canonical Metamodel class [com.tdocc.core.entities.Document_] not found during initialization.Execute query ReadAllQuery(referenceClass=Document sql="SELECT DOCUMENTID, FILETYPE, DATEMODIFIED, HASH, LINK, REFERENCENUMBER, DATEADDED, DELETED, FOLDERID, OCR, FILESIZE, MODIFIER, CATEGORY, READONLY, SUPERSEDED, DESCRIPTION, NAME, OWNER FROM Documents")
Ross Peoples
Does it matter that all the casing in the field names is different than what is stored in the database and what I wrote?
Ross Peoples
According to [the PosgresQL documentation](http://www.postgresql.org/docs/current/static/sql-syntax-lexical.html), it doesn't matter.
Pascal Thivent
Other than the above return from logging, that's it. I cannot figure out why I'm not getting any rows returned.
Ross Peoples
Does the query "work" in Squirrel?
Pascal Thivent
I can't use Squirrel SQL apparently. I reinstalled it twice and it keeps telling me that it won't load the postgresql driver it installed during the installation. The "debug" application needs some debugging before I can figure out what's going on apparently. I'm beginning to think that this whole Java thing isn't going to end well.
Ross Peoples
@Ross Then just add it manually... Don't take it bad but the problem is not Java.
Pascal Thivent
I downloaded the trial of RazorSQL and it doesn't return any results either. Wonder why pgAdmin3 and every other non-Java project can connect to my PostgreSQL database but Java stuff can't?
Ross Peoples
Ok, I actually just tried the same query in pgAdmin3 "SELECT d FROM DOCUMENTS d" and it returns nothing. If I rewrite the query to "SELECT * FROM DOCUMENTS", I get the same thing. BUT, if I write the query as "SELECT * FROM "Documents"" (surrounding the table name in quotes), it works, but only in pgAdmin3. The same query gives me syntax errors in RazorSQL. What the hell is going on?
Ross Peoples
@Ross The first query is a JPQL query, not an SQL query so it's not surprising. However, the later looks fine and `DOCUMENTS` doesn't seem to be a PostgreSQL reserved keyword... I've no idea of what's going on.
Pascal Thivent
I can't even figure out how to make this work from RazorSQL. Every query I put in there doesn't fail, it just returns zero rows.
Ross Peoples
+1  A: 

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

Doug Clarke