Offset Offset - 20 days ago 13
Java Question

Spring Boot findByIdIn internal list

Having trouble to create the right itnerface for my Query for this given Problem.

I have this entity:

public class TwoEntity extends BaseEntity implements Serializable {

private static final long serialVersionUID = 1L;

/*
* ATTRIBUTE
*/
private String groupName;

@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<OneEntity> oneList;


And the Crud Repositoy:

public interface TwoRepository extends CrudRepository<TwoEntity, Long> {

TwoEntityfindById(Long id);

TwoEntityfindByGroupName(String groupName);

List<TwoEntity> findBy????(OneEntity oe);


My Goal is to get All TwoEntities where OneEntitie is a element in the list of TwoEntity.
I´am using Spring boot and Hibernate to accomplish this. I cant delete the OneEntity Object from my Database because TwoEntity has OneEntity as ForeignKey in the List.

Is there anyway of get this to work with the given Tools from the Interface?
A List of available KeyWords can be found here: spring docs for crud

/E

I Guess I have a wrong Architecture. Currently I have a Unidirectional Relation between this Entities. I guess I have to make those entities bidirectional and delete them manuelle with
oneList.setList(null)
.

BUT I´m not 100% sure. Open for Input.

Answer

You could use this:

List<TwoEntity> findByOneList_Id(Long oneEntityId)

But you need to extend from JpaRepository

public interface TwoRepository extends JpaRepository<TwoEntity, Long> {

The method will be translated to

twoEntity.oneList.id

Here official doc

http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-methods.query-property-expressions