0

I have Location and I want to retrieve all locations in db by company

I can do this in this way List<Location> findByCompanyId(Long companyId);

but How can I retrieve ids of Locations not whole object

 List<Long> findByCompanyId(Long companyId);

I want the same thing as in first method but without whole object only ids

public class Location extends BaseEntity {

@Column(name = "`street_first`", nullable = false)
private String streetFirst;

@Column(name = "`street_second`")
private String streetSecond;

@Column(name = "`city`", nullable = false)
private String city;

@Column(name = "`zip`")
private String zip;

@Column(name = "`state`", nullable = false)
private String state;

@Column(name = "`phone`", nullable = false)
private String phone;

@JoinColumn(name = "`company_id`", nullable = false)
private Company company;
}
//getters and setters
roki
  • 348
  • 3
  • 15
  • @NicolasLabrot nope he is trying to retrieve two columns and using query annotation. in contrast I am looking for solution which will not use query annotation. – roki Apr 06 '17 at 07:20
  • is there any projection or whatever? – roki Apr 06 '17 at 07:20
  • The related question and your try to achieve the same goal: projection on a N columns using spring data jpa. Sorry but cf. Robert Niestroj there is no other option than `@Query` annotation – Nicolas Labrot Apr 06 '17 at 07:23

1 Answers1

1
@Query("SELECT l.id FROM Location l WHERE l.company.id = :companyId");
List<Long> getLocationIds(@Param("companyId")Long companyId);
Robert Niestroj
  • 15,299
  • 14
  • 76
  • 119