简体   繁体   English

Spring data cassandra:在类 User 上找不到要绑定构造函数的属性构建器

[英]Spring data cassandra: No property builder found on class User to bind constructor to

I started a basic Spring project.我开始了一个基本的 Spring 项目。 I use Spring data Cassandra.我使用 Spring 数据 Cassandra。

My model use the builder pattern.我的模型使用构建器模式。 Here's my User class:这是我的用户类:

@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
}

I have no problem on the controller side to serialize the json from a POST request to User object.我在控制器端将 json 从 POST 请求序列化到 User 对象没有问题。 There's also no problem when I call the repo to save the object, but when I do :当我调用 repo 来保存对象时也没有问题,但是当我这样做时:

User getByUsername(String username);

Then I get the following error: No property builder found on entity class com.project.model.user.User to bind constructor parameter然后我收到以下错误:没有在实体类 com.project.model.user.User 上找到属性构建器来绑定构造函数参数

How can I solve this issue?我该如何解决这个问题?

To create Spring Data entities, there are mandatory elements that should be written in your POJO, you should have full arguments constructor, getters and setters.要创建 Spring Data 实体,必须在 POJO 中编写一些必需元素,您应该拥有完整的参数构造函数、getter 和 setter。 Also, don't hesitate to add the default constructor and to override toString with equals and hashCode methods.此外,不要犹豫,添加默认构造函数并使用 equals 和 hashCode 方法覆盖 toString。 This is an example of an Entity class using Spring Data Cassandra terminology:这是使用 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());
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “MappingException:没有在实体类java.time.OffsetDateTime上找到属性null以将构造函数参数绑定到!”与Spring MongoDB - “MappingException: No property null found on entity class java.time.OffsetDateTime to bind constructor parameter to!” with Spring MongoDB MappingException:“在实体上找不到要将构造函数参数绑定到的属性”? - MappingException: "No property found on entity to bind constructor parameter to"? 在实体类java.time.ZonedDateTime上找不到属性null以将构造函数参数绑定到 - No property null found on entity class java.time.ZonedDateTime to bind constructor parameter to 春季数据-找不到属性 - Spring Data - no property found Spring-data-cassandra找不到UserDefinedType - UserDefinedType not found by spring-data-cassandra spring-data-jdbc 错误 找不到类的必需标识符属性 - spring-data-jdbc error Required identifier property not found for class Spring boot在@SpringBootApplication类上找不到默认构造函数 - Spring boot No default constructor found on @SpringBootApplication class Spring Data Cassandra和用户定义的类型 - Spring Data Cassandra and User Defined Types Spring 启动 - 未找到用户类型的属性 ID - Spring boot - No property ID found for type User Spring 数据 JPA Class 没有构造函数的投影 - Spring Data JPA Class Projection Without Constructor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM