Exception when not finding
What is the rationale behind throwing an exeption in JPA, when something can't be found?
Added:
This behaviour was seen in EJB2, and has as far been removed from EJB3 - but... It remains when calling Query#getSingleResult() which throws a NoResultException.
Normally I would not expect it to be an exception that something could not be found. Actually this might me the default situation in some cases, and at leats the expected cases in most other sotuations. It makes me remeber an old saying that you should not use exceptions as part of your business logic, but only as what it is, special cases you don't know how or don't want to deal with.
Does anyone have an idea why it is implemented this way in JPA? What is the drawback on returning null when something could not be found?
Asked by: Dainton971 | Posted: 28-01-2022
Answer 1
Are you calling getReference(class, primaryKey)? That will throw an exception if the primary key doesn't exist; if you'd like null object returned, use find(class, primaryKey) instead. getReference() allows lazy loading, which find() doesn't, but the side-effect of that is that to allow lazy-loading you need to be referring to a real object, which means that you must pass a valid PK.
Answered by: Thomas454 | Posted: 01-03-2022Similar questions
java - Finding Exception Cause from Vague Stack Trace
I have an Android app that has recently been mysteriously force closing for some of my users using 4.x devices. Looking at the exception stack, it seems like the error occurs before any of my code ever gets run, but I assume I am doing something that is causing this.
*Is something wrong with how I am finding my cause or stack? Otherwise, if this is a full stack trace is it really possible that Android fail...
java - What are my next steps on finding the cause of this exception?
I'm fairly new to Eclipse, Java, and Android programming. I've taken over development of an Android application. Someone has made a change several months ago that is causing the application to crash (exception) in code that is not part of the application code. I don't have any idea where or what part of the project was changed to cause the crash. I step through the code as the application is running on the Android device b...
java - finding out which type of exception to throw
I am confronting with the following issue for some time.
Let's say you have some code and at some point you need to throw an exception. Let's suppose we want to throw a runtime exception.
Java comes with plenty of exceptions and, in some cases, you may find one suitable for your case.
The problem is how can I figure out which of the exceptions is most suitable for my case?
My question is onl...
java - File reader finding exception
EDIT: Sorry, I'm an idiot. I didn't realize my loop was going for too long for my test documents data, the final product will be 150 lines long, but mine was only 9, causing the error. Sorry for the time wasting, and thanks for the help
So I need to write a program that reads in data from a file, said data is separated by "," which I am using the split command to store in an array. Every 3rd result...
What Java exception class to use for HTTP errors?
I am using Apache HttpClient and would like to communicate HTTP errors (400 Bad Request, 404 Not Found, 500 Server Error, etc.) via the Java exception mechanism to the calling code. Is there an exception in the Java standard library or in a widely used library that would be appropriate to use or to subclass for this purpose?
The alternative is to c...
exception - Best way to return status flag and message from a method in Java
I have a deceptively simple scenario, and I want a simple solution, but it's not obvious which is "most correct" or "most Java".
Let's say I have a small authenticate(Client client) method in some class. The authentication could fail for a number of reasons, and I want to return a simple boolean for control flow, but also return a String message for the user. These are the possibilities I can think of:
Which Java exception should I use?
Let's assume I'm a complete lazy bum and I don't want to invest the several dozen keystrokes needed for my own exception class (it's not utterly important which gets used, really). However, to pretend I'm following good practices here, I want a pre-existing one that best fits my situation.
Problem: I need to throw an exception when my class's constructor receives an object in its parameters that i...
java - throws Exception in finally blocks
Is there an elegant way to handle exceptions that are thrown in finally block?
For example:
try {
// Use the resource.
}
catch( Exception ex ) {
// Problem with the resource.
}
finally {
try{
resource.close();
}
catch( Exception ex ) {
// Could not close the resource?
}
}
How do you avoid the try/catch in the f...
In Java, when should I create a checked exception, and when should it be a runtime exception?
This question already has answers here:
java - SQL exception when trying make an prepared statement
I'm trying to pass the following String to a PreparedStatement:
private static final String QUICK_SEARCH = "select * from c where NAME like '% ? %'";
However, I get an SQL exception that the bind variable is missing.
Any suggestions?
Doubt in exception handling and finally block in java
Can you tell the idea of how to doing it?
Code :
public void main(String[] args) {
try {
//Some Exception throws here
}
catch(SomeException se) {
se.printStackTrace();
}
finally {
try {
//SomeException1 throws here
}
catch(SomeException1 se1) {
se.printStackTrace();
//Control is getting stop in this bloc...
java - Exception occurs with JAX-RPC handler
I have some SOAP webservices build with JAX-RPC. These work fine. But as soon as I add a handler, I get an exception. When the binding is removed from the webservices.xml, everything works fine again.
The weird thing is, the handler itself isn't included in the stacktrace of the exception. I also noticed, the init and getHeaders methods of the handler are called, before the exception is r...
java - exception when parsing multipart form data
I am trying to handle a file upload, and I'm using the
com.oreilly.servlet.multipart.MultipartParser class to parse the posted
data (in cos.jar). However, when I call the constructor for MultipartParser, I get this
exception:
java.io.IOException: Corrupt form data: premature ending
at com.oreilly.servlet.multipart.MultipartParser.<init>(MultipartParser.java:166)
at com.oreilly.servlet.multipa...
java - Help with an Exception
Encounter the following exception.
Anyone have any idea how to resolve this? Or what is the issue exactly.
[#|2009-03-03T10:41:18.079+0800|SEVERE|sun-appserver-pe8.2|ilog.rules.teamserver.model.IlrGlobalCache|_ThreadID=12;|java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
java.rmi.RemoteException: nested exception is: java.sql.SQLException: Error in allocating a co...
Still can't find your answer? Check out these amazing Java communities for help...
Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)