简体   繁体   中英

java.lang.IllegalStateException: Required identifier property not found

I keep getting this exception while I'm trying to save an object to an H2 database.

The object I'm trying to persist is defined as following:

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;

The controller:

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);
    }
}

The payload that I'm sending to that endpoint via Postman:

{
    "name":"first task",
    "description":"some fascinating description"
}

The repository:

package mypackage.foo.core.jpa.repository;

@Repository
public interface TaskDao extends JpaRepository<Task, Long> {
}

The app configuration:

@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 deps:

<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>

My approach: The first idea was that it's been happening due to missing setter for Id in Task , but it didn't help. I've been wondering if I can get any hints how to solve this issue? Any attempt to call save(task) is causing IllegalStateException to be thrown.

The save(Task task) in TaskService is essentially calling the taskDao.save(Task task) .

I was also suspecting the wrong @Id annotation (I mean from the wrong package), but it's not the case here.

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

Turnes out that removing these two lines have helped:

@EnableJpaRepositories(basePackages = {"mypackage.foo.core.jpa.repository"})
@EntityScan(basePackages = {"mypackage.foo.core.jpa.domain"})

When I noticed the same experience after switching to MySql, I started to think that the problem must be with my configuration. I tried to replace these annotations with ComponentScan , but IntelliJ started complaining that: Redundant declaration: @SpringBootApplication already applies given @ComponentScan

so, my idea was, maybe these @EnableJpaRepositories and @EntityScan aren't necessary here after all? Removing them helped. I'm not sure why tho.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM