我正在使用JPA和EJB为模型层创建一个Java EE(Web)应用程序。 我想我必须使用Session Beans进行CRUD。
这是我的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.java类(实体)
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方法是如何工作的...我认为它在DB中搜索具有实体主键的实体,然后它在编辑的字段上工作吗?
但我怎么能只知道这个名字来编辑一个品牌?
答案 0 :(得分:1)
真的很简单,这里是你的答案:
我想知道.merge方法是如何工作的...... 当您调用merge方法时,JPA将验证标记为主键(@Id)的字段是否不为空: - 如果是:JPA将在您的数据库中创建新记录 - IT NOT:JPA将使用id字段值更新您的记录,例如(UPDATE table_name ..... WHERE id =?) 所以,你说得对:)
但我如何只知道名字来编辑品牌? 如果你想编辑一个知道另一个字段而不是Id字段的记录,你将有两个选择: 1.编写JPQL,如:
UPDATE Person p SET p.lastName = 'New Last Name' WHERE p.name = 'his name'
在这两种情况下,您都需要执行以下操作:
Query query = em.createQuery or em.createNativeQuery
然后执行它