Spring Data Couchbase / Spring Boot:无法获得对象的嵌套列表

时间:2017-09-13 05:32:57

标签: spring-boot couchbase

我是couchbase的新手,我遇到了从couchbase获取文档的问题。嵌套的对象列表(设备列表)始终为null。我使用了Spring Boot和Spring Data Couchbase。

模型(User.java)

@Document
public class User implements Serializable {
   @Id
   private String id;

   @Field
   @NotNull
   private String username;

   @Field
   @NotNull
   private String password;

   @Field
   private List<Devices> deviceList;

   /** getter and setter here **/
}

模型(Devices.java)

@Document
public class Devices implements Serializable {
   @Field
   @NotNull
   private String deviceId;

   @Field
   @NotNull
   private String status;

   @Field
   @NotNull
   private String createdDate;

   @Reference
   private User user;

   /** getter and setter here **/
}

存储库

@Repository
@Transactional
@N1qlPrimaryIndexed
public interface UserRepository extends CouchbaseRepository<User, String>{

   User findOneByUsername(String username);

   User findOneByDeviceId(String deviceId);
}

文档

{
   "username": "username",
   "password": "password",
   "devicesList": [
    {
      "deviceId": "abc123",
      "status": "deactived",
      "createdDate": "2017-07-28 15:59:13"
    },
    {
      "deviceId": "abc456",
      "status": "actived",
      "createdDate": "2017-07-28 15:59:13"
    },
    {
      "deviceId": "abc789",
      "status": "deactived",
      "createdDate": "2017-07-28 15:59:13"
    }
  ]
}

结果

{
   "responseCode": 200,
   "data": {
      "id": "user-login-03",
      "username": "username",
      "password": "password",
      "deviceList": null
   }
}

我希望你能帮我解决这个问题。提前谢谢

1 个答案:

答案 0 :(得分:0)

在您的课程中,属性名称为 deviceList ,但在json中, devicesList 。 由于属性名称不同,持久化的json无法转换为POJO。