简体   繁体   English

java-如何在JPA中为电子邮件返回多个列?

[英]java- how to return multiple columns in JPA for email?

I wrote a program where I let my program endlessly pick up records from my emailqueue table so that it can process the records and send the email to outsiders.First the code is like below: 我编写了一个程序,让我的程序不断从我的emailqueue表中提取记录,以便它可以处理记录并将电子邮件发送给外部人员。首先,代码如下所示:

 MySqlConnect con=new MySqlConnect();
    public PreparedStatement preparedStatement = null;

    public Connection con1 = con.connect();



    //pick up queue and send email
    public void email() throws Exception {


        try{

            while(true) {
                String sql = "SELECT id,user,subject,recipient,content FROM emailqueue WHERE status='Pending' ";
                PreparedStatement statement = con1.prepareStatement(sql);
                ResultSet rs = statement.executeQuery();



                while (rs.next()) {

                    String subject = rs.getString("subject");

                    String recipient = rs.getString("recipient");

                    String content = rs.getString("content");

                    String id = rs.getString("id");
                    String username = rs.getString("user");

                    String emailStatus = "DONE";
                    String errormsg=sendEmail(recipient, subject, content, id,username);
                    if (!errormsg.equals("")) {
                        emailStatus = "FAILED";

                    }
                    TerminalLogger.printMsg("Status  : " + emailStatus);

                }
                statement.close();
                rs.close();

            }

As you can see,I used JDBC driver to get the value of each column in the table and passed them to sendEmail where it does it's work to send email.Sure,so far everything works flawlessly. 如您所见,我使用JDBC驱动程序来获取表中每一列的值,并将它们传递给sendEmail ,在该处可以发送电子邮件。当然,到目前为止,一切都可以正常工作。

Now,I want to achieve the same goal by using JPA because I want to learn more about it and i'm new to it.So I added a database mapping to my email table,like below: 现在,我想通过使用JPA来实现相同的目标,因为我想了解更多有关它的知识,而且我还很陌生。因此,我在电子邮件表中添加了数据库映射,如下所示:

Emailqueue.java Emailqueue.java

@Entity
@Table(name = "emailqueue")

public class Emailqueue {
    private long ulnodeid;
    private String user;
    private String subject;
    private String recipient;
    private String content;
    private String status;

    @Id
    @Column(name = "ID")
    public long getUlnodeid() {
        return ulnodeid;
    }

    public void setUlnodeid(long ulnodeid) {
        this.ulnodeid = ulnodeid;
    }

    @Basic
    @Column(name = "USER")
    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    @Basic
    @Column(name = "SUBJECT")
    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    @Basic
    @Column(name = "RECIPIENT")
    public String getRecipient() {
        return recipient;
    }

    public void setRecipient(String recipient) {
        this.recipient = recipient;
    }

    @Basic
    @Column(name = "CONTENT")
    public String  getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    @Basic
    @Column(name = "STATUS")
    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }







    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Emailqueue that = (Emailqueue) o;
        return ulnodeid == that.ulnodeid &&
                user == that.user &&
                Objects.equals(subject, that.subject) &&
                Objects.equals(recipient, that.recipient) &&
                Objects.equals(content, that.content) &&
                Objects.equals(status, that.status);
    }

    @Override
    public int hashCode() {
        return Objects.hash(ulnodeid, user,subject,recipient,content,status);
    }
}

I wrote a method to retrieve records from the table using Entity Manager,below: 我编写了一种使用实体管理器从表中检索记录的方法,如下所示:

public Emailqueue getrecords() {
        try {

            String sql = "select object(o) from Emailqueue as o where " +
                    "status='" + "Pending" + "'";

            List resList = em.createQuery(sql).getResultList();
            if (resList == null) {
                throw new Exception("Error with selection query.");
            }

            if (resList.size() > 0) {
                return (Emailqueue) resList.get(0);
            }

           // msg = "Setting <" + name + "> not found.";

            return null;
        } catch (Exception e) {

            msg = CoreUtil.wrapMsg(CoreUtil.FUNC_ERROR,
                    this.getClass().getName(), "get(" + "Pending" + ")", e.getMessage());


            return null;
        }
    }

I mainly need recipient,subject,content,id and username records so i could pass it to the method. 我主要需要recipient,subject,content,id and username记录,因此我可以将其传递给该方法。 How would I retrieve values for each row where the status is pending in this case using JPA? 在这种情况下,如何使用JPA检索状态为未决的每一行的值?

Edit: 编辑:

Using a for loop: 使用for循环:

public void email() throws Exception {


        try{

            while(true) {

                String sql = "select p.id,p.user,p.subject,p.recipient,p.content from Emailqueue p where " +
                        "status='Pending'";
                List<Emailqueue> emailList = em.createQuery(sql).getResultList();
                for (Emailqueue obj : emailList) {
                    String emailStatus = "DONE";
                    String errormsg=sendEmail(obj.getRecipient(), obj.getSubject(), obj.getContent(),obj.getUlnodeid(),obj.getUser());
                    if (!errormsg.equals("")) {
                        emailStatus = "FAILED";
                    }
                    TerminalLogger.printMsg("Status  : " + emailStatus);
                }

            }

Try replacing the query call with: 尝试将查询调用替换为:

List<Emailqueue> res = em.createQuery("SELECT e from Emailqueue e WHERE e.status='Pending'",
                                          Emailqueue.class).getResultList();

This will return a list of matching instances as Java objects (entities), which you can easily work with by calling getters/setters, for example (using the sendEmail method from your question): 这将以Java对象(实体)的形式返回匹配实例的列表,您可以轻松地通过调用getter / setter来使用它们,例如(使用问题中的sendEmail方法):

for (Emailqueue eq : res) {
   sendStatus = sendEmail(eq.getRecipient(), eq.getSubject(), eq.getContent(), eq.getId(), eq.getUsername());
}

Or, better yet, change the signature of sendEmail to sendEmail(Emailqueue eq) and call the getters in the method. 或者更好的是,将sendEmail的签名更改为sendEmail(Emailqueue eq)并在方法中调用getter。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM