Skip to content

feat: add LOCATE predicate for JPA entity attributes annotated with @Convert #115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 22, 2019

Conversation

igdianov
Copy link
Collaborator

@igdianov igdianov commented Apr 22, 2019

Fixes #112

This PR adds support for LOCATE predicate for JPA entity class attributes annotated with @Convert, i.e.

Given entity class with JsonNode attribute:

@Entity(name = "JsonEntity")
@Table(name = "json_entity")
@Data
public class JsonEntity {
 
    @Id
    private int id;
 
    private String firstName;
 
    private String lastName;
 
    @Convert(converter = JsonNodeConverter.class)
    @Column(columnDefinition = "text")
    private JsonNode attributes;
    
}

And JsonNodeConverter implementation:

public class JsonNodeConverter implements AttributeConverter<JsonNode, String> {
    private final static Logger logger = LoggerFactory.getLogger(JsonNodeConverter.class);
    
    private static final ObjectMapper objectMapper = new ObjectMapper()
                                                            .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    @Override
    public String convertToDatabaseColumn(JsonNode jsonMap) {
 
        String jsonString = null;
        try {
            jsonString = objectMapper.writeValueAsString(jsonMap);
        } catch (final JsonProcessingException e) {
            logger.error("JSON writing error", e);
        }
 
        return jsonString;
    }
 
    @Override
    public JsonNode convertToEntityAttribute(String jsonString) {
 
        JsonNode jsonMap = null;
        try {
            jsonMap = objectMapper.readValue(jsonString, JsonNode.class);
            
        } catch (final IOException e) {
            logger.error("JSON reading error", e);
        }
 
        return jsonMap;
    }
 
}

With attributes json string data stored in the record:

-- Json entity
insert into json_entity (id, first_name, last_name, attributes) values
	(1, 'john', 'doe', '{"attr":{"key":["1","2","3","4","5"]}}'),
	(2, 'joe', 'smith', '{"attr":["1","2","3","4","5"]}');

When the following query is executed:

query {
    JsonEntities(where: { 
       attributes: {LOCATE: "key"} 
    }) {
      select {
      id
      firstName
      lastName
      attributes
    }
  }
}

The expected result:

{
   "JsonEntities":{
      "select":[
         {
            "id":1,
            "firstName":"john",
            "lastName":"doe",
            "attributes":{
               "attr":{
                  "key":[ "1", "2", "3", "4", "5"]
               }
            }
         }
      ]
   }
}

@igdianov igdianov self-assigned this Apr 22, 2019
@codecov
Copy link

codecov bot commented Apr 22, 2019

Codecov Report

Merging #115 into master will increase coverage by 0.56%.
The diff coverage is 91.66%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master     #115      +/-   ##
============================================
+ Coverage     65.81%   66.37%   +0.56%     
- Complexity      368      372       +4     
============================================
  Files            37       37              
  Lines          2024     2034      +10     
  Branches        299      302       +3     
============================================
+ Hits           1332     1350      +18     
+ Misses          562      552      -10     
- Partials        130      132       +2
Impacted Files Coverage Δ Complexity Δ
...ry/schema/impl/GraphQLJpaOneToManyDataFetcher.java 73.68% <ø> (-0.46%) 13 <0> (ø)
...graphql/jpa/query/schema/impl/PredicateFilter.java 96.96% <100%> (+0.09%) 4 <0> (ø) ⬇️
...jpa/query/schema/impl/GraphQLJpaSchemaBuilder.java 86.83% <100%> (+0.19%) 105 <0> (+2) ⬆️
...hql/jpa/query/schema/impl/JpaPredicateBuilder.java 46.63% <50%> (+0.51%) 42 <0> (+1) ⬆️
...ventures/graphql/jpa/query/schema/JavaScalars.java 36.32% <0%> (+3.41%) 4% <0%> (+1%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1955ab6...934872e. Read the comment docs.

@igdianov igdianov changed the title feat: add LOCATE predicate for attributes annotated with @Convert feat: add LOCATE predicate for JPA entity attributes annotated with @Convert Apr 22, 2019
@igdianov igdianov merged commit 3f9704b into master Apr 22, 2019
@igdianov igdianov deleted the feat-112-locate-predicate branch April 22, 2019 20:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add query support for fields with @Convert
1 participant