I have two tables Users and Post.
Entity User looks like this:
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "name")
private String name;
@Column(name = "age")
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
Entity Post looks like this:
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "name")
private String name;
@Column(name = "user_id")
private int userId;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
I created UsersRepository with @Query
public interface UsersRepository extends JpaRepository<User, Long> {
@Query("SELECT u.id, u.name, u.age FROM users AS u LEFT JOIN post AS p ON u.id=p.user_id WHERE p.user_id IS NULL")
List<User> findByStatus();
}
I get error QuerySyntaxException: users is not mapped
Can I do this without @Query? Can i do this with @OneToMany? How can I implement select on Jpa OneToMany?
I want to get all users who don't have posts.