在尝试将对象保存到H2数据库时,我不断收到此异常。
我要保留的对象定义如下:
package mypackage.foo.core.jpa.domain;
@Entity
@Getter
@NoArgsConstructor
public class Task implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Setter
private Long id;
private String name;
private String description;
控制器:
package mypackage.foo.core.web.controller;
@RestController
@RequiredArgsConstructor
public class TaskController {
private final TaskService taskService;
@PostMapping("/api/task/save")
public ResponseEntity<Task> handlePostAddTask(@RequestBody Task task) {
Task savedTask = taskService.save(task);
return new ResponseEntity<>(savedTask, HttpStatus.CREATED);
}
}
我通过邮递员发送到该端点的有效负载:
{
"name":"first task",
"description":"some fascinating description"
}
存储库:
package mypackage.foo.core.jpa.repository;
@Repository
public interface TaskDao extends JpaRepository<Task, Long> {
}
应用配置:
@SpringBootApplication
@EnableJpaRepositories(basePackages = {"mypackage.foo.core.jpa.repository"})
@EntityScan(basePackages = {"mypackage.foo.core.jpa.domain"})
public class FooApplication {
public static void main(String[] args) {
SpringApplication.run(FooApplication.class, args);
}
}
Maven部门:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
我的方法:
第一个想法是由于Id
中缺少Task
的setter而一直在发生,但这没有帮助。我一直想知道我是否可以找到解决此问题的任何提示?
尝试调用save(task)
会导致抛出IllegalStateException
。
save(Task task)
中的TaskService
实际上是在呼叫taskDao.save(Task task)
。
我也怀疑错误的@Id
注解(我的意思是来自错误的软件包),但事实并非如此。
答案 0 :(得分:0)
Use @Data annotation from lombok as given below. This will generate both setter & getter methods. I hope this will resolve the issue.
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import lombok.Data;
@Entity
@Data
public class Student {
@Id
@GeneratedValue
private long id;
private String firstName;
private String lastName;
@Data Equivalent to @Getter @Setter @RequiredArgsConstructor @ToString @EqualsAndHashCode.
~Satheesh
答案 1 :(得分:0)
结果证明,删除这两行有助于:
@EnableJpaRepositories(basePackages = {"mypackage.foo.core.jpa.repository"})
@EntityScan(basePackages = {"mypackage.foo.core.jpa.domain"})
当我在切换到MySql后注意到相同的体验时,我开始认为问题一定出在我的配置上。我试图用ComponentScan
替换这些注释,但是IntelliJ开始抱怨:
Redundant declaration: @SpringBootApplication already applies given @ComponentScan
所以,我的想法是,也许这里根本不需要这些@EnableJpaRepositories
和@EntityScan
吗?删除它们很有帮助。我不知道为什么。