问题
It is possible to compare two properties with JPA custom query ?
Registration.java :
@Entity
public class Registration implements Serializable {
@Id
@GeneratedValue(generator = "uuid")
private UUID uuid;
@ManyToOne(targetEntity=Entry.class)
private Entry entry;
@ManyToMany(targetEntity=Rate.class,fetch = FetchType.EAGER)
private List<Rate> rate;
@ManyToMany(fetch = FetchType.EAGER)
private List<Planning> planning;
}
Rate.java :
@Entity
public class Rate implements Serializable {
@Id
@GeneratedValue(generator = "uuid")
private UUID uuid;
@ManyToOne
private Activity activity;
}
Planning.java :
@Entity
public class Planning implements Serializable {
@Id
@GeneratedValue(generator = "uuid")
private UUID uuid;
@OneToOne
private Location location;
@OneToOne(fetch = FetchType.EAGER)
private Coach coach;
@OneToOne
private Activity activity;
I would retrieve only rate.activity.id = planning.activity.id with something like :
public interface RegistrationDao extends CrudRepository<Registration, UUID> {
findByPlanningAndRateActivityEqualsPlanningActivityOrderByEntryLastnameAsc(Planning planning);
}
Thanks
回答1:
Rather than having a long method name you can have a JPA Query it will be something like this. Hope this helps.
@Query("select * FROM Registration REG,Entry E, Rate R,Planning P WHERE REG.id = :id and R.activity=P.activity ORDER BY E.lastname")
public List<Registration> <MethodName>(@Param("id") String registrationid);
回答2:
I have changed for this solution but I dont think that is the good approach (where rateActivity and planningActivity have to have the same id)
public interface RegistrationDao extends CrudRepository<Registration, UUID> {
Iterable<Registration> findByPlanningAndRateActivityEqualsAndPlanningActivityEqualsOrderByEntryLastnameAsc(Planning planning,Activity rateActivity,Activity planningActivity);
}
来源:https://stackoverflow.com/questions/35989046/jpa-query-equals-between-two-properties