Using JPA in WebSphere to select all rows from the database
I try to implement a web service that uses open JPA to access the data layer I use WebSphere v7.0 0 and JPA 2.0 This service will enable all rows from a small database (about 6 rows, which will not expand much in the future) I try to get all the rows and return them through the user I am now creating a session bean that will retrieve data
I have several JPA objects, one of which (a row representing all the data I want to return) looks like
@Entity @NamedQueries({ @NamedQuery(name="EmailDomainTrust.getEmailDomains",query="SELECT DOMAIN_NAME,"+ "DESCRIPTION,CONFIRMED_BY,CONFIRMED_DATE" + "FROM EMAIL_DOMAIN_TRUST") }) @Table(name="EMAIL_DOMAIN_TRUST") public class EmailDomainTrust implements Serializable { @Id @Column(name="EMAIL_DOMAIN_TRUST_ID") private long emailDomainTrustId; @Column(name="DOMAIN_NAME") private String domainName; }
There's more there, but I don't want it to last long I just thought I'd show some useful variables, maybe some get sets In my session bean, I try to get all the rows
public List<EmailDomainTrust> GetEmailDomains(){ EntityManagerFactory emf = Persistence.createEntityManagerFactory(""); EntityManager em = emf.createEntityManager(); //EntityTransaction userTransaction = em.getTransaction(); System.out.println("Testing 1..2...3...!"); List<EmailDomainTrust> ListOfEmailDomains = em.find(EmailDomainTrust.class,arg1) try { } catch(Exception e) { } return null; }
So far, I have absolutely no snuff However, the online tutorial never describes getting all rows from a table I won't have any parameters for this method, so I won't be able to select by ID or any similar Any suggestion would be good
Solution
You can use namedquery
@NamedQueries({ @NamedQuery(name="EmailDomainTrust.getEmailDomains",query="SELECT e FROM EmailDomainTrust e") })
In the session bean:
return em.createNamedQuery("EmailDomainTrust.getEmailDomains",EmailDomainTrust.class).getResultList();