无法在联系人实体中将位置模型设置为空
Not able to set location model to null in contact entity
我正在开发一个 spring mvc 应用程序,其中有 2 个模型 classes。以下是我的模型 classes:
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;
}
}
和位置模型
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;
}
}
关于删除位置,我想将相应联系人的位置设置为空。我为此使用以下代码:
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 class 中有用于 locationId 的 getter 方法 class。
如何将 null 设置为基本类型?对实体中的字段使用包装器类型通常是一种很好的做法。
@Id
@Column(name="locationid")
@GeneratedValue
private Integer locationId;
您在查询的where LocationModel 中设置并将其与int 进行比较。应该是
Query query = session.createQuery("update ContactModel set locationModel=:newLocation where locationModel.id=:locationId");
相反。或者传递 LocationModel 实例而不是 id
我正在开发一个 spring mvc 应用程序,其中有 2 个模型 classes。以下是我的模型 classes:
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;
}
}
和位置模型
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;
}
}
关于删除位置,我想将相应联系人的位置设置为空。我为此使用以下代码:
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 class 中有用于 locationId 的 getter 方法 class。
如何将 null 设置为基本类型?对实体中的字段使用包装器类型通常是一种很好的做法。
@Id
@Column(name="locationid")
@GeneratedValue
private Integer locationId;
您在查询的where LocationModel 中设置并将其与int 进行比较。应该是
Query query = session.createQuery("update ContactModel set locationModel=:newLocation where locationModel.id=:locationId");
相反。或者传递 LocationModel 实例而不是 id