簡體   English   中英

在休眠的Spring Data JPA中顯示錯誤

[英]Show erros in hibernate spring-data jpa

我正在使用spring數據jpa和hibernate在spring mvc項目中工作,我正在使用在擴展JpaRepository的接口中聲明的本機查詢,如下所示:

public interface I_Table_one extends JpaRepository<table_id, Long>{

    @Query(value ="select name_att, .... .... various att"
            + "from Table_one "
            + "where id_table = 4 ", 
           nativeQuery = true)
    public List<Table_one_Mapped_Class>serachInTable();

這種方法在很大程度上並不過是一個例子,我在選擇中有80個屬性,我只是想知道有一種方法可以知道我錯了哪個列名。

我的映射類:

@Entity
@Table(name="Table_one")
public class Table_one_Mapped_Class implements Serializable {

    private static final long serialVersionUID = 1L;

////ID///////
                @Id
                @Column (name="ID_TABLE", nullable=false)
                private Long idInMyTable;
////ID///////
 .....other columns

這是我的休眠屬性配置:

jpaProperties.put("hibernate.dialect", "org.hibernate.dialect.Oracle10gDialect");
           jpaProperties.put("hibernate.format_sql", true);
           jpaProperties.put("hibernate.ejb.naming_strategy", "org.hibernate.cfg.ImprovedNamingStrategy");
           jpaProperties.put("hibernate.show_sql", true);

當我嘗試在類似${value}的html頁面中從控制器類中打印值時,出現以下錯誤

我在控制台中收到此錯誤:

警告:org.hibernate.engine.jdbc.spi.SqlExceptionHelper-SQL錯誤:17006,SQLState:99999
錯誤:org.hibernate.engine.jdbc.spi.SqlExceptionHelper-無效的列名

當我嘗試打開頁面時出現此錯誤

請求處理失敗; 嵌套的異常是org.springframework.orm.jpa.JpaSystemException:org.hibernate.exception.GenericJDBCException:無法執行查詢; 嵌套的異常是javax.persistence.PersistenceException:org.hibernate.exception.GenericJDBCException:無法執行查詢

在控制台中說:

無效的列名

但是我怎么知道哪個列的名稱無效,在接口方法中輸入的查詢中的名稱還是映射類中屬性的無效名稱

我已經看到了類似的問題,還有誰張貼了我需要選擇select中的所有列才能工作的信息,但是為什么我只需要select中的幾個列,我就需要選擇所有列,即使不需要需要他們嗎?

如果您使用的Spring Data JPA> 1.4,則可以創建一個DTO類,其中包含表中所需的確切屬性。

示例DTO和-Query看起來像這樣,然后:

查詢:

@Query("select new de.mypackage.mymodel.dto.myDTO(name_Att, att2,, att3)" +
        " from Table_One " +
        "where ...")
List<myDTO> getAttributesFromBigTable(String name_Attribute, 
                String attribute2, String attribute3);

DTO:

    public class myDTO {

    private String name_attr;
    private String attr2;
    private String attr3;

    public myDTO(String name_attr, String attr2, String attr3) {
        this.name_attr = name_attr;
        this.attr2 = attr2;
        this.attr3 = attr3;
    }

    public String getName_Attr() {
       return name_attr;
    }
...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM