spring-data-cassandra:在类用户上找不到要绑定构造函数的属性生成器

7d7tgy0s  于 2021-06-10  发布在  Cassandra
关注(0)|答案(1)|浏览(378)

我开始了一个基本的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上找不到绑定构造函数参数的属性生成器
如何解决这个问题?

iugsix8n

iugsix8n1#

要创建spring数据实体,必须在pojo中编写一些必需的元素,您应该有完整的参数构造函数、getter和setter。另外,请毫不犹豫地添加默认构造函数,并使用equals和hashcode方法重写tostring。这是一个使用spring data cassandra术语的实体类示例:

@Table
public class Person {

  @PrimaryKey
  private final String id;

  private final String name;
  private final int age;

  public Person(String id, String name, int age) {
    this.id = id;
    this.name = name;
    this.age = age;
  }

  public String getId() {
    return id;
  }

  public String getName() {
    return name;
  }

  public int getAge() {
    return age;
  }

  @Override
  public String toString() {
    return String.format("{ @type = %1$s, id = %2$s, name = %3$s, age = %4$d }",
      getClass().getName(), getId(), getName(), getAge());
  }
}

相关问题