I'm following a JPA/Hibernate course from UDemy (https://www.udemy.com/course/hibernate-and-spring-data-jpa-beginner-to-guru) and was asked to convert a FKEY long identifier into object it points to on the entity class. Things worked before doing this. Now I get that
Caused by: org.hibernate.MappingException: Could not determine type for: com.guru.sdjpajdbc.domain.Author, at table: book, for columns: [org.hibernate.mapping.Column(author)]
error.
package com.guru.sdjpajdbc.domain;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
@Data
@Entity
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode
public class Author {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@EqualsAndHashCode.Exclude String firstName;
@EqualsAndHashCode.Exclude private String lastName;
}
package com.guru.sdjpajdbc.domain;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import org.springframework.data.annotation.Transient;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@Entity
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Book {
public Book(String isbn, String publisher, String title) {
super();
this.isbn = isbn;
this.publisher = publisher;
this.title = title;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String isbn;
private String publisher;
private String title;
@Transient
private Author author;
}
V1__init_database.sql
drop table if exists book cascade;
drop table if exists author;
create table book (id bigint not null auto_increment primary key,isbn varchar(255),publisher varchar(255),title varchar(255),author_id BIGINT) engine = InnoDB;
create table author
(
id bigint not null auto_increment primary key,
first_name varchar(255),
last_name varchar(255)
) engine = InnoDB;
alter table book
add constraint book_author_fk foreign key (author_id) references author (id);
insert into author (first_name, last_name) values ('Craig', 'Walls');
insert into book (isbn, publisher, title, author_id) values ('978-1617294945', 'Simon & Schuster',
'Spring in Action, 5th Edition',(select id from author where first_name = 'Craig' and last_name = 'Walls') );
insert into book (isbn, publisher, title, author_id) values ('978-1617292545', 'Simon & Schuster',
'Spring Boot in Action, 1st Edition',(select id from author where first_name = 'Craig' and last_name = 'Walls') );
insert into book (isbn, publisher, title, author_id) values ('978-1617297571', 'Simon & Schuster',
'Spring in Action, 6th Edition',(select id from author where first_name = 'Craig' and last_name = 'Walls') );
insert into author (first_name, last_name) values ('Eric', 'Evans');
insert into book (isbn, publisher, title, author_id) values ('978-0321125217', 'Addison Wesley',
'Domain-Driven Design',(select id from author where first_name = 'Eric' and last_name = 'Evans') );
insert into author (first_name, last_name) values ('Robert', 'Martin');
insert into book (isbn, publisher, title, author_id) values ('978-0134494166', 'Addison Wesley',
'Clean Code',(select id from author where first_name = 'Robert' and last_name = 'Martin') );
The instructor ran into this error but fixed it solely by adding the @Transient annotation. For some reason, it works for him, but not for me.