使用put方法更新jpa中的实体

时间:2017-05-31 06:58:10

标签: java mysql hibernate rest jpa

如何使用put方法更新jpa中的实体?

我正在使用以下JSON进行更新:

{
    "acceptedGender":"both",
    "price":123123.00,
    "type":"apartment",
    "vacantNum":13,
    "hadID":4
}

我使用下面的put方法进行更新是否正确?

有人可以帮助我

这是我的代码:

@javax.ejb.Stateless
@Path("com.balay.entity.hatypes")
public class HaTypesFacadeREST extends AbstractFacade<HaTypes> {

    @PersistenceContext(unitName = "BalayRSPU")
    private EntityManager em;

    public HaTypesFacadeREST() {
        super(HaTypes.class);
    }

    **@POST
    @Override
    @Consumes({MediaType.APPLICATION_JSON})
    public void create(HaTypes entity) {
        em = Persistence.createEntityManagerFactory("BalayRSPU").createEntityManager();
        try{
          em.getTransaction().begin();
          em.persist(entity);
          em.getTransaction().commit();
       }finally{
          em.close();
       }
    }**

@PUT
@Path("{id}")
@Consumes({MediaType.APPLICATION_JSON})
public void edit(@PathParam("id") Integer id, HaTypes entity) {
            em = 
          Persistence.createEntityManagerFactory("BalayRSPU").
          createEntityManager();
      try{
        em.getTransaction().begin();
         Admin update = em.getReference(Hatypes.class, id);
      update = entity;
      em.persist(update);
        em.getTransaction().commit();
    }finally{
        em.close();
    }
}

@DELETE
@Path("{id}")
public void remove(@PathParam("id") Integer id) {
    super.remove(super.find(id));
}

@GET
@Path("{id}")
@Produces({MediaType.APPLICATION_JSON})
public HaTypes find(@PathParam("id") Integer id) {
    return super.find(id);
}

@GET
@Override
@Produces({MediaType.APPLICATION_JSON})
public List<HaTypes> findAll() {
    return super.findAll();
}

@GET
@Path("{from}/{to}")
@Produces({MediaType.APPLICATION_JSON})
public List<HaTypes> findRange(@PathParam("from") Integer from, 
@PathParam("to") Integer to) {
    return super.findRange(new int[]{from, to});
}

@GET
@Path("count")
@Produces(MediaType.TEXT_PLAIN)
public String countREST() {
    return String.valueOf(super.count());
}

@Override
protected EntityManager getEntityManager() {
    em = 
Persistence.createEntityManagerFactory("BalayRSPU").createEntityManager();
    return em;
}

}
 @Entity
 @Table(name = "ha_types")
 @XmlRootElement
 @NamedQueries({
@NamedQuery(name = "HaTypes.findAll", query = "SELECT h FROM HaTypes h")
, @NamedQuery(name = "HaTypes.findByHatID", query = "SELECT h FROM HaTypes h 
WHERE h.hatID = :hatID")
, @NamedQuery(name = "HaTypes.findByType", query = "SELECT h FROM HaTypes h 
 WHERE h.type = :type")
, @NamedQuery(name = "HaTypes.findByAcceptedGender", query = "SELECT h FROM 
HaTypes h WHERE h.acceptedGender = :acceptedGender")
, @NamedQuery(name = "HaTypes.findByVacantNum", query = "SELECT h FROM 
HaTypes h WHERE h.vacantNum = :vacantNum")
, @NamedQuery(name = "HaTypes.findByPrice", query = "SELECT h FROM HaTypes h 
WHERE h.price = :price")})
public class HaTypes implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "hatID")
private Integer hatID;
@Basic(optional = false)
@Column(name = "type")
private String type;
@Basic(optional = false)
@Column(name = "acceptedGender")
private String acceptedGender;
@Basic(optional = false)
@Column(name = "vacantNum")
private int vacantNum;
// @Max(value=?)  @Min(value=?)//if you know range of your decimal fields 
consider using these annotations to enforce field validation
@Basic(optional = false)
@Column(name = "price")
private BigDecimal price;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "hatID")
private Collection<Reservation> reservationCollection;
@JoinColumn(name = "hadID", referencedColumnName = "hadID")
@ManyToOne(optional = false)
private HaDetails hadID;

public HaTypes() {
}

public HaTypes(Integer hatID) {
    this.hatID = hatID;
}

public HaTypes(Integer hatID, String type, String acceptedGender, int 
vacantNum, BigDecimal price) {
    this.hatID = hatID;
    this.type = type;
    this.acceptedGender = acceptedGender;
    this.vacantNum = vacantNum;
    this.price = price;
}

public Integer getHatID() {
    return hatID;
}

public void setHatID(Integer hatID) {
    this.hatID = hatID;
}

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}

public String getAcceptedGender() {
    return acceptedGender;
}

public void setAcceptedGender(String acceptedGender) {
    this.acceptedGender = acceptedGender;
}

public int getVacantNum() {
    return vacantNum;
}

public void setVacantNum(int vacantNum) {
    this.vacantNum = vacantNum;
}

public BigDecimal getPrice() {
    return price;
}

public void setPrice(BigDecimal price) {
    this.price = price;
}

@XmlTransient
public Collection<Reservation> getReservationCollection() {
    return reservationCollection;
}

public void setReservationCollection(Collection<Reservation> 
reservationCollection) {
    this.reservationCollection = reservationCollection;
}

public HaDetails getHadID() {
    return hadID;
}

public void setHadID(HaDetails hadID) {
    this.hadID = hadID;
}

@Override
public int hashCode() {
    int hash = 0;
    hash += (hatID != null ? hatID.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 HaTypes)) {
        return false;
    }
    HaTypes other = (HaTypes) object;
    if ((this.hatID == null && other.hatID != null) || (this.hatID != null 
    && !this.hatID.equals(other.hatID))) {
        return false;
    }
    return true;
}

@Override
public String toString() {
    return "com.balay.entity.HaTypes[ hatID=" + hatID + " ]";
}

}
@Entity
@Table(name = "ha_details")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "HaDetails.findAll", query = "SELECT h FROM HaDetails h")
, @NamedQuery(name = "HaDetails.findByHadID", query = "SELECT h FROM 
 HaDetails h WHERE h.hadID = :hadID")
, @NamedQuery(name = "HaDetails.findByBusinessName", query = "SELECT h FROM 
 HaDetails h WHERE h.businessName = :businessName")
, @NamedQuery(name = "HaDetails.findByContactNum", query = "SELECT h FROM 
 HaDetails h WHERE h.contactNum = :contactNum")
, @NamedQuery(name = "HaDetails.findByBusinessAddress", query = "SELECT h 
 FROM HaDetails h WHERE h.businessAddress = :businessAddress")
, @NamedQuery(name = "HaDetails.findByStatus", query = "SELECT h FROM 
 HaDetails h WHERE h.status = :status")
, @NamedQuery(name = "HaDetails.findByRegDate", query = "SELECT h FROM 
 HaDetails h WHERE h.regDate = :regDate")
, @NamedQuery(name = "HaDetails.findByRemarks", query = "SELECT h FROM 
 HaDetails h WHERE h.remarks = :remarks")
, @NamedQuery(name = "HaDetails.findByLongitude", query = "SELECT h FROM 
 HaDetails h WHERE h.longitude = :longitude")
, @NamedQuery(name = "HaDetails.findByLatitude", query = "SELECT h FROM 
 HaDetails h WHERE h.latitude = :latitude")})
 public class HaDetails implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "hadID")
private Integer hadID;
@Basic(optional = false)
@Column(name = "businessName")
private String businessName;
@Basic(optional = false)
@Column(name = "contactNum")
private String contactNum;
@Basic(optional = false)
@Column(name = "businessAddress")
private String businessAddress;
@Basic(optional = false)
@Column(name = "status")
private String status;
@Basic(optional = false)
@Column(name = "regDate")
@Temporal(TemporalType.TIMESTAMP)
private Date regDate;
@Column(name = "remarks")
private String remarks;
@Basic(optional = false)
@Column(name = "longitude")
private String longitude;
@Basic(optional = false)
@Column(name = "latitude")
private String latitude;
@JoinColumn(name = "landlordID", referencedColumnName = "landlordID")
@ManyToOne(optional = false)
private Landlord landlordID;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "hadID")
private Collection<HaImages> haImagesCollection;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "hadID")
private Collection<HaAmenities> haAmenitiesCollection;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "hadID")
private Collection<HaTypes> haTypesCollection;
@OneToMany(mappedBy = "hadID")
private Collection<Tenant> tenantCollection;

public HaDetails() {
}

public HaDetails(Integer hadID) {
    this.hadID = hadID;
}

public HaDetails(Integer hadID, String businessName, String contactNum, String businessAddress, String status, Date regDate, String longitude, String latitude) {
    this.hadID = hadID;
    this.businessName = businessName;
    this.contactNum = contactNum;
    this.businessAddress = businessAddress;
    this.status = status;
    this.regDate = regDate;
    this.longitude = longitude;
    this.latitude = latitude;
}

public Integer getHadID() {
    return hadID;
}

public void setHadID(Integer hadID) {
    this.hadID = hadID;
}

public String getBusinessName() {
    return businessName;
}

public void setBusinessName(String businessName) {
    this.businessName = businessName;
}

public String getContactNum() {
    return contactNum;
}

public void setContactNum(String contactNum) {
    this.contactNum = contactNum;
}

public String getBusinessAddress() {
    return businessAddress;
}

public void setBusinessAddress(String businessAddress) {
    this.businessAddress = businessAddress;
}

public String getStatus() {
    return status;
}

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

public Date getRegDate() {
    return regDate;
}

public void setRegDate(Date regDate) {
    this.regDate = regDate;
}

public String getRemarks() {
    return remarks;
}

public void setRemarks(String remarks) {
    this.remarks = remarks;
}

public String getLongitude() {
    return longitude;
}

public void setLongitude(String longitude) {
    this.longitude = longitude;
}

public String getLatitude() {
    return latitude;
}

public void setLatitude(String latitude) {
    this.latitude = latitude;
}

public Landlord getLandlordID() {
    return landlordID;
}

public void setLandlordID(Landlord landlordID) {
    this.landlordID = landlordID;
}

@XmlTransient
public Collection<HaImages> getHaImagesCollection() {
    return haImagesCollection;
}

public void setHaImagesCollection(Collection<HaImages> haImagesCollection) {
    this.haImagesCollection = haImagesCollection;
}

@XmlTransient
public Collection<HaAmenities> getHaAmenitiesCollection() {
    return haAmenitiesCollection;
}

public void setHaAmenitiesCollection(Collection<HaAmenities> 
haAmenitiesCollection) {
    this.haAmenitiesCollection = haAmenitiesCollection;
}

@XmlTransient
public Collection<HaTypes> getHaTypesCollection() {
    return haTypesCollection;
}

public void setHaTypesCollection(Collection<HaTypes> haTypesCollection) {
    this.haTypesCollection = haTypesCollection;
}

@XmlTransient
public Collection<Tenant> getTenantCollection() {
    return tenantCollection;
}

public void setTenantCollection(Collection<Tenant> tenantCollection) {
    this.tenantCollection = tenantCollection;
}

@Override
public int hashCode() {
    int hash = 0;
    hash += (hadID != null ? hadID.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 HaDetails)) {
        return false;
    }
    HaDetails other = (HaDetails) object;
    if ((this.hadID == null && other.hadID != null) || (this.hadID != null 
    && !this.hadID.equals(other.hadID))) {
        return false;
    }
    return true;
}

@Override
public String toString() {
    return "com.balay.entity.HaDetails[ hadID=" + hadID + " ]";
}

}

0 个答案:

没有答案