speedment / jpa-streamer

JPAstreamer is a lightweight library for expressing JPA queries as Java Streams
GNU Lesser General Public License v2.1
345 stars 35 forks source link

Filter based on SubEntity (ManyToOne relation) #393

Open creoludifico opened 8 months ago

creoludifico commented 8 months ago

Hi

I've not found any example on how to filter a entity based on some value inside a sub entity.

Assume this is my MainEntity

@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "MAIN_ENTITY", schema = "Q")
public class MainEntity {

    ...

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "SubEntityID", insertable = false, updatable = false)
    private SubEntity subEntity;

    ...
}

And this is some service where i'm trying to filter on the subentity

var username = "bob"

/*
   Gets the main entities (I would like the filter for the ManyToOne entity here)
*/
var entities = jpaStreamer.stream(of(MainEntity.class).joining(MainEntity$.subEntity))
        .filter(MainEntity$.username.equal(username))
        //.filter(MainEntity$.subEntity.state.notEqual("DONE")) <-Doesnt work..
        .toList();

/*
   Have to do it like this. Filter the main entities from the subEntity.. 
   This results in slower performance because it's no longer part of the SQL query
*/
var filteredEntities = new ArrayList<MainEntity>();
entities.forEach(entity -> {
    if(!(entity.getSubEntity().getState().equals("DONE"))){
        filteredEntities.add(entity);
    }
});

Is this possible in JpaStreamer? I know it's possible in Entity Framework and LINQ from C# ;)

mbuchner commented 6 months ago

@creoludifico Did you find any solution for this?

If filters on joined entities are not possible the whole framework doesn't really make sense for enterprise applications.

+1

sasanyi commented 4 months ago

The same problem here. Does anyone have a solution?