我开始了一个基本的spring项目。我用的是spring数据cassandra。
我的模型使用生成器模式。下面是我的用户类:
@Table("user")
@JsonDeserialize(builder = User.Builder.class)
public class User {
@PrimaryKey
public final String username;
@Column("password")
public final String password;
@Column("firstname")
public final String firstName;
@Column("lastname")
public final String lastName;
@Column("birthdate")
public final Date birthDay;
@Column("emailaddress")
public final String emailAddress;
@Column("organization")
public final String organization;
@Column("locality")
public final String locality;
@Column("stateprovince")
public final String stateProvince;
@Column("countrycode")
public final String countryCode;
@Column("disabled")
public final boolean disabled;
@Column("registrationdate")
public final Date registrationDate;
private User(Builder builder) {
username = builder.username;
password = builder.password;
firstName = builder.firstName;
lastName = builder.lastName;
birthDay = builder.birthDay;
emailAddress = builder.emailAddress;
organization = builder.organization;
locality = builder.locality;
stateProvince = builder.stateProvince;
countryCode = builder.countryCode;
disabled = builder.disabled;
registrationDate = builder.registrationDate;
}
public static Builder newBuilder() {
return new Builder();
}
public static Builder newBuilder(User copy) {
Builder builder = new Builder();
builder.username = copy.username;
builder.password = copy.password;
builder.firstName = copy.firstName;
builder.lastName = copy.lastName;
builder.birthDay = copy.birthDay;
builder.emailAddress = copy.emailAddress;
builder.organization = copy.organization;
builder.locality = copy.locality;
builder.stateProvince = copy.stateProvince;
builder.countryCode = copy.countryCode;
builder.disabled = copy.disabled;
builder.registrationDate = copy.registrationDate;
return builder;
}
public static final class Builder {
private String username;
private String password;
private String firstName;
private String lastName;
private Date birthDay;
private String emailAddress;
private String organization;
private String locality;
private String stateProvince;
private String countryCode;
private boolean disabled;
private Date registrationDate;
private Builder() {
}
public static Builder newStub() {
return new Builder()
.withUsername("stub")
.withEmailAddress("stub@unittest.com")
.withPassword("dontforgettests")
.withCountryCode("9")
.withFirstName("Someone")
.withLastName("Overarainbow");
}
public Builder withUsername(String val) {
username = val;
return this;
}
//etc
}
在控制器端,我可以将post请求中的json序列化为user对象。当我调用repo来保存对象时也没有问题,但是当我这样做时:
User getByUsername(String username);
然后我得到以下错误:在实体类com.project.model.user.user上找不到绑定构造函数参数的属性生成器
如何解决这个问题?
1条答案
按热度按时间iugsix8n1#
要创建spring数据实体,必须在pojo中编写一些必需的元素,您应该有完整的参数构造函数、getter和setter。另外,请毫不犹豫地添加默认构造函数,并使用equals和hashcode方法重写tostring。这是一个使用spring data cassandra术语的实体类示例: