Home > Software engineering >  Spring application crashes when database doesn't return results
Spring application crashes when database doesn't return results

Time:08-10

When I execute query that is expected to return no results my application crashes, the database that I am currently using is SQLite. When I was using MariaDB this error hasn't been ocurring, error:

2022-08-10 16:10:47.160  WARN 15512 --- [nio-8080-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 0, SQLState: null
2022-08-10 16:10:47.160 ERROR 15512 --- [nio-8080-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper   : Query does not return results
2022-08-10 16:10:47.207 ERROR 15512 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.orm.jpa.JpaSystemException: could not extract ResultSet; nested exception is org.hibernate.exception.GenericJDBCException: could not extract ResultSet] with root cause

Query:

@Query(value = "SELECT * FROM Port WHERE user_id=:userId", nativeQuery = true)
Port checkIfUserHasPort(@Param("userId") Long userId);

Executing method inside try catch did not work. Any idea how to workaround this?

Port entity:

@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class Port {
    @Id
    private int port;
    private boolean active;
    private LocalDateTime expiration;
    @OneToOne
    @JoinColumn(name="id")
    private User user;
}

User entity

@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "user")
public class User {
    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "id_Sequence")
    @SequenceGenerator(name = "id_Sequence", sequenceName = "ID_SEQ")
    private Long id;
    @JsonIgnore
    private String username;
    @JsonIgnore
    private String password;
    @JsonIgnore
    @OneToOne(mappedBy = "user")
    private Port port;
    @JsonIgnore
    @ManyToMany(fetch = EAGER)
    private Collection<Role> roles = new ArrayList<>();
    @JsonIgnore
    @OneToMany(mappedBy = "user")
    private Set<Log> logs=new HashSet<>();
}

CodePudding user response:

please try this ;

@Query(value = "SELECT * FROM Port WHERE user_id= ?1")

Optional checkIfUserHasPort(@Param("userId") Long userId);

CodePudding user response:

What do you mean by no result? null or empty object

CodePudding user response:

Can you share your entity class? Without entity class and table definition very difficult to guess.

  • Related