在HIbernate

时间:2018-03-03 19:15:52

标签: java mysql hibernate stored-procedures hibernate-criteria

我在Mysql中创建了一个存储过程,我试图使用J2EE / Hibernate调用该存储过程。执行存储过程时,它显示错误。

  

Hibernate:调用getUserData(?,?)   2018年3月4日上午12:06:18 org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions   警告:SQL错误:0,SQLState:S0022

     

2018年3月4日上午12:06:18 org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions   错误:找不到列'id'。

     

执行SP时出错:org.hibernate.exception.SQLGrammarException:无法执行查询

我的存储过程中没有提及 id ,但仍然显示有关 id 的错误。

  

User.java

@Entity
public class User {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String firstName;
private String lastName;

public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getFirstName() {
    return firstName;
}
public void setFirstName(String firstName) {
    this.firstName = firstName;
}
public String getLastName() {
    return lastName;
}
public void setLastName(String lastName) {
    this.lastName = lastName;
}
}
  

UserDao.java

public List<User> searchUserListWithSP(String firstName,String lastName) {

    List<User> userList = new ArrayList<>();
    try{
        SessionFactory factory = HibernateUtility.getSessionFactory();
        Session session = factory.getCurrentSession();
        session.beginTransaction();

        Query query = session.createSQLQuery("call getUserData(:firstName,:lastName)")      
                .addEntity(User.class)
                .setParameter("firstName", firstName)
                .setParameter("lastName", lastName);

        userList = query.list();

        System.out.println(userList);

    } catch(Exception e) {
        System.out.println("Error occured while executing SP : "+e);
    }
    System.out.println("UserDao @searchUserListWithSP -- END");
    return userList;
}
  

getUserData.sql(存储过程)

delimiter //

drop procedure if exists getUserData//
create procedure getUserData(
in firstName varchar(50),
in lastName varchar(50)
)
begin

declare queryString text default "";

set queryString = concat(queryString, 'select u.firstName, u.lastName from user u where 1=1');

if (firstName is not null and firstName != '') then
    set queryString = concat(queryString," and firstName like '%", firstName,"%' ");
end if;

if (lastName is not null and lastName != '') then
    set queryString = concat(queryString," and lastName like '%", lastName,"%' ");
end if;

set queryString = concat(queryString, ";");

-- select queryString;
SET @SQL := queryString;
prepare stmt from @SQL;
execute stmt;
deallocate prepare stmt;

end//

因此,当从UserDao执行存储过程时,我收到此错误。我已经搜索过它,但没有发现任何具体的内容。如果我在我的存储过程select语句中添加id,那么它将解决问题但是它显示其他字段的错误。

2 个答案:

答案 0 :(得分:1)

请参阅此内容,了解addEntity()方法的概念

会很有帮助

http://www.programering.com/a/MDM0AjMwATY.html

答案 1 :(得分:1)

我找到了解决方法。使用addEntity()方法尝试将结果与对象映射,这可能就是我找不到错误列的原因。我已经删除了addEntity,并尝试先做同样的事情。

Query query = session.createSQLQuery("call getUserData(:firstName,:lastName)")      
                //.addEntity(User.class)
                .setParameter("firstName", firstName)
                .setParameter("lastName", lastName);

        userList = query.list();

        System.out.println(userList);
        //output is [[Ljava.lang.Object;@2a9a3bb0]
        //It will return List of array of object. So need to iterate according.

        for(int i=0; i<userList.size(); i++) {

            Object[] obj =  (Object[]) userList.get(i);

            for(int j=0; j<obj.length; j++) {
                System.out.println(obj[j]);
            }
        }