How do I Stream<GroupJson> and entire database using JPA?

Viewed 397

I am trying to stream in an entire database (about 22,000 records) via Spring JPA. Using the FindAll() method I can get them in, but they are all brought into memory at once. I want to stream them.

I have tried streamAll():

@Repository
public interface GroupJsonRepository extends CrudRepository<GroupJson, String> {
    Stream<GroupJson> streamAll();
}

but I get a bizarre error:

No property streamAll found for type GroupJson!

My object is:

@Entity
@Table(name = "GroupJson")
public class GroupJson {

    @Id
    private String id;

    private String hash;
    private String jsonData;
    private ZonedDateTime lastUpdatedTimeStamp;
...

Is there another repository I can use that does this? I can only find CrudRepository. OR, is there some other magic JPA key words that work? I am using Spring boot 1.5.9 and I am streaming data elsewhere, but I am using a custom call:

Stream<Authority> findByPartitionKey(Long partitionKey);
2 Answers

You can use query if too,

@Query("select gj from Customer gj")
Stream<GroupJson> streamAll();

You have to include the "By" part in the method declaration to enable Spring Data to parse your method name. Thats the reason why you get your strange error. Spring Data interprets streamAll as a property in your entity.

@Repository
public interface GroupJsonRepository extends CrudRepository<GroupJson, String> {
    Stream<GroupJson> streamAllBy();
}
Related