現在の状況
ページネーションを実装しようとしていますが、次のような現象に頭を抱えています。
mysqlコンソールでは正常にクエリが実行されるのに
実際のアプリケーションではSQLGrammarExceptionが発生する。
以下コード:
Java
1 2 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 3 EntityManager em = DBUtil.createEntityManager(); 4 5 User login_user = (User)request.getSession().getAttribute("login_user"); 6 7 int page = 1; 8 9 try { 10 page = Integer.parseInt(request.getParameter("page")); 11 } catch (NumberFormatException e) {} 12 13 String getOrderItems = "SELECT * FROM items INNER JOIN cart ON items.id = cart.item_id AND cart.user_id = :user_id AND cart.cart_status = 2"; 14 List<Item> order_items = em.createNativeQuery(getOrderItems, Item.class) 15 .setParameter("user_id", login_user.getId()) 16 .setFirstResult(5 * (page - 1)) 17 .setMaxResults(5) 18 .getResultList(); 19 20 String countOrderItems = "SELECT COUNT(*) FROM items INNER JOIN cart ON items.id = cart.item_id AND cart.user_id = :user_id AND cart.cart_status = 2"; 21 long order_count = (long)em.createNativeQuery(countOrderItems, Item.class) 22 .setParameter("user_id", login_user.getId()) 23 .getSingleResult(); 24 25 em.close(); 26 27 request.setAttribute("order_items", order_items); 28 request.setAttribute("page", page); 29 request.setAttribute("order_count", order_count); 30 31 RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/views/users/history.jsp"); 32 rd.forward(request, response); 33 } 34} 35
beans
1@Table(name = "items") 2@NamedQueries({ 3 @NamedQuery( 4 name = "getAllItems", 5 query = "SELECT i FROM Item AS i ORDER BY i.id ASC" 6 ), 7 @NamedQuery( 8 name = "getCategoryItems", 9 query = "SELECT i FROM Item AS i WHERE i.category_id = :category_id ORDER BY i.id ASC" 10 ), 11 @NamedQuery( 12 name = "searchItems", 13 query = "SELECT i FROM Item AS i WHERE i.name LIKE :query" 14 ) 15}) 16 17@Entity 18public class Item { 19 @Id 20 @Column(name = "id") 21 private Integer id; 22 23 @Column(name = "name", nullable = false) 24 private String name; 25 26 @Column(name = "price", nullable = false) 27 private Integer price; 28 29 @Column(name = "image_pass", nullable = false) 30 private String image_pass; 31 32 @Column(name = "show_image_pass", nullable = false) 33 private String show_image_pass; 34 35 @Column(name = "category_id", nullable = false) 36 private Integer category_id;
beans
1@NamedQueries({ 2 @NamedQuery( 3 name = "checkRegisterdEmail", 4 query = "SELECT COUNT(u) FROM User AS u WHERE u.email = :email" 5 ), 6 @NamedQuery( 7 name = "checkLoginCode", 8 query = "SELECT u FROM User AS u WHERE u.email = :email AND u.password = :password" 9 ) 10}) 11@Table(name = "users") 12 13@Entity 14public class User { 15 @Id 16 @Column(name = "id") 17 @GeneratedValue(strategy = GenerationType.IDENTITY) 18 private Integer id; 19 20 @Column(name = "name", length = 12, nullable = false) 21 private String name; 22 23 @Column(name = "address", nullable = false) 24 private String address; 25 26 @Column(name = "email", nullable = false, unique = true) 27 private String email; 28 29 @Column(name = "password", nullable = false) 30 private String password; 31}
発生しているエラー :
HTTPステータス 500 - org.hibernate.exception.SQLGrammarException: could not execute query
type 例外レポート
メッセージ org.hibernate.exception.SQLGrammarException: could not execute query
例外
javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute query
org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:149)
org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:157)
org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1423)
org.hibernate.query.internal.AbstractProducedQuery.getSingleResult(AbstractProducedQuery.java:1463)
controllers.users.UsersHistoryServlet.doGet(UsersHistoryServlet.java:57)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
filters.EncodingFilter.doFilter(EncodingFilter.java:43)
原因
org.hibernate.exception.SQLGrammarException: could not execute query
org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:106)
org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:111)
org.hibernate.loader.Loader.doList(Loader.java:2695)
org.hibernate.loader.Loader.doList(Loader.java:2675)
org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2507)
org.hibernate.loader.Loader.list(Loader.java:2502)
org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:335)
org.hibernate.internal.SessionImpl.listCustomQuery(SessionImpl.java:2162)
org.hibernate.internal.AbstractSharedSessionContract.list(AbstractSharedSessionContract.java:1016)
org.hibernate.query.internal.NativeQueryImpl.doList(NativeQueryImpl.java:152)
org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1414)
org.hibernate.query.internal.AbstractProducedQuery.getSingleResult(AbstractProducedQuery.java:1463)
controllers.users.UsersHistoryServlet.doGet(UsersHistoryServlet.java:57)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
filters.EncodingFilter.doFilter(EncodingFilter.java:43)
原因
java.sql.SQLException: Column 'id' not found.
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:965)
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:898)
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:887)
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:861)
com.mysql.jdbc.ResultSetImpl.findColumn(ResultSetImpl.java:1080)
com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2570)
org.hibernate.type.descriptor.sql.IntegerTypeDescriptor$2.doExtract(IntegerTypeDescriptor.java:62)
org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:47)
org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:261)
org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:257)
org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:247)
org.hibernate.type.AbstractStandardBasicType.hydrate(AbstractStandardBasicType.java:333)
org.hibernate.loader.Loader.extractKeysFromResultSet(Loader.java:794)
org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:719)
org.hibernate.loader.Loader.processResultSet(Loader.java:991)
org.hibernate.loader.Loader.doQuery(Loader.java:949)
org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:341)
org.hibernate.loader.Loader.doList(Loader.java:2692)
org.hibernate.loader.Loader.doList(Loader.java:2675)
org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2507)
org.hibernate.loader.Loader.list(Loader.java:2502)
org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:335)
org.hibernate.internal.SessionImpl.listCustomQuery(SessionImpl.java:2162)
org.hibernate.internal.AbstractSharedSessionContract.list(AbstractSharedSessionContract.java:1016)
org.hibernate.query.internal.NativeQueryImpl.doList(NativeQueryImpl.java:152)
org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1414)
org.hibernate.query.internal.AbstractProducedQuery.getSingleResult(AbstractProducedQuery.java:1463)
controllers.users.UsersHistoryServlet.doGet(UsersHistoryServlet.java:57)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
filters.EncodingFilter.doFilter(EncodingFilter.java:43)
宜しくお願い致します。
追記: テーブルの定義
itemsテーブル
+-----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(100) | NO | | NULL | |
| price | int(11) | NO | | NULL | |
| image_pass | varchar(100) | NO | | NULL | |
| show_image_pass | varchar(100) | NO | | NULL | |
| category_id | int(11) | NO | | NULL | |
+-----------------+--------------+------+-----+---------+----------------+
cartテーブル
+-------------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+----------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| cart_status | int(11) | NO | | 0 | |
| item_id | int(11) | NO | | NULL | |
| order_at | datetime | YES | | NULL | |
| order_num | int(11) | YES | | NULL | |
| user_id | int(11) | NO | | NULL | |
+-------------+----------+------+-----+---------+----------------+