I have 2 entities:
@Data
@Table("main_entities")
public class MainEntity {
@Id
private Long id;
private String anotherId;
@MappedCollection(idColumn = "main_entity_id")
private SecondEntity secondEntity;
}
@Data
@Table("second_entities")
public class SecondEntity {
@Id
private Long id;
private Long mainEntityId;
}
And exists the repository:
public interface MainEntityRepository extends CrudRepository<MainEntity, Long> {
@Query("SELECT * FROM main_entities WHERE another_id = :anotherId")
Optional<MainEntity> findByAnotherId(@Param("anotherId") String anotherId);
}
When I use the MainEntityRepository#findById(Long) - the SecondEntity is available, when I use the MainEntityRepository#findByAnotherId(String) - the SecondEntity is null
Update 2021.12.15: if set the
@MappedCollection(idColumn = "main_entity_id")
private Set<SecondEntity> secondEntities;
Its allows to get the mapped collection via MainEntityRepository#findByAnotherId(String)