Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

selecting all rows from a database using JPA in WebSphere

I am trying to implement a web service that uses open JPA to access the data layer. I am using websphere v7.0 and JPA 2.0. This service is going to get all rows out of a small db (about 6 rows and it won't expand much at all in the future). I am attempting to get all rows and return them through the user. I am right now creating the Session Bean that will retrieve the data.

I have several JPA objects one of them (representing a row of all the data I want to return) looks like so...

@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 is a lot more in there, but I don't want to make this too long. I just thought I would show a couple usefull variables and maybe some get sets. In my session bean I am trying 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;    
}

What I have so far is definitely not up to snuff. But the tutorials online never describe getting all rows out of a table. I won't have any parameters for this method, so I won't be able to select based on ID or anything like that. Any advice would be great.

like image 289
SoftwareSavant Avatar asked Mar 20 '12 19:03

SoftwareSavant


People also ask

What is JPQL for retrieving all rows from a table?

Java Persistence Query language It is used to create queries against entities to store in a relational database. JPQL is developed based on SQL syntax. But it won't affect the database directly. JPQL can retrieve information or data using SELECT clause, can do bulk updates using UPDATE clause and DELETE clause.

What are three methods to execute queries in JPA?

There are three basic types of JPA Queries:Query, written in Java Persistence Query Language (JPQL) syntax. NativeQuery, written in plain SQL syntax. Criteria API Query, constructed programmatically via different methods.

Which methods should be used for pagination with JPQL?

Solution: With JPA and Hibernate, you have to set the pagination information on the Query interface and not in the query String as you know it from SQL. You can do that by calling the setFirstResult(int startPosition) and setMaxResults(int maxResults) methods.


1 Answers

You can use NamedQuery

@NamedQueries({
@NamedQuery(name="EmailDomainTrust.getEmailDomains",
    query="SELECT e FROM EmailDomainTrust e")          
})

in session bean:

return em.createNamedQuery("EmailDomainTrust.getEmailDomains", EmailDomainTrust.class).getResultList();
like image 139
Andrey Avatar answered Jan 30 '23 02:01

Andrey