我有2个实体和投影的休息库:
@Entity
public class Person {
@Id
@GeneratedValue
private Long id;
String name;
@ManyToOne
Language language;
}
@Entity
public class Language {
@Id
@GeneratedValue
private Long id;
private String name;
}
public interface PersonRepository extends PagingAndSortingRepository<Person, Long> {
}
@Projection(name = "details", types = {Person.class})
interface PersonProjection {
Long getId();
String getName();
@Value("#{target.language.name}")
String getLanguage();
}
当我尝试按语言字段对其进行排序时
/ API /人/投影=细节&安培;排序=语言
它会生成尝试按language.id
对其进行排序的SQL选择person0_.id作为我...按 language1_.id asc限制排序?
有没有办法告诉Spring Data,JPA使用不同的默认属性进行排序而不在url中明确指定它?
答案 0 :(得分:1)
配置Spring数据时,它会在实体对象上使用属性遍历。 在您的情况下,对于人员和语言,您在属性“名称”上有名称冲突。 为了解决这个问题,你的sort param应该引用正确的属性,即: 对于按语言名称排序,您的网址应为/ api / persons /?projection = details&amp; sort = language_name 按人名排序你的url参数shoud look / api / persons /?projection = details&amp; sort = person_name
答案 1 :(得分:0)
首先覆盖import Vapor
import Fluent
import Foundation
final class Store: Model {
// MARK: - Model
var id: Node?
var exists: Bool = false
var locationIDs: [Node] = [] // No idea if this is right
var name: String
init(name: String, locationIDs: [Node] = []) {
self.id = nil
self.name = name
self.locationIDs = locationIDs
}
init(node: Node, in context: Context) throws {
id = try node.extract("id")
name = try node.extract("name")
// ???
}
func makeNode(context: Context) throws -> Node {
return try Node(node: [
"id": id,
"name": name
// ???
])
}
static func prepare(_ database: Database) throws {
try database.create( "stores" ) { creator in
creator.id()
creator.string("name")
// creator.id("", optional: false) // ???
}
}
static func revert(_ database: Database) throws {
try database.delete("stores")
}
}
界面中的findAll()
方法:
PersonRepository
然后将命名查询添加到public interface PersonRepository extends PagingAndSortingRepository<Person, Long> {
@Override
@Query
public Iterable<User> findAll();
}
实体类:
User