为什么会出现“未为实体指定标识符:”错误?

时间:2019-07-02 14:03:45

标签: java spring-boot

我正在按照一个教程进行操作,并且创建了2个实体,但是当我运行应用程序时,出现以下错误“初始化方法调用失败;嵌套的异常是org.hibernate.AnnotationException:未为实体指定标识符:com.example .db_test.Post”。它在我关注的教程中有效,该教程很旧,所以也许这不再起作用了。创建这2个实体的正确方法是什么?

@Entity
public class Post {
    @Id
    @GeneratedValue
    private Long id;
    private String title;
    private String body;
    private Date postedOn;
    //Author
    @ManyToOne
    private Author author;

    private Post(){}

    public Post(String title){
        setTitle(title);
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public Date getPostedOn() {
        return postedOn;
    }

    public void setPostedOn(Date postedOn) {
        this.postedOn = postedOn;
    }

    public void setAuthor(Author author) {
        this.author = author;
    }

    public Author getAuthor() {
        return author;
    }
}

@Entity
public class Author {
    @Id
    @GeneratedValue
    private Long id;
    private String firstName;
    private String lastName;
    //posts
    @OneToMany(mappedBy = "author")
    private List<Post> posts;

    private Author(){
    }

    public Author(String firstName, String lastName){
        this.setFirstName(firstName);
        this.setLastName(lastName);
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public List<Post> getPosts() {
        return posts;
    }

    public void setPosts(List<Post> posts) {
        this.posts = posts;
    }
}

0 个答案:

没有答案