When I try to access the findById it is showing the following:
IllegalArgumentException "id to load is required for loading"
Here’s my code:
package controllers;
import play.*;
import play.mvc.*;
import java.util.*;
import models.*;
public class Application extends Controller {
public static void index() {
render();
}
public static void saveUser(String name)
{
User1 user =new User1(name);
String res = "";
if( user.save()!=null){
res="Stored Successfully";
}
else{
res="Failed to store";
}
render(res);
}
public static void showUser(Long id)
{
User1 user= User1.findById(id);
render(user);
}
}
and below is my routes file i don’t understand why the error is coming and illegal argument exception.
# Routes
# This file defines all application routes (Higher priority routes first)
# ~~~~
# Home page
GET / Application.index
# Ignore favicon requests
GET /favicon.ico 404
# Map static resources from the /app/public folder to the /public path
GET /public/ staticDir:public
# Catch all
* /{controller}/{action} {controller}.{action}
asked May 23, 2015 at 17:39
Ravi Ravi
211 gold badge1 silver badge4 bronze badges
4
IllegalArgumentException is thrown because the id is null. Make sure you pass correct value with the request. Mapping the controller method in routes file as follows would prevent from passing null:
GET /user/{id} Aplication.showUser
answered May 23, 2015 at 18:51
Mon CalamariMon Calamari
4,4033 gold badges25 silver badges44 bronze badges
The value of id variable that you pass to User1.findById is something unacceptable.
Maybe it’s a negative number. Read documentation to User1.findById to find requirements to the id variable.
Try this code to check the value of id before calling findById(id):
public static void showUser(Long id)
{
System.out.println("Application,showUser(id = " + id + " );"
User1 user= User1.findById(id);
render(user);
}
answered May 23, 2015 at 17:48
![]()
Nick VolynkinNick Volynkin
13.5k6 gold badges42 silver badges66 bronze badges
0
View unanswered posts | View active topics
| Author | Message |
|---|---|
|
Post subject: id to load is required for loading — Error
|
|
|
Newbie |
Please help: Hibernate version:3.0 java.lang.IllegalArgumentException: id to load is required for loading |
| Top |
|
|
tenwit |
Post subject:
|
||
|
You’re passing null to Session().get(). You can’t get null. |
||
| Top |
|
||
|
thozhan_munus |
Post subject:
|
|
|
This error comes only when we use |
| Top |
|
|
tenwit |
Post subject:
|
||
|
I presume that that’s an Hibernate tool. Perhaps you could ask your question over on the Tools forum. |
||
| Top |
|
||
|
max |
Post subject:
|
||
|
so you have mapped the id to a column which dont have data…? (p.s. loadrunner is not a hibernatetool, I think it is some stresstesting tool they are using) |
||
| Top |
|
||
|
thozhan_munus |
Post subject:
|
|
|
The id’s are created automatically by the oracle sequences.
This error happens when there are lot of concurrent users |
| Top |
|
|
max |
Post subject:
|
||
|
so you are most likely using the session unsafely — they are not threadsafe, so only use them in one thread at one time. |
||
| Top |
|
||
|
thozhan_munus |
Post subject:
|
|
|
How do i do that?.. |
| Top |
|
|
max |
Post subject:
|
||
|
huh ? no settings needed/possible. just ensure that your software is written so you don’t use the same session in multiple threads. |
||
| Top |
|
||
|
thozhan_munus |
Post subject:
|
|
|
I guess we are already doing that. Is the following property is of any useful?. <property name=»current_session_context_class»>thread</property> |
| Top |
|
|
zzyborg |
Post subject:
|
||
|
Quote: just ensure that your software is written so you don’t use the same session in multiple threads. you may try to use a «ThreadLocal» sessions . |
||
| Top |
|
||
|
thozhan_munus |
Post subject:
|
|
|
We are already doing that… |
| Top |
|
|
tenwit |
Post subject:
|
||
|
You probably don’t want to use ThreadLocal sessions, as that implies long-lived sessions, which have separate issues that you’ll need to carefully manage. Use ThreadLocal SessionFactories, and get/release sessions as needed. That won’t solve your null id problem though. You’ll have to debug that one yourself. |
||
| Top |
|
||
| You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum |
Как только программа заходит в метод loginClient выкидывает IllegalArgumentException: id to load is required for loading. Cуть метода получить ввод юзера из html формы, найти юзера по юзернайм в базе и сверить введенные и данные в базе.
@PostMapping("/login")
public String loginClient(@ModelAttribute("input") Input input) throws Exception {
String page = "";
userInput = input.getPostUsername();
Client existingClient = service.getClient(userInput);
if((input.getPostUsername().equals(existingClient.getUsername())&
(input.getPostPassword().equals(existingClient.getPassword())))){
page = "Auth";
SpotifyAPI newApi = new SpotifyAPI();
newApi.startServer(8081);
}
else{
page = "index";
}
return page;
<form method="post" th:action="@{/login}" th:object="${input}" class="login">
<p>
<label th:for="postUsername">Username: </label>
<input type="text" th:name="postUsername" id="postUsername"/>
</p>
<p>
<label th:for="postPassword">Password: </label>
<input type="text" th:name="postPassword" id="postPassword"/>
</p>
java.lang.IllegalArgumentException: id to load is required for loading
org.hibernate.event.spi.LoadEvent.<init>(LoadEvent.java:93)
org.hibernate.event.spi.LoadEvent.<init>(LoadEvent.java:63)
org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.doLoad(SessionImpl.java:2920)
org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.lambda$load$1(SessionImpl.java:2904)
org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.perform(SessionImpl.java:2860)
org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.load(SessionImpl.java:2904)
org.hibernate.internal.SessionImpl.get(SessionImpl.java:1097)
spotify.repository.ClientDAOImpl.getCustomer(ClientDAOImpl.java:58)
spotify.service.ClientServiceImpl.getClient(ClientServiceImpl.java:41)
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.base/java.lang.reflect.Method.invoke(Method.java:567)
org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344)
org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294)
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
com.sun.proxy.$Proxy46.getClient(Unknown Source)
spotify.controller.AppControl.loginClient(AppControl.java:31)
ClientServiceImpl class:
@Override
@Transactional
public Client getClient(String username) {
return clientDAO.getCustomer(username);
}
DAOimpl
@Override
public Client getCustomer(String username) {
// get the current hibernate session
Session currentSession = sessionFactory.getCurrentSession();
// now retrieve/read from database using the primary key
return currentSession.get(Client.class, username);
}
The cause of the exception: When using hibernate, you want to query by id, but the id is null, so this exception occurs!
Solution: break the point debugging, first see if the id has value! If not, the problem is found! If there is no value, you have to figure out where the value has disappeared! So, check the code and find a way to get the value of id!
My problem is: I didn’t submit the value of the id when submitting the form.
Exception description:
Serious: Exception occurred during processing request: id to load is required for loading
java.lang.IllegalArgumentException: id to load is required for loading
at org.hibernate.event.spi.LoadEvent.<init>(LoadEvent.java:109)
at org.hibernate.event.spi.LoadEvent.<init>(LoadEvent.java:79)
at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.load(SessionImpl.java:2548)
at org.hibernate.internal.SessionImpl.get(SessionImpl.java:955)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
When submitting the form form:
<tr>
<td><label class="mylabel">Username:</td>
<td>
<input name="oeadmin.name" type="text" value="${currentUser.name}" readonly="readonly"/>
</td>
</tr>
I didn’t submit the id at all.
Then I:
<tr>
<td><label class="mylabel">Username:</td>
<td>
<input type="hidden" value="${currentUser.id }" name="oeadmin.id" /> <!--here-->
<input name="oeadmin.name" type="text" value="${currentUser.name}" readonly="readonly"/>
</td>
</tr>
It is solved!
tags: Exception collector
cause:
When using hibernate, I sometimes encounter this exception: java.lang.IllegalArgumentException: id to load is required for loading, this is because when using a query like findbyId(id), the passed id is null
Solution
Method 1: background solution. Perform non-null verification when the id is passed in. Skip this method when it is empty.
Method 2: Resolve at the front desk Find the corresponding element to verify why it is empty, and find a way to make it not empty
Intelligent Recommendation
Required String parameter ‘id’ is not present
Question details: To put it simply, I initiated a post request to the background via Ajax, but the background did not receive the parameters sent by the request, and reported such an error. The error …
java.lang.IllegalArgumentException
The reason for reporting this error is that jdk version and spring version are not compatible, jdk1.8 should use spring 4.0 and above…
More Recommendation
Last Update:2018-02-27
Source: Internet
Reason one: This exception is caused by the use of the struts tag for passing values, such as:
<s:a action= «Role_delete?id=%{id}» onclick= «return confirm (‘ OK to delete? ‘) ‘ > Delete </s:a>
<s:a action= «Role_editui?id=%{id}» > Modify </s:a>
Using the above method to pass the ID will appear java.lang.IllegalArgumentException:id to load is required for loading exception, resulting in ID null, the reason is because the wood has the value of the ID passed past
Change into
<a href= «Role_delete?id=${id}» onclick= «return confirm (‘ OK to delete? ‘) ‘ > Delete </a>
<a href= «Role_editui?id=${id}» > Modify </a>
This exception may not occur
Reason two: There is no null value in the method calling the ID
if (id==null) {
return null;
}else{
Perform the operation ….. …..
}
>http://blog.csdn.net/pdh822407904/article/details/19286569
Java.lang.IllegalArgumentException:id to load are required for loading workaround


