由于模型构造函数,如何在 Spring 启动时查询
How to query in Spring boot thanks to the model constructors
我想检索一个对象 Intervention,但我的干预被 linked 到其他对象。例如,当我只从干预中检索信息而没有使用 table 地址进行任何 link 时,它就可以正常工作。但是,我只想从 table 地址获取街道名称,但我不知道该怎么做。
提前致谢
@Repository
interface InterventionRepository : JpaRepository<Intervention, Long> {
@Query("select new Intervention(i.id, i.date, i.wishedDate, new Address(a.streetName1)) " +
"from Intervention i " +
"inner join i.address a ")
fun getInterventions(): Optional<ArrayList<Intervention>>
}
最后我自己找到了解决办法,我真的做错了。
我需要在我的构造函数中添加地址的值。我是这样工作的:
@Repository
interface InterventionRepository : JpaRepository<Intervention, Long> {
@Query("select new Intervention(i.id, i.date, i.wishedDate, a.name) " +
"from Intervention i " +
"inner join i.address a ")
fun getInterventions(): Optional<ArrayList<Intervention>>
}
在我的构造函数中我是这样做的:
constructor(interventionId: Int?, date: Date?, addressName: String?) {
this.id = interventionId
this.date = date
this.address = Address()
this.address?.name = addressName
}
我不知道这是否是最好的解决方案,但我这样做了,希望它能对其他人有所帮助。
我想检索一个对象 Intervention,但我的干预被 linked 到其他对象。例如,当我只从干预中检索信息而没有使用 table 地址进行任何 link 时,它就可以正常工作。但是,我只想从 table 地址获取街道名称,但我不知道该怎么做。
提前致谢
@Repository
interface InterventionRepository : JpaRepository<Intervention, Long> {
@Query("select new Intervention(i.id, i.date, i.wishedDate, new Address(a.streetName1)) " +
"from Intervention i " +
"inner join i.address a ")
fun getInterventions(): Optional<ArrayList<Intervention>>
}
最后我自己找到了解决办法,我真的做错了。 我需要在我的构造函数中添加地址的值。我是这样工作的:
@Repository
interface InterventionRepository : JpaRepository<Intervention, Long> {
@Query("select new Intervention(i.id, i.date, i.wishedDate, a.name) " +
"from Intervention i " +
"inner join i.address a ")
fun getInterventions(): Optional<ArrayList<Intervention>>
}
在我的构造函数中我是这样做的:
constructor(interventionId: Int?, date: Date?, addressName: String?) {
this.id = interventionId
this.date = date
this.address = Address()
this.address?.name = addressName
}
我不知道这是否是最好的解决方案,但我这样做了,希望它能对其他人有所帮助。