在WebSphere中使用JPA从数据库中选择所有行 [英] selecting all rows from a database using JPA in WebSphere

查看:76
本文介绍了在WebSphere中使用JPA从数据库中选择所有行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一个使用开放JPA访问数据层的Web服务.我正在使用Websphere v7.0和JPA 2.0.该服务将使所有行都从一个小数据库中删除(大约6行,将来完全不会扩展).我试图获取所有行并通过用户返回它们.我现在正在创建将检索数据的会话Bean.

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.

我有几个JPA对象,其中一个(代表我要返回的所有数据的一行)看起来像这样...

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;
}

里面有很多东西,但是我不想把它弄得太长.我只是以为我会显示几个有用的变量,也许还有一些获取集. 在我的会话bean中,我试图获取所有行...

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;    
}

到目前为止,我所掌握的绝对不是鼻烟.但是在线教程从来没有描述如何从表中获取所有行.我没有此方法的任何参数,因此我将无法基于ID或类似的东西进行选择.任何建议都很好.

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.

推荐答案

您可以使用NamedQuery

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

在会话bean中:

return em.createNamedQuery("EmailDomainTrust.getEmailDomains", EmailDomainTrust.class).getResultList();

这篇关于在WebSphere中使用JPA从数据库中选择所有行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆