Closed susimsek closed 4 years ago
Please see the query(FabricQuery) method at IFabricCommonService, you can do like this:
IFabricObjectService.query(FabricQuery.build("your type", "username", "test"))
Hello, I tried the code you wrote. list object in findAll method returns empty. how can i solve this problem?
@Repository @RequiredArgsConstructor public class UserRepositoryImpl implements UserRepository {
private final ObjectMapper objectMapper;
private final IFabricObjectService fabricService;
private final IFabricCommonService iFabricCommonService;
private final ModelMapper modelMapper;
@Override
public List<User> findAll() {
FabricQuery fabricQuery=new FabricQuery();
fabricQuery.setType(ModelConstant.USER_TYPE);
FabricObject test=new FabricObject();
test.setId("00001");
test.setType("user");
Map<String,Object> values=new HashMap();
values.put("id","00001");
values.put("username","test");
values.put("email","test58.gmail.com");
test.setValues(values);
iFabricCommonService.create(test);
FabricQueryResponse<List<FabricObject>> list=iFabricCommonService.query(FabricQuery.build("user", "username", "test"));
List<FabricObject> fabricObjectList=fabricService.extQuery(fabricQuery);
Assert.notNull(fabricObjectList, "fabric object list must not be null!");
List<User> userList=new ArrayList<>();
fabricObjectList.forEach(fabricObject -> userList.add(convertFabricObjectToUser(fabricObject)));
return userList;
}
private FabricObject convertUserToFabricObject(User user){
FabricObject fabricObject=new FabricObject();
fabricObject.setId(user.getId());
fabricObject.setType(user.getType());
Map<String, Object> values = objectMapper.convertValue(user, new TypeReference<Map<String, Object>>() {});
fabricObject.setValues(values);
return fabricObject;
}
private User convertFabricObjectToUser(FabricObject fabricObject){
Map<String, Object> values = fabricObject.getValues();
User user = objectMapper.convertValue(values, User.class);
return user;
}
}
In fact:
FabricObject test=new FabricObject();
test.setId("00001");
test.setType("user");
Map<String,Object> values=new HashMap();
values.put("id","00001");
values.put("username","test");
values.put("email","test58.gmail.com");
will be stored at fabric chain as following:
{
"key": "generated key",
"type": "user",
"values": {
"id": "0001",
"username": "test",
"email": "test58.gmail.com"
}
}
So that, you should create your query like this:
FabricQuery fabricQuery=new FabricQuery();
fabricQuery.setType(ModelConstant.USER_TYPE);
JsonObject json = new JsonObject();
json.addProperty("username", "test");
query.equals("values", json);
//then do query
FabricQueryResponse<List<FabricObject>> list= iFabricCommonService.query(fabricQuery);
it worked,thanks.
Hi,i am using fabric explorer framework. I wrote a code like below and I created this fabric object in hyperledger fabric. I want to search for fabric objects assigned "test" in username using Fabric Query. how can i do this in fabric explorer?
Map<String,Object> values=new HashMap(); values.put("id","00000"); values.put("username","test"); values.put("email","test58.gmail.com");
fabricObject.setValues(values);