JPA:通过交叉外国价值找到结果

时间:2017-06-05 20:06:56

标签: java jpa

这是我的数据库

   CREATE TABLE Orders (
        OrderID int NOT NULL,
        OrderNumber int NOT NULL,
        products_id int,
        PRIMARY KEY (OrderID),
        FOREIGN KEY (products_id) REFERENCES product(products_id)
    );

****我生成了NetBeans实体类,它提供了以下代码; ****

package Entity;

import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;

@Entity
@Table(name = "orders")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Orders.findAll", query = "SELECT o FROM Orders o")
    , @NamedQuery(name = "Orders.findByOrderID", query = "SELECT o FROM Orders o WHERE o.orderID = :orderID")
    , @NamedQuery(name = "Orders.findByOrderNumber", query = "SELECT o FROM Orders o WHERE o.orderNumber = :orderNumber")})
public class Orders implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "OrderID")
    private Integer orderID;
    @Basic(optional = false)
    @Column(name = "OrderNumber")
    private int orderNumber;

    public Orders() {
    }

    public Orders(Integer orderID) {
        this.orderID = orderID;
    }

    public Orders(Integer orderID, int orderNumber) {
        this.orderID = orderID;
        this.orderNumber = orderNumber;
    }

    public Integer getOrderID() {
        return orderID;
    }

    public void setOrderID(Integer orderID) {
        this.orderID = orderID;
    }

    public int getOrderNumber() {
        return orderNumber;
    }

    public void setOrderNumber(int orderNumber) {
        this.orderNumber = orderNumber;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (orderID != null ? orderID.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {

        if (!(object instanceof Orders)) {
            return false;
        }
        Orders other = (Orders) object;
        if ((this.orderID == null && other.orderID != null) || (this.orderID != null && !this.orderID.equals(other.orderID))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "Entity.Orders[ orderID=" + orderID + " ]";
    }

}

****我是JPA的新手,我只想获得" OrderNumber"输入" products_id"可能吗?** 我想要这样的东西**

public Orders findOrderNumber(int productsId) {
        EntityManager em = getEntityManager();
        Orders order= null;
        try {
         order= (Orders) em.createQuery("SELECT o FROM Orders o WHERE o.productsId=" + productsId).getSingleResult();


        } catch (NoResultException e) {
            order= null;
        }

        return order;
    }

2 个答案:

答案 0 :(得分:1)

试试这段代码:

// Retrieve operation using the JPA Entity Manager
order = em.find(Orders.class,productsId);

// get the orderNumber from an instance of Entity Orders
int orderNumber = order.getOrderNumber();

答案 1 :(得分:-1)

你应该使用准备好的声明。

How to use prepared statement in JPA

现在您的查询容易受到SQL注入攻击:https://www.owasp.org/index.php/SQL_Injection

现在到底有什么工作?