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.