0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Springboot:query did not return a unique result: 3ってでた

Last updated at Posted at 2023-03-18

springbootで構築したアプリケーションを実行したら、
query did not return a unique result: 3
ってでた、、なんでや、、😢

調べてみたら、返ってきた結果が1つではなく3つだよって意味だった。

なので、可能性は2つ。一つ目は、Repositoryのエラー。Repositoryで結果を返すところの引数を一つの結果だけを格納する状態にしてたために起こるエラー。2つ目は、Contorollerのエラー。これは、@NamedQueryを使った際に、createNamedQueryの結果を格納する変数が単一結果格納用の変数だったり、getResultList()の格納先が単一結果格納用だったりする場合に起こるエラー。

今回私はNamedQueryではない簡単な検索を行っていたので、一つ目が該当するはず。。
そう思って見てみると、

Lesson04_04UserRepository.java
package jp.co.sss.training.repository;

import org.springframework.data.jpa.repository.JpaRepository;

import jp.co.sss.training.entity.Lesson04_04User;


import org.springframework.data.jpa.repository.JpaRepository;

import jp.co.sss.training.entity.Lesson04_04User;

public interface Lesson04_04UserRepository extends JpaRepository<Lesson04_04User, Integer> {
	Lesson04_04User findByDepartmentId(int departmentId);
}

これだと一行分のデータしか入らない、、
しかし、今回は私は格納すべきデータは3行分。
なので、

Lesson04_04UserRepository.java
package jp.co.sss.training.repository;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;

import jp.co.sss.training.entity.Lesson04_04User;

public interface Lesson04_04UserRepository extends JpaRepository<Lesson04_04User, Integer> {
	List<Lesson04_04User> findByDepartmentId(int departmentId);
}

このように書けば無事できました!!
複数行分のデータを格納する時は、List型にするのを忘れずに^^;

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?