質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

Q&A

解決済

1回答

3811閲覧

findOne(Example<S>) は引数 (Integer) にさせたい

arin

総合スコア5

Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

0グッド

0クリップ

投稿2021/10/14 08:00

findOne(Example<S>) は引数 (Integer) にさせたい

Spring Bootで会員情報管理システムを作っています。
会員情報取得機能を実装中にCustomerService.Javaで以下のエラーメッセージが発生しました。

発生している問題・エラーメッセージ

型 QueryByExampleExecutor<Customer> のメソッド findOne(Example<S>) は引数 (Integer) に適用できません 型 CrudRepository<Customer,Integer> のメソッド delete(Customer) は引数 (Integer) に適用できません

Customer.java

Java

1package com.example.domain; 2 3import lombok.AllArgsConstructor; 4import lombok.Data; 5import lombok.NoArgsConstructor; 6 7import javax.persistence.*; 8 9@Entity 10@Table(name = "customers") 11@Data 12@NoArgsConstructor 13@AllArgsConstructor 14public class Customer { 15 @Id 16 @GeneratedValue(strategy = GenerationType.IDENTITY) 17 private Integer id; 18 @Column(nullable = false) 19 private String firstName; 20 @Column(nullable = false) 21 private String lastName; 22}

CustomerService.java

Java

1package com.example.service; 2 3import com.example.domain.Customer; 4import com.example.repository.CustomerRepository; 5import org.springframework.beans.factory.annotation.Autowired; 6import org.springframework.stereotype.Service; 7import org.springframework.transaction.annotation.Transactional; 8 9import java.util.List; 10 11 12@Service 13@Transactional 14public class CustomerService { 15 @Autowired 16 CustomerRepository customerRepository; 17 18 public List<Customer> findAll() { 19 return customerRepository.findAllOrderByName(); 20 } 21 22 public Customer findOne(Integer id) { 23 return customerRepository.findOne(id); 24 } 25 26 public Customer create(Customer customer) { 27 return customerRepository.save(customer); 28 } 29 30 public Customer update(Customer customer) { 31 return customerRepository.save(customer); 32 } 33 34 public void delete(Integer id) { 35 customerRepository.delete(id); 36 } 37}

CustomerRepository.java

package com.example.repository; import com.example.domain.Customer; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import java.util.List; public interface CustomerRepository extends JpaRepository<Customer, Integer> { @Query("SELECT x FROM Customer x ORDER BY x.firstName, x.lastName") List<Customer> findAllOrderByName(); }

CustomerRestController.java

Java

1package com.example.api; 2import com.example.domain.Customer; 3import com.example.service.CustomerService; 4import org.springframework.beans.factory.annotation.Autowired; 5import org.springframework.web.bind.annotation.GetMapping; 6import org.springframework.web.bind.annotation.PathVariable; 7import org.springframework.web.bind.annotation.RequestMapping; 8import org.springframework.web.bind.annotation.RestController; 9 10import java.util.List; 11 12@RestController 13@RequestMapping("api/customers") 14public class CustomerRestController { 15 @Autowired 16 CustomerService customerService; 17 18 @GetMapping 19 List<Customer> getCustomers() { 20 List<Customer> customers = customerService.findAll(); 21 return customers; 22 } 23 @GetMapping(path = "{id}") 24 Customer getCustomer(@PathVariable Integer id) { 25 Customer customer = customerService.findOne(id); 26 return customer; 27 } 28} 29

HajibootRestApplication.java

Java

1ソースコードpackage com.example.hajibootrest; 2 3import org.springframework.boot.SpringApplication; 4import org.springframework.boot.autoconfigure.SpringBootApplication; 5 6@SpringBootApplication 7public class HajibootRestApplication { 8 9 public static void main(String[] args) { 10 SpringApplication.run(HajibootRestApplication.class, args); 11 } 12 13}``` 14 15 16### 補足情報(FW/ツールのバージョンなど) 17 18Spring Boot 19Spring Tool Suite4

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

Spring Boot 1.xの時は、findOne(id)とdelete(id)でしたが
Spring Boot 2.xでは、findById(id)とdeleteById(id)にメソッド名が変更になっています。

java

1//customerRepository.findOne(id); 2customerRepository.findById(id); 3 4//customerRepository.delete(id); 5customerRepository.deleteById(id);

参考: Spring公式のissue


■追記

Optionalは、色々書き方がありますが、例えば以下のようなことができます。

java

1public Customer findOne(Integer id) { 2 return customerRepository.findById(id).orElseThrow(() -> 3 new EntityNotFoundException("Customer not found with id " + id)); 4}

投稿2021/10/14 10:35

編集2021/10/14 14:06
KT001

総合スコア618

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

arin

2021/10/14 13:07

ありがとうございます。 customerRepository.findById(id);と記述したところ下記のエラーが出ました。 型の不一致: Optional<Customer> から Customer には変換できません さら修正を下記のようにしたらこのようなエラーが出ました。 Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalStateException: Cannot load driver class: net.sf.log4jdbc.DriverSpy ### CustomerService.java ```Java package com.example.service; import com.example.domain.Customer; import com.example.repository.CustomerRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; import java.util.Optional; @Service @Transactional public class CustomerService { @Autowired CustomerRepository customerRepository; public List<Customer> findAll() { return customerRepository.findAllOrderByName(); } public Optional<Customer> findOne(Integer id) { return customerRepository.findById(id); } public Customer create(Customer customer) { return customerRepository.save(customer); } public Customer update(Customer customer) { return customerRepository.save(customer); } public void delete(Integer id) { customerRepository.deleteById(id); } } ``` ### CustomerService.java ```Java package com.example.api; import com.example.domain.Customer; import com.example.service.CustomerService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; import java.util.Optional; @RestController @RequestMapping("api/customers") public class CustomerRestController { @Autowired CustomerService customerService; @GetMapping List<Customer> getCustomers() { List<Customer> customers = customerService.findAll(); return customers; } @GetMapping(path = "{id}") Optional<Customer> getCustomer(@PathVariable Integer id) { Optional<Customer> customer = customerService.findOne(id); return customer; } } ```
KT001

2021/10/14 14:09

追記しました。OptionalはJava8以降の書き方なので、Javaの入門書も見てみると良いかもしれません。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問