Hibernate does not read @Column when fetching data via @Query

时间:2019-04-23 15:10:40

标签: java spring hibernate spring-boot

I made a custom method that applies a @Query annotation to fetch data from a SQL DB. That method uses a String along with a Pageable as argument. That Pageable carries my paging information, such as page requested, limit of occurrences, sort direction and property.

Whenever my Pageable passes a property name for sorting, Hibernate uses that same name to look for a column on the DB, even though I annotated another column name on the model. I did not have this problem with example queries, or derived method implementations.

I have already tried the configuration below, without any change to the way Hibernate looks for the column name. How can I make Hibernate look for the correct column name on the 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

Example: If my pageable has "projectName" as property name for the model, Hibernate will look for a projectName column, and not "PROJECT_NAME", as annotated on the model. I have been using Hibernate for some queries, and this problem started happening ever since I began using @Query.

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);

Model:

@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;
}

1 个答案:

答案 0 :(得分:0)

您要指定一个本机查询。它本身受支持,但是the Spring Data documentation注意:

  

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

(添加了强调。)因此,如果要控制排序顺序,则选择要么使用JPQL查询,要么使用ORDER BY子句在查询中构建所需的排序顺序。我没有立即看到为什么前者不是显示查询的选项,而后者肯定是选项。