无法将java.lang.String字段CustomersEntity.address设置为CustomersEntity,CustomerEntity中的address字段有问题吗?

时间:2019-01-16 18:39:59

标签: java hibernate

我正在尝试通过休眠将一个新的CustomersEntity实例保存到我的数据库中。调试后,导致我的错误的确切行是:“ session.save(newCustomer)”我已经打印了此“ newCustomer”的内容,并且所有字段都存在,但ID字段除外。我很确定自己已经为冬眠正确地注释了我的ID字段,但是我可能是错的。我一直坚持这一点,非常感谢任何见解。

import org.springframework.stereotype.Indexed;
import javax.persistence.*;
import java.sql.Timestamp;
import java.util.Objects;

@Entity
@Indexed
@Table(name = "customers", schema = "customer_db", catalog = "")
    public class CustomersEntity {
    private String lastName;
    private String firstName;
    private String email;
    private String address;
    private String city;
    private String state;
    private String zip;
    private Timestamp lastEmailSent;

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int customerId;



@Basic
@Column(name = "lastName")
public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

@Basic
@Column(name = "firstName")
public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

@Basic
@Column(name = "email")
public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

@Basic
@Column(name = "address")
public String getAddress() {
    return address;
}

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

@Basic
@Column(name = "city")
public String getCity() {
    return city;
}

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

@Basic
@Column(name = "state")
public String getState() {
    return state;
}

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

@Basic
@Column(name = "zip")
public String getZip() {
    return zip;
}

public void setZip(String zip) {
    this.zip = zip;
}

@Basic
@Column(name = "lastEmailSent")
public Timestamp getLastEmailSent() {
    return lastEmailSent;
}

public void setLastEmailSent(Timestamp lastEmailSent) {
    this.lastEmailSent = lastEmailSent;
}


@Column(name = "customerId")
public int getCustomerId() {
    return customerId;
}
public void setCustomerId(int customerId) {
    this.customerId = customerId;
}


@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    CustomersEntity that = (CustomersEntity) o;
    return customerId == that.customerId &&
            Objects.equals(lastName, that.lastName) &&
            Objects.equals(firstName, that.firstName) &&
            Objects.equals(email, that.email) &&
            Objects.equals(address, that.address) &&
            Objects.equals(city, that.city) &&
            Objects.equals(state, that.state) &&
            Objects.equals(zip, that.zip) &&
            Objects.equals(lastEmailSent, that.lastEmailSent);
}

@Override
public int hashCode() {

    return Objects.hash(lastName, firstName, email, address, city, state, 
    zip, lastEmailSent, customerId);
}

}

我收到错误消息“无法将java.lang.String字段... model.CustomersEntity.address设置为... model.CustomersEntity。关于问题的任何见解?对不起,如果我的问题有触发了任何人。

2 个答案:

答案 0 :(得分:0)

听起来像在您尝试保存该实体的代码中,您是在使用实体本身而不是地址字符串来设置地址。

答案 1 :(得分:0)

我建议您在创建客户对象的地方检查控制器逻辑。另外,请确保您的休眠映射文件中的属性声明正确。