0

I am trying to save the data entered through REST API's post method() to MySQL Database, but I am getting the org.hibernate.id.IdentifierGenerationException: Unknown integral data type for ids : java.lang.String

Book.java

@Entity
@Table(name = "book")
public class Book {

    @Id
    @GeneratedValue
    @Column(name = "ISBN")
    private String ISBN;
    @Column(name = "title")
    private String title;
    @Column(name = "subject")
    private String subject;
    @Column(name = "publisher")
    private String publisher;
    @Column(name = "language")
    private String language;
    @Column(name = "numberOfPages")
    private int numberOfPages;

    public Book() {
    }

    public Book(String iSBN, String title, String subject, String publisher, String language, int numberOfPages) {
        ISBN = iSBN;
        this.title = title;
        this.subject = subject;
        this.publisher = publisher;
        this.language = language;
        this.numberOfPages = numberOfPages;
    }

    public String getISBN() {
        return ISBN;
    }

    public void setISBN(String iSBN) {
        ISBN = iSBN;
    }

    public String getTitle() {
        return title;
    }

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

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getPublisher() {
        return publisher;
    }

    public void setPublisher(String publisher) {
        this.publisher = publisher;
    }

    public String getLanguage() {
        return language;
    }

    public void setLanguage(String language) {
        this.language = language;
    }

    public int getNumberOfPages() {
        return numberOfPages;
    }

    public void setNumberOfPages(int numberOfPages) {
        this.numberOfPages = numberOfPages;
    }

    @Override
    public String toString() {
        return "Book [ISBN=" + ISBN + ", title=" + title + ", subject=" + subject + ", publisher=" + publisher
                + ", language=" + language + ", numberOfPages=" + numberOfPages + "]";
    }

}

LibrarianController.java

@RestController
@RequestMapping("/librarian")
public class LibrarianController {

    private static final Logger log = LoggerFactory.getLogger(LibrarianController.class);

    @Autowired
    LibrarianService librarianService;

    @PostMapping
    public ResponseEntity<Book> addBooks(@RequestBody Book book) {
        log.info("Entering addBooks method ");
        librarianService.addBooks(book);
        log.info("Exiting addBooks method ");
        return ResponseEntity.ok(book);


    }
}

LibrarianService.java

@Service
public class LibrarianService {

    @Autowired
    LibrarianRepository librarianRepository;

    public void addBooks(Book book) {
         librarianRepository.save(book);
    }
}

LibrarianRepository.java

@Repository
public interface LibrarianRepository extends JpaRepository<Book, String> {

}

getting the following error message Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is java.lang.annotation.AnnotationTypeMismatchException: Incorrectly typed data found for annotation element public abstract java.lang.String javax.persistence.GeneratedValue.generator() (Found data of type Ljavax/persistence/GenerationType;.AUTO)

farhanlq
  • 71
  • 4
  • 16
  • 2
    You have put a `@GeneratedValue` annotation on the ISBN field, which is a string. Hibernate doesn't know how to generate a string value for this field. First of all: do you really want Hibernate to automatically generate a value for this field? If not, then remove the annotation. If yes, then you need to configure Hibernate's generator to generate an appropriate string value. – Jesper Sep 06 '19 at 09:11
  • Yes, I want Hibernate to automatically generate a value for this. Do you know how to configure Hibernate's generator to generate an appropriate value? – farhanlq Sep 06 '19 at 09:41
  • 1
    Exact same problem and relevant solutions are discussed at https://stackoverflow.com/questions/51998450/auto-generate-string-primary-key-with-annotation-in-hibernate – testuser Sep 06 '19 at 11:13
  • That's work for me, Thanks a lot. – farhanlq Sep 06 '19 at 11:31

1 Answers1

0

When we use a string as a key autogenerated and, we must indicate that to hibernate. Instead of using this:

@Id`enter code here`
@GeneratedValue
@Column(name = "ISBN")
private String ISBN;

try this:

@Id
@GeneratedValue(generator="system-uuid")
@GenericGenerator(name="system-uuid", strategy = "uuid")
@Column(name = "ISBN")
private String ISBN;