无法在联系人实体中将位置模型设置为null

时间:2015-01-20 07:08:52

标签: java hibernate spring-mvc

我正在开发一个spring mvc app,其中我有2个模型类。以下是我的模型类:

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;

@Entity
@Table(name="Contact")
public class ContactModel {

    @Id
    @Column(name="contactid")
    @GeneratedValue
    private int contactId;

    @Column(name="contactname")
    private String contactName;

    @Column(name="contactemail")
    private String email;

    @Column(name="contactphone")
    private String phone;

    @ManyToOne
    @JoinColumn(name="locationid")
    private LocationModel locationModel;

    public LocationModel getLocationModel() {
        return locationModel;
    }
    public void setLocationModel(LocationModel locationModel) {
        this.locationModel = locationModel;
    }
    public int getContactId() {
        return contactId;
    }
    public void setContactId(int contactId) {
        this.contactId = contactId;
    }
    public String getContactName() {
        return contactName;
    }
    public void setContactName(String contactName) {
        this.contactName = contactName;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
}

和LocationModel

import java.util.List;

//import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.Cascade;

@Entity
@Table(name="Location")
public class LocationModel {

    @Id
    @Column(name="locationid")
    @GeneratedValue 
    private int locationId;

    @Column(name="locationname")
    private String locationName;

    @Column(name="locationdesc")
    private String locationDescription;

    @Column(name="type")
    private String locationType;

    @Column(name="address")
    private String address;

    @Column(name="city")
    private String city;

    @Column(name="state")
    private String state;

    @Column(name="district")
    private String district;

    @Column(name="lattitude")
    private String lattitude;

    @Column(name="longitude")
    private String longitude;

    @OneToMany(mappedBy = "locationModel")
    private List<ContactModel> contactList;

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getLattitude() {
        return lattitude;
    }

    public void setLattitude(String lattitude) {
        this.lattitude = lattitude;
    }

    public String getLongitude() {
        return longitude;
    }

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

    public String getLocationType() {
        return locationType;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getDistrict() {
        return district;
    }

    public void setDistrict(String district) {
        this.district = district;
    }

    public void setLocationType(String locationType) {
        this.locationType = locationType;
    }

    public int getLocationId() {
        return locationId;
    }

    public void setLocationId(int locationId) {
        this.locationId = locationId;
    }

    public String getLocationName() {
        return locationName;
    }

    public void setLocationName(String locationName) {
        this.locationName = locationName;
    }

    public String getLocationDescription() {
        return locationDescription;
    }

    public void setLocationDescription(String locationDescription) {
        this.locationDescription = locationDescription;
    }
}

在删除位置时,我想将相应联系人的位置设置为null。我正在使用以下代码:

public void selLocationToNull(int locationId) throws Exception {
        try {
            logger.info("deleteContact() begins:");
            Session session = sessionFactory.getCurrentSession();
            Query query = session.createQuery("update ContactModel set locationModel=:newLocation where locationModel=:locationId");
            query.setParameter("newLocation", null);
            query.setParameter("locationId", locationId);
            query.executeUpdate();
            logger.info("null update query executed...");
        } catch (Exception e) {
            logger.debug("Error while updating location to null: "
                    + e.getMessage());
            throw e;
        } finally {
        }
    }

我对此感到异常:

org.hibernate.PropertyAccessException: could not get a field value by reflection getter of com.bizmerlin.scm.model.LocationModel.locationId

Caused by: java.lang.IllegalArgumentException: Can not set int field com.bizmerlin.scm.model.LocationModel.locationId to java.lang.Integer

我的LocationModel类中有locationId的getter方法。

2 个答案:

答案 0 :(得分:1)

如何将null设置为基本类型?通常,最好在实体中为字段使用包装类型。

@Id
@Column(name="locationid")
@GeneratedValue 
private Integer locationId;

答案 1 :(得分:1)

您在查询中设置了LocationModel并将其与int进行比较。应该是

Query query = session.createQuery("update ContactModel set locationModel=:newLocation where locationModel.id=:locationId");

代替。或者传递LocationModel实例而不是id