【问题标题】:Hibernate does not read @Column when fetching data via @Query通过@Query 获取数据时,Hibernate 不读取@Column
【发布时间】:2019-04-23 15:10:40
【问题描述】:

我创建了一个自定义方法,该方法应用@Query 注释从 SQL DB 中获取数据。该方法使用 String 和 Pageable 作为参数。该 Pageable 包含我的分页信息,例如请求的页面、出现次数限制、排序方向和属性。

每当我的 Pageable 传递一个属性名称进行排序时,Hibernate 都会使用相同的名称来查找数据库中的列,即使我在模型上注释了另一个列名称。对于示例查询或派生方法实现,我没有遇到这个问题。

我已经尝试过下面的配置,Hibernate 查找列名的方式没有任何改变。如何让 Hibernate 在 DB 上查找正确的列名?

spring.jpa.hibernate.naming.implicit- 
strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
spring.jpa.hibernate.naming.physical- 
strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

示例:如果我的 pageable 将“projectName”作为模型的属性名称,Hibernate 将查找 projectName 列,而不是模型上注释的“PROJECT_NAME”。我一直在使用 Hibernate 进行一些查询,自从我开始使用 @Query 以来,这个问题就开始发生了。

查询:

@Query(value = "SELECT * FROM dbo.DW_DOCUMENT WHERE (USER_CREATED LIKE ?1 OR 
PROJECT_NAME LIKE ?1 OR PRIORITY LIKE ?1) AND (DOCUMENT_STATUS = 'Canceled' 
OR STATUS = 'Canceled')", nativeQuery = true)
Page<Autopilot_Queue> findQuickCanceledStr(@Param("search") String search, 
Pageable page);

型号:

@PropertySource("classpath:application.properties")
@Entity
@Table(name="DW_DOCUMENT")
@Component
public class Autopilot_Queue {
@Id
@GeneratedValue
@Column(name = "ID")
private Integer id;
@Column(name = "PROJECT_NAME")
private String projectName;
@Column(name = "PRIORITY")
private String priority;
@Column(name = "DOCUMENT_TEMPLATE")
private String template;
@Column(name = "STATUS")
private String processStatus;
@Column(name = "DOCUMENT_LANGUAGE")
private String language;
@Column(name = "DOCUMENT_STATUS")
private String documentStatus;
@Column(name = "USER_CREATED")
private String user;

public Autopilot_Queue() {

}

public Autopilot_Queue(Integer id, String projName, String priority, String statusProcess, String template, String language, String statusDocument, String user) {
    super();
    this.id = id;
    this.projectName = projName;
    this.priority = priority;
    this.template = template;
    this.processStatus = statusProcess;
    this.language = language;
    this.documentStatus = statusDocument;
    this.user = user;
}

public Autopilot_Queue(int id, String projName, String priority, String statusProcess, String template, String language, String statusDocument, String user) {
    super();
    this.id = id;
    this.projectName = projName;
    this.priority = priority;
    this.template = template;
    this.processStatus = statusProcess;
    this.language = language;
    this.documentStatus = statusDocument;
    this.user = user;
}

public Integer getID() {
    return id;
}

public void setID(Integer id) {
    this.id = id;
}

public String getProjectName() {
    return projectName;
}

public void setProjectName(String projectName) {
    this.projectName = projectName;
}

public String getPriority() {
    return priority;
}

public void setPriority(String priority) {
    this.priority = priority;
}

public String getProcessStatus() {
    return processStatus;
}

public void setProcessStatus(String processStatus) {
    this.processStatus = processStatus;
}

public String getLanguage() {
    return language;
}

public void setLanguage(String language) {
    this.language = language;
}

public String getDocumentStatus() {
    return documentStatus;
}

public void setDocumentStatus(String documentStatus) {
    this.documentStatus = documentStatus;
}

public String getUser() {
    return user;
}

public void setUser(String user) {
    this.user = user;
}

public String getTemplate() {
    return template;
}

public void setTemplate(String template) {
    this.template = template;
}

【问题讨论】:

    标签: java spring hibernate spring-boot


    【解决方案1】:

    您正在指定 native 查询。这本身是受支持的,但the Spring Data documentation 指出:

    Spring Data JPA 目前不支持原生动态排序 查询,因为它必须操纵实际的查询 声明,它不能可靠地为本地 SQL 做。你可以, 但是,通过指定计数使用本机查询进行分页 查询自己[...]

    (添加了重点。)因此,如果您想控制排序顺序,那么您可以选择使用 JPQL 查询,或者使用ORDER BY 子句将所需的排序顺序构建到查询中。我没有立即明白为什么前者不是所提供查询的选项,而后者当然是一个选项。

    【讨论】:

    • 但这不只是列名翻译的问题吗?例如,当我手动将 projectName 转换为“PROJECT_NAME”时,查询会正确应用排序。
    • Hibernate 只是不使用该属性名称来查找 @Column 注释以获取到数据库的相应映射。
    猜你喜欢
    • 2018-10-23
    • 1970-01-01
    • 2018-10-04
    • 1970-01-01
    • 1970-01-01
    • 2011-05-04
    • 1970-01-01
    • 1970-01-01
    • 2014-09-20
    相关资源
    最近更新 更多