Arun
Arun

Reputation: 3680

Get List of Entity Objects with Input List : Spring Data JPA - Hibernate

I have a List of APIObjects List<APIObjects> apiObjectList as an Input to my API (through HTTP-Post), I need to check all the parameters within this Object exists in my Database

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;
 }

APIObject.java

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

In my Repository Interface i have a method like this

List<DatabaseObject> findByGroupIdAndArtifactIdAndVersionIn (List<APIobject> apiList);

But i get this exception

Caused by: java.lang.IllegalArgumentException: No parameter available for part artifactId SIMPLE_PROPERTY (1): [Is, Equals].

How to write a Spring-data-JPA method for this ? All the three parameters should match (groupid, artifactid and version)

Upvotes: 0

Views: 6553

Answers (2)

Yaroslav
Yaroslav

Reputation: 118

Try using Query by Example technique. In your case the solution may look like this:

DatabaseObject dbObject = new DatabaseObject(apiObject.getGroupId(), ...);
Iterable<DatabaseObject> dbObjects = DatabaseObjectRepository.findAll(Example.of(dbObject));

Upvotes: 0

Boris
Boris

Reputation: 24443

If you want to create a query from method names, you should change the repository method:

List<DatabaseObject> findByGroupIdAndArtifactIdAndVersion(String groupId, String artifactId, String version);

Or with In keyword:

List<DatabaseObject> findByAPIobjectIn(Collection<APIObject> apis)

See Query Creation for more details.

Upvotes: 1

Related Questions