I have this class
public class Hotel implements Serializable {
@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "FILE_ID")
private File file;
..
}
and this query
Query query = em.createQuery("SELECT file.hotel FROM File file where file.id = :id ");
and I would like to know if it is possible to return an empty Hotel object instead of null if the hotel does not exists in the relationship but the file exists
Since you're using JPA, you could make use of a repository. JPA can automatically return a type of Optional<>
. Via Optional.isPresent()
you can check if the query returned something.
A repository could look like this:
public interface FileRepository extends JpaRepository<File, Long> {
Optional<File> findById(Long id);
}