“.merge(entity)”是如何工作的?

how does ".merge(entity)" work?

我正在为模型层使用 JPA 和 EJB 创建一个 java EE(网络)应用程序。 我想我必须为 CRUD 使用会话 Bean。

这是我的 BrandFacade.java(会话 bean)

package model.business;

import model.localinterface.BrandFacadeLocal;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import model.entities.Brand;

@Stateless
public class BrandFacade extends AbstractFacade<Brand> implements BrandFacadeLocal, BrandFacadeRemote {

    @PersistenceContext(unitName = "MyWheelEE-ejbPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public BrandFacade() {
        super(Brand.class);
    }


    @Override
    public boolean CreateBrand(String name) {
        Brand brand=new Brand(0, name);
        boolean result=true;
        try {
            em.persist(brand);
        } catch (Exception e) {
            result=false;
        }  
        em.close();
        return result;
    }

    @Override
    public void deleteBrand(int brandOid) {
        em.remove(getBrandByOid(brandOid));
        em.flush();
    }

    @Override
    public Brand getBrandByOid(int brandOid) {
        em.flush();
        return em.find(Brand.class, brandOid);
    }

    @Override
    public void editBrand(Brand brand) {
        em.merge(brand);
        em.flush();
    }


}

这是我的Brand.javaclass(实体)

package model.entities;

import java.io.Serializable;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

@Entity
@Table(name = "brand")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Brand.findAll", query = "SELECT b FROM Brand b"),
    @NamedQuery(name = "Brand.findByOid", query = "SELECT b FROM Brand b WHERE b.oid = :oid"),
    @NamedQuery(name = "Brand.findByName", query = "SELECT b FROM Brand b WHERE b.name = :name")})
public class Brand implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "oid")
    private Integer oid;
    @Basic(optional = false)
    @Column(name = "name")
    private String name;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "brandOid")
    private List<Wheelchair> wheelchairList;

    public Brand() {
    }

    public Brand(Integer oid) {
        this.oid = oid;
    }

    public Brand(Integer oid, String name) {
        this.oid = oid;
        this.name = name;
    }

    public Integer getOid() {
        return oid;
    }

    public void setOid(Integer oid) {
        this.oid = oid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @XmlTransient
    public List<Wheelchair> getWheelchairList() {
        return wheelchairList;
    }

    public void setWheelchairList(List<Wheelchair> wheelchairList) {
        this.wheelchairList = wheelchairList;
    }

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

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Brand)) {
            return false;
        }
        Brand other = (Brand) object;
        if ((this.oid == null && other.oid != null) || (this.oid != null && !this.oid.equals(other.oid))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "model.entities.Brand[ oid=" + oid + " ]";
    }

}

我想知道 .merge 方法是如何工作的...我认为它在数据库中搜索具有传递的实体主键的实体,然后它在编辑字段上工作,对吗?

但是我如何编辑只知道名称的品牌?

真的很简单,这里是你的答案:

我想知道 .merge 方法是如何工作的... 当你调用 merge 方法时,JPA 将验证标记为主键(@​​Id)的字段是否不为空: - 如果是:JPA 将在您的数据库中创建一条新记录 - 它不是:JPA 将使用 id 字段值更新您的记录,例如 (UPDATE table_name ..... WHERE id=?) 所以,你是对的:)

但是我如何编辑只知道名称的品牌? 如果您想编辑知道另一个字段而不是 Id 字段的记录,您将有 2 个选项: 1. 编写 JPQL,类似于:

UPDATE Person p SET p.lastName = 'New Last Name' WHERE p.name = 'his name'
  1. 编写本机查询,在这种情况下,您将编写 PLAIN SQL 和 运行 it

在这两种情况下,您都需要执行以下操作:

Query query = em.createQuery or em.createNativeQuery

然后直接执行