1

I get a List of Objects List<APIObjects> apiObjectList as an Input to my API (through HTTP-Post), I need to compare this Input-List with my List of Entity Object which i get by executing repository.findAll() with Spring-boot-data-JPA framework

Currently i loop the List<DatabaseObject> and then find if there is a match. Below is my code

public Boolean findIfAllAPIobjectsExist (List<APIObject> apiObjects) {
    List<DatabaseObject> databaseObjectsList = databaseRepository.findAll()

    return apiObjects.stream().allMatch {
        apiObject -> {
            for (DatabaseObject dbObject : databaseObjectsList) {
                if ((dbObject.getGroupId().trim().equals(dbObject.getGroupId().trim())) &&
                                (dbObject.getArtifactId().trim() .equals(dbObject.getArtifactId().trim())) &&
                                (dbObject.getVersion().trim().equals(dbObject.getVersion().trim()))) {
                            System.out.println("Matching ..");
                            return true;
                        }
                    }
                    return false;
            }
        }

    }

}

But this looping seems to consume lot of time and Memory and How can it be tackled with Lambda functions ? I am pretty sure that my current methodology shown above (looping of DatabaseObject) isn't the right or professional way to tackle it

APIObject.java

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class APIObject{
    private String groupId;
    private String artifactId;
    private String version;
}

DatabaseObject.java

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "my_table")
public class DatabaseObject {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    Long id;

    @Column(name = "name")
    String name;

    @Column(name = "group_id")
    String groupId;

    @Column(name = "artifact_id")
    String artifactId;

    @Column(name = "version")
    String version;
    }
2
  • Check this one - stackoverflow.com/a/34140736/2478531 Commented Oct 26, 2018 at 8:46
  • Is there an hibernate or spring-data-jpa way of tackling this ? Probably with an in query Commented Oct 26, 2018 at 9:24

1 Answer 1

2

I would override equals and hashCode in both of my Enties which hold the three attributes groupId, artifactId, version then sort the two List based on the three arrtibutes, and just use .equals to determine if the two Lists are equivalent or no :

databaseObjectsList.sort(
        Comparator.comparing(DatabaseObject::getGroupId)
                .thenComparing(DatabaseObject::getArtifactId)
                .thenComparing(DatabaseObject::getVersion)
);
apiObjects.sort(Comparator.comparing(APIObject::getGroupId)
        .thenComparing(APIObject::getArtifactId)
        .thenComparing(APIObject::getVersion)
);

return databaseObjectsList.equals(apiObjects);

With lombok you can use :

@EqualsAndHashCode(of = {"groupId", "artifactId", "version"})
class DatabaseObject {..}
Sign up to request clarification or add additional context in comments.

2 Comments

Is there an hibernate or spring-data-jpa way of tackling this ? Probably with an in query
@Arun really I don't know, if there are a similar way in hibernate or spring-data-jpa, but you maybe you can find a solution, I'm here if you find it :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.