JPA query equals between two properties

青春壹個敷衍的年華 提交于 2019-12-24 14:57:54

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!