在hibernate spring-data jpa中显示错误 [英] Show erros in hibernate spring-data jpa

查看:1642
本文介绍了在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在选择中的属性我只知道一个是有办法知道我的错误列名称。
$ b $ p我的映射类:

  @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 ///////
.....其他栏目

这是我的hibernate属性配置:

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

当我尝试从我的控制器类的HTML页面中打印一个值时,出现以下错误像这样 $ {value}



我在控制台中得到这个错误:


WARN: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:无法执行查询

在控制台中说:


无效的列名称

我知道哪个列有一个无效的名称,在查询中是否存在于接口方法中,或者是我的映射类中的某个属性的名称是否无效



我有看到类似的问题,并发布什么人,我需要选择所有列中的选择,以便工作,但为什么我只需要在我的选择几列,我需要选择所有的列,即使不需要他们如果你使用的是Spring Data JPA> 1.4,你可以创建一个DTO-Class,其中包含你需要的属性从你的桌子上。



示例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;
}
...
}


I'm working in a spring mvc project using spring data jpa and hibernate, I am using native queries that I declare in a interface that extends JpaRepository like this:

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

This method doesn't have to much sense is just a example, I have one that have like 80 attributes in the select I just one to know is there is a way to know what column name I have wrong.

My mapped class:

@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

This is my hibernate properties configuration:

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

and I get the following error when i try to print a value from my controller class in a html page like this ${value}

I get this error in the console :

WARN : org.hibernate.engine.jdbc.spi.SqlExceptionHelper - SQL Error: 17006, SQLState: 99999
ERROR: org.hibernate.engine.jdbc.spi.SqlExceptionHelper - Invalid Column Name

and this error when I try to open the page

Request processing failed; nested exception is org.springframework.orm.jpa.JpaSystemException: org.hibernate.exception.GenericJDBCException: could not execute query; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: could not execute query

In the console says:

invalid column name

but how can I know what column have a invalid name, is in the query that I put in the interface method, or is a invalid name of an attribute in my mapped class

I have seen similar problems and what person posted that I need to select ALL the columns in the select in order to work, but why I only need a few column in my select do I need to select all of the column even if don't need them?

解决方案

If you're using Spring Data JPA >1.4, you could create a DTO-Class containing exactly the attributes you need from your table(s).

A sample-DTO and -Query would look like this, then:

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

The 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;
    }
...
}

这篇关于在hibernate spring-data jpa中显示错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆