selecting all rows from a database using JPA in WebSphere

不问归期 提交于 2019-12-06 19:02:51

问题


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.


回答1:


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();



回答2:


By inline query

List<EmailDomainTrust> ListOfEmailDomains = entityManager.createQuery("SELECT e FROM EmailDomainTrust e").getResultList();

By named query (by Andrey and mprabhat)

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();

With the query API (gleaned from Criteria Query API)

CriteriaQuery<EmailDomainTrust> criteria = em.getCriteriaBuilder().createQuery(EmailDomainTrust.class);
    criteria.select(criteria.from(EmailDomainTrust.class));
    List<EmailDomainTrust> ListOfEmailDomains = em.createQuery(criteria).getResultList();
    return ListOfEmailDomains;


来源:https://stackoverflow.com/questions/9793252/selecting-all-rows-from-a-database-using-jpa-in-websphere

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!