23.12.27 13:03 작성
·
815
0
강의내용그대로 입력하였는데 왜그럴까요 ㅠㅠ
서비스부분
public void loan(BookLoanReq req) {
Book book = bookRepstory.findByName(req.getBookName()).orElseThrow(IllegalArgumentException::new);
if (userLoanHistoryRep.existsByBookNameAndIsReturn(book.getName(), false)){
throw new IllegalArgumentException("대출중인 책입니다.");
}
User user=userRepstrory.findByName(req.getUserName()).orElseThrow(IllegalArgumentException::new);
userLoanHistoryRep.save( new UserLoanHistory(user.getId(),book.getName(),false));
}
public interface UserRepstrory extends JpaRepository<User,Long> {
Optional<User> findByName(String name);
}
package com.group.libraryapp.domain.user;
import javax.persistence.*;
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
public User(String name,Integer age) {
if(name==null||name.isBlank()){
throw new IllegalArgumentException(String.format("널값"));
}
this.name = name;
this.age = age;
}
@Column(name="age",length = 20)
private Integer age;
@Column(name="name",nullable = false)
private String name;
protected User(){
}
public Long getId() {
return id;
}
public Integer getAge() {
return age;
}
public String getName() {
return name;
}
public void update(String name){
this.name=name;
}
public User(Long id, Integer age, String name) {
this.id = id;
this.age = age;
this.name = name;
}
}
마지막으로 오류코드올려드릴게요
ava.lang.IllegalArgumentException: null
at java.base/java.util.Optional.orElseThrow(Optional.java:403) ~[na:na]
at com.group.libraryapp.service.book.BookService.loan(BookService.java:39) ~[main/:na]
at com.group.libraryapp.controller.book.BookController.loan(BookController.java:29) ~[main/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]