首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >通过“wither`”方法进行属性填充

通过“wither`”方法进行属性填充
EN

Stack Overflow用户
提问于 2019-01-15 18:24:09
回答 1查看 923关注 0票数 4

在使用不可变对象时,Property Population并没有像我期望的那样工作。

我尝试遵循spring-data-couchbase-3.1.4.RELEASE reference docsProperty Population部分,它建议我可以传入一个不可变的实体,并使用适当的@wither方法来设置id。但是,当我尝试这样做时,返回的实体仍然具有ID的null值。

com/example/demospringdatacouchbaseapp/model/Car.java

代码语言:javascript
运行
复制
import static org.springframework.data.couchbase.core.mapping.id.GenerationStrategy.USE_ATTRIBUTES;
import org.springframework.data.couchbase.core.mapping.Document;
import org.springframework.data.couchbase.core.mapping.id.GeneratedValue;
import org.springframework.data.couchbase.core.mapping.id.IdAttribute;
import com.couchbase.client.java.repository.annotation.Field;
import com.couchbase.client.java.repository.annotation.Id;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Value;
import lombok.experimental.Wither;

@Value
@AllArgsConstructor
@Builder(toBuilder=true)
@Document
public class Car {

  public static final String ID_DELIMITER = ".";

  @Id
  @GeneratedValue(strategy = USE_ATTRIBUTES, delimiter = ID_DELIMITER)
  @Wither
  String id;

  @Field
  @IdAttribute(order=0)
  String manufacturer;

  @Field
  @IdAttribute(order=1)
  String model;

  @Field
  @IdAttribute(order=2)
  String spec;

  @Field
  String colour;

}

com/example/demospringdatacouchbaseapp/repository/CarRepository.java

代码语言:javascript
运行
复制
import java.util.Collection;
import org.springframework.data.couchbase.repository.CouchbaseRepository;
import com.example.demospringdatacouchbaseapp.model.Car;

public interface CarRepository extends CouchbaseRepository<Car, String> {

  Collection<Car> findByColour(String colour);

}

Create simple car test

代码语言:javascript
运行
复制
  @Test
  public void createSingCarTest() {

    /*
     * Given
     */

    Car givenCar = createMadWeeClio();

    /*
     * When
     */

    Car persistedCar = repository.save(givenCar);

    /*
     * Then
     */

    assertThat(persistedCar).isEqualTo(givenCar.withId(getExpectedId(givenCar)));

  }

  ...

  private String getExpectedId(Car givenCar) {
    return givenCar.getManufacturer() + Car.ID_DELIMITER + givenCar.getModel() + Car.ID_DELIMITER
        + givenCar.getSpec();
  }

  private Car withExpectedId(Car car) {
    return car.withId(getExpectedId(car));
  }

  private Car createMadWeeClio() {
    return Car.builder().manufacturer("RenaultSport").model("Clio").spec("200 Cup").colour("white")
        .build();
  }

  private Car createMadMeg() {
    return Car.builder().manufacturer("RenaultSport").model("Megane").spec("R.S Trophy")
        .colour("Yellow").build();
  }

我希望CouchbaseRepository::save操作返回一个不可变实体对象的新实例,其中填充了自动生成的ID属性。然而,在我的测试中,它以null的形式返回。

传入可变实体会导致id字段按预期设置。我还可以看到在couchbase中填充了ID字段。

EN

回答 1

Stack Overflow用户

发布于 2019-01-16 18:34:17

我认为问题在于save操作并不是创建Car对象的新实例-相反,它会尝试将id设置到对象上(但不能,因为它是不可变的)。

@Value Lombok注释产生的方法如下所示

代码语言:javascript
运行
复制
Car withId(String id) {
    return new Car(id, this.model, this.spec, this.colour);
}

但是当你save你的对象时,这个方法不会被调用。

当您从数据库检索对象时,将调用withId方法。因此,如果您从数据库中返回所有对象(或使用其他条件搜索,如model),您将看到它们实际上确实具有带值的id属性。文档在这里介绍了这一点:

https://docs.spring.io/spring-data/couchbase/docs/3.1.4.RELEASE/reference/html/#mapping.property-population

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54196925

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档