gpt4 book ai didi

java - 在 HIbernate 中调用存储过程时找不到列 'id'

转载 作者:行者123 更新时间:2023-11-29 05:56:20 25 4
gpt4 key购买 nike

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

Hibernate: call getUserData(?,?) Mar 04, 2018 12:06:18 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions WARN: SQL Error: 0, SQLState: S0022

Mar 04, 2018 12:06:18 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions ERROR: Column 'id' not found.

Error occured while executing SP : org.hibernate.exception.SQLGrammarException: could not execute query

我没有在我的存储过程中提到 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 (Stored Procedure)

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,那么它会解决问题,但随后它会显示其他字段的错误。

最佳答案

引用这个有助于理解addEntity()方法的概念

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

关于java - 在 HIbernate 中调用存储过程时找不到列 'id',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49088189/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com