0

I was following a tutorial and I created 2 entities, but when i run the application I get the following error "Invocation of init method failed; nested exception is org.hibernate.AnnotationException: No identifier specified for entity: com.example.db_test.Post". It worked in the tutorial I was following, the tutorial is old so maybe this does not work anymore. What would be the correct way to create this 2 entities.

@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;
    }
}
Dinu Nicolae
  • 1,019
  • 2
  • 17
  • 42
  • 7
    Are you importing javax.persistence.Id correctly? Generally this is the error, by importing another package, for example org.springframework.data.annotation.Id. – Andrea Jul 02 '19 at 14:06
  • Possible duplicate of https://stackoverflow.com/questions/4381290/hibernate-exception-org-hibernate-annotationexception-no-identifier-specified – user Jul 02 '19 at 14:31
  • This is also a possible duplicate: https://stackoverflow.com/questions/15320408/org-hibernate-annotationexception-no-identifier-specified-for-entity-even-whe – g00glen00b Jul 03 '19 at 13:26

0 Answers0