質問編集履歴
3
コメント削除
test
CHANGED
File without changes
|
test
CHANGED
@@ -131,11 +131,9 @@
|
|
131
131
|
RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/views/layout/index.jsp");
|
132
132
|
rd.forward(request, response);
|
133
133
|
} catch (ServletException e) {
|
134
|
-
// TODO 自動生成された catch ブロック
|
135
134
|
e.printStackTrace();
|
136
135
|
} catch (NoResultException e) {
|
137
136
|
System.out.println("ログイン社員番号未登録エラー");
|
138
|
-
// TODO 自動生成された catch ブロック
|
139
137
|
errors.add("社員番号が登録されていません");
|
140
138
|
|
141
139
|
e.printStackTrace();
|
2
スペルミス
test
CHANGED
File without changes
|
test
CHANGED
@@ -122,7 +122,7 @@
|
|
122
122
|
|
123
123
|
try {
|
124
124
|
Employees employees = (Employees) em
|
125
|
-
.createNa
|
125
|
+
.createNativeQuery("select * from employees where code =:code",Employees.class)
|
126
126
|
.setParameter("code", code)
|
127
127
|
.getSingleResult();
|
128
128
|
|
1
質問の中身、ソース
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
CreateNativequeryのエラー回避のやり方について
|
test
CHANGED
@@ -1,15 +1,17 @@
|
|
1
1
|
### 実現したいこと
|
2
2
|
JavaでWebサイトのログイン画面を開発を学習中です。
|
3
|
-
|
3
|
+
CreateNativequeryのエラー回避のやり方を教えて頂けないでしょうか?
|
4
|
-
|
5
4
|
<処理の内容>
|
6
5
|
- ログイン画面に社員番号とパスワードを入力し、DBで社員番号の照合を行う。
|
7
6
|
- 社員番号でDBを検索し、登録があれば正常ケースとして、index.jspに遷移
|
8
7
|
- 社員番号がDBに登録がなければ、loginnerro.jspに遷移
|
9
8
|
|
10
9
|
(質問内容の詳細)
|
10
|
+
ログイン画面にデータ未登録の社員番号を入力し、エラー動作の確認を行ったところ、
|
11
|
-
|
11
|
+
「IllegalArgumentException」や「NoResultException」などのエラーが出ました。
|
12
|
-
|
12
|
+
CreateNativequeryメソッドを使用する場合、データがない場合や検索コードの誤りなど
|
13
|
+
エラーが起こる場合の対処方法を教えて頂けないでしょうか?
|
14
|
+
※サイト画面の入力チェックはバリデーションとして実装済です。
|
13
15
|
|
14
16
|
### 発生している問題・エラーメッセージ
|
15
17
|
タイプ 例外報告
|
@@ -24,9 +26,16 @@
|
|
24
26
|
org.hibernate.query.internal.AbstractProducedQuery.getSingleResult(AbstractProducedQuery.java:1643)
|
25
27
|
controllers.LoginServlet.doPost(LoginServlet.java:78)
|
26
28
|
|
29
|
+
タイプ 例外報告
|
30
|
+
|
31
|
+
メッセージ No query defined for that name [select * from employees where code =:code]
|
32
|
+
|
33
|
+
説明 サーバーは予期しない条件に遭遇しました。それはリクエストの実行を妨げます。
|
34
|
+
|
35
|
+
例外
|
36
|
+
|
37
|
+
java.lang.IllegalArgumentException: No query defined for that name [select * from employees where code =:code]
|
27
38
|
### 該当のソースコード
|
28
|
-
|
29
|
-
```ここに言語名を入力
|
30
39
|
package controllers;
|
31
40
|
|
32
41
|
import java.io.IOException;
|
@@ -78,64 +87,64 @@
|
|
78
87
|
throws ServletException, IOException, NoResultException{
|
79
88
|
// TODO Auto-generated method stub
|
80
89
|
|
81
|
-
System.out.println("LoginservletPost");
|
90
|
+
System.out.println("LoginservletPost");
|
82
91
|
|
83
|
-
EntityManager em = DBUtil.createEntityManager();
|
92
|
+
EntityManager em = DBUtil.createEntityManager();
|
84
93
|
|
85
|
-
String code = request.getParameter("emp_number");
|
94
|
+
String code = request.getParameter("emp_number");
|
86
|
-
String password = request.getParameter("emp_pass");
|
95
|
+
String password = request.getParameter("emp_pass");
|
87
96
|
|
88
|
-
System.out.println("emp_number" + code);
|
97
|
+
System.out.println("emp_number" + code);
|
89
|
-
System.out.println("password" + password);
|
98
|
+
System.out.println("password" + password);
|
90
99
|
|
91
|
-
List<String> errors = new ArrayList<String>();
|
100
|
+
List<String> errors = new ArrayList<String>();
|
92
101
|
|
93
|
-
//社員番号のブランクチェック
|
102
|
+
//社員番号のブランクチェック
|
94
|
-
if (code == null || code.equals("")) {
|
103
|
+
if (code == null || code.equals("")) {
|
95
|
-
errors.add("社員番号を入力してください");
|
104
|
+
errors.add("社員番号を入力してください");
|
96
|
-
}
|
105
|
+
}
|
97
106
|
|
98
|
-
//パスワードのブランクチェック
|
107
|
+
//パスワードのブランクチェック
|
99
|
-
if (password == null || password.equals("")) {
|
108
|
+
if (password == null || password.equals("")) {
|
100
|
-
errors.add("パスワードを入力してください");
|
109
|
+
errors.add("パスワードを入力してください");
|
101
|
-
}
|
110
|
+
}
|
102
111
|
|
103
|
-
//
|
112
|
+
//入力内容にエラーがあるかどうか
|
104
|
-
|
113
|
+
if (errors.size() > 0) {
|
114
|
+
//エラーありログインエラー
|
115
|
+
System.out.println("ログイン入力エラー");
|
105
|
-
|
116
|
+
request.setAttribute("loginError", "true");
|
106
|
-
.set
|
117
|
+
request.setAttribute("errors", errors);
|
118
|
+
RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/views/error/loginerrors.jsp");
|
107
|
-
.
|
119
|
+
rd.forward(request, response);
|
120
|
+
} else {
|
121
|
+
//エラーなしログイン成功
|
108
122
|
|
109
|
-
|
123
|
+
try {
|
110
|
-
|
124
|
+
Employees employees = (Employees) em
|
111
|
-
//エラーありログインエラー
|
112
|
-
System.out.println("loginError");
|
113
|
-
re
|
125
|
+
.createNamedQuery("select * from employees where code =:code",Employees.class)
|
114
|
-
|
126
|
+
.setParameter("code", code)
|
115
|
-
RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/views/error/loginerrors.jsp");
|
116
|
-
|
127
|
+
.getSingleResult();
|
117
|
-
} else {
|
118
|
-
//エラーなしログイン成功
|
119
|
-
request.setAttribute("employees", employees);
|
120
|
-
RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/views/layout/index.jsp");
|
121
|
-
rd.forward(request, response);
|
122
128
|
|
129
|
+
System.out.println("ログイン成功");
|
130
|
+
request.setAttribute("employees", employees);
|
131
|
+
RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/views/layout/index.jsp");
|
132
|
+
rd.forward(request, response);
|
133
|
+
} catch (ServletException e) {
|
134
|
+
// TODO 自動生成された catch ブロック
|
135
|
+
e.printStackTrace();
|
136
|
+
} catch (NoResultException e) {
|
137
|
+
System.out.println("ログイン社員番号未登録エラー");
|
138
|
+
// TODO 自動生成された catch ブロック
|
139
|
+
errors.add("社員番号が登録されていません");
|
140
|
+
|
141
|
+
e.printStackTrace();
|
123
|
-
}
|
142
|
+
}
|
143
|
+
|
144
|
+
}
|
124
145
|
|
125
146
|
}
|
126
147
|
}
|
127
|
-
```
|
128
|
-
### 試したこと
|
129
|
-
Employees employees = (Employees) em
|
130
|
-
.createNativeQuery("select * from employees where code =:code", Employees.class)
|
131
|
-
.setParameter("code", code)
|
132
|
-
.getSingleResult();
|
133
|
-
|
134
|
-
の箇所に、try-catchしてNoResultExceptionをcatchするようにしたのですが、
|
135
|
-
Employeesクラスの初期化がうまくいかず、エラーが解消されませんでした。
|
136
|
-
対処方法はtry-catchなのかな、とは思っているのですが、
|
137
|
-
どの位置に入れる事が適切か、教えて頂けると幸いです。
|
138
|
-
素人質問で申し訳ないです。
|
139
148
|
|
140
149
|
### 言語
|
141
150
|
Java
|