Reputation: 53
I have entity class User with composite key via @IdClass annotation
@Entity
@IdClass(UserId.class)
public class User {
@Id
private String name;
@Id
private String surname;
private boolean active;
}
Composite key:
@Data
public class UserId implements Serializable {
private String name;
private String surname;
}
I use Spring Data with hibernate JPA under hood. Therefore I have repo interface:
public interface UserRepo extends JpaRepository<User, UserId> {
}
And I'd like to get all active users from db by list of concrete names and surnames. E.g. I'd like to use method like this
List<User> findAllByActiveTrue(List<UserId> ids);
How can I do query for this requirement via Spring Data or JPQL?
Upvotes: 2
Views: 715
Reputation: 18430
You can use @Embeddable
& @EmbeddedId
for composite primary key
@Embeddable
public class UserId implements Serializable {
private String name;
private String surname;
}
@Entity
public class User {
@EmbeddedId
private UserId userId;
...
}
Then query like
List<User> findByUserIdInAndActiveTrue(List<UserId> ids);
A good tutorial about @Embeddable
& @EmbeddedId
here
Upvotes: 1