users@glassfish.java.net

Problem: How do I get already existing data from my database?

From: <glassfish_at_javadesktop.org>
Date: Sun, 24 Jun 2007 00:32:36 PDT

Hi,

I'm new to the Java Persistence API and I'm trying to get the GlassFish JPA implementation working on a Tomcat installation. My persistence.xml file is looking pretty much like the example from https://glassfish.dev.java.net/javaee5/persistence/entity-persistence-support.html.
I'm using an Oracle XE database where I have already some tables filled with data.
So my question now is: How can I read this data from the database?
I for example have a table called "CREDITCARDTYPES" which has a corresponding Entity called "CreditCardType" with only one field. I then tried the following:

EntityManager em = emf.createEntityManager();
String queryString = "SELECT c FROM CreditCardType c";
Query query = em.createQuery(queryString);
List result = query.getResultList();
System.out.println("Java Persistence query: " + queryString + " returns " + result.size() + " elements");

And there I always get a 0-size list back.
I then tried to insert a new CreditCardType like that:

EntityTransaction tx = em.getTransaction();
tx.begin();
CreditCardType c = new CreditCardType();
c.setCardType("ABC");
em.persist(c);
tx.commit();

And when I then execute the upper code again my list contains one element. I checked that the database table was updated and now contains that new entry which is alright (my table already had 2 other entries, so it now has 3). But what about the data that was already in the database table (these 2 other entries)? How do I get this? I thought that this should be available as well by calling query.getResultList().

Thank you very much for your help.
[Message sent by forum member 'm_r_c_l_x' (m_r_c_l_x)]

http://forums.java.net/jive/thread.jspa?messageID=223684