简体   繁体   English

注释@GeneratedValue导致InvalidDataAccessApiUsageException

[英]Annotation @GeneratedValue caused InvalidDataAccessApiUsageException

I have two classes connected with many-to-many relationship: 我有两个与多对多关系相关的类:

Book.java Book.java

@Entity
@Table(name = "book")
public class Book {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "book_id")
    private Long id;
    @Column(nullable = false)
    private String title;
    private String description;
    @Column(name = "release_date")
    @Temporal(TemporalType.TIMESTAMP)
    private Date releaseDate;
    @JoinColumn(name = "cover_image")
    @OneToOne(cascade = CascadeType.MERGE)
    private UploadFile coverImage;
    @OneToOne(cascade = CascadeType.MERGE)
    private UploadFile content;
    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "book_category", joinColumns = {@JoinColumn(name = "book_id", referencedColumnName = "id")},
        inverseJoinColumns = {@JoinColumn(name = "category_id", referencedColumnName = "id")})
    private Set<Category> categories;
}

Category.java Category.java

@Entity
@Table(name = "category")
public class Category {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "category_id")
    private Long id;
    @Column(name = "category_name")
    private String name;
    @ManyToMany(mappedBy = "categories", fetch = FetchType.LAZY) // I also tried an option with adding cascade = CascadeType.ALL
    private List<Book> books;
}

When I want to add book to database I recive following exception: 当我想将书添加到数据库时,出现以下异常:

org.springframework.dao.InvalidDataAccessApiUsageException: detached entity passed to persist: com.github.model.Category; nested exception is org.hibernate.PersistentObjectException: detached entity passed to persist: com.github.model.Category

Interestingly removing @GeneratedValue annotation from the Category class causes everything to work as well as possible. 有趣的是,从Category类中删除@GeneratedValue批注会使所有事情都尽可能正常地工作。 To add book I extends JpaRepository: 要添加书,我扩展了JpaRepository:

public interface Repository extends JpaRepository<Book, Long>{
}

and then 接着

Repository repository;
repository.save(book);

What's also should be important I'm using h2 database. 我正在使用h2数据库也应该很重要。 To see all the code you can visit my github: https://github.com/mmaciula/Library/tree/master/src/main/java/com/github 要查看所有代码,您可以访问我的github: https : //github.com/mmaciula/Library/tree/master/src/main/java/com/github

EDIT 编辑

After I added @Column annotations the problem with InvalidDataAccessApiUsageException was solved. 添加@Column批注后, InvalidDataAccessApiUsageException的问题已解决。 However, the problem with mapping collection appears. 但是,出现映射收集问题。 Here is the stack track I receive: 这是我收到的堆栈跟踪:

java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.BeanCreationException: 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 org.hibernate.AnnotationException: Unable to map collection com.github.model.Book.categories
Caused by: org.hibernate.AnnotationException: Unable to map collection com.github.model.Book.categories
Caused by: org.hibernate.cfg.RecoverableException: Unable to find column with logical name: id in org.hibernate.mapping.Table(book) and its related supertables and secondary tables
Caused by: org.hibernate.MappingException: Unable to find column with logical name: id in org.hibernate.mapping.Table(book) and its related supertables and secondary tables

EDIT 2 编辑2

When I changed referencedColumnName in @JoinTable from id to book_id I recived DataIntegrityViolationException shown below: 当我改变referencedColumnName在@JoinTable从ID到book_id我recived DataIntegrityViolationException如下图所示:

org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint ["FKAM8LLDERP40MVBBWCEQPU6L2S: PUBLIC.BOOK_CATEGORY FOREIGN KEY(CATEGORY_ID) REFERENCES PUBLIC.CATEGORY(CATEGORY_ID) (1)"; SQL statement:
insert into book_category (book_id, category_id) values (?, ?) [23506-196]]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement

Possible solution 1 : remove (cascade = CascadeType.ALL) 可能的解决方案1:删除(cascade = CascadeType.ALL)

Possible solution 2 : add (cascade = CascadeType.ALL) in @ManyToMany(mappedBy = "categories", fetch = FetchType.LAZY,cascade = CascadeType.ALL) 可能的解决方案2:在@ManyToMany(mappedBy = "categories", fetch = FetchType.LAZY,cascade = CascadeType.ALL)添加(cascade = CascadeType.ALL) @ManyToMany(mappedBy = "categories", fetch = FetchType.LAZY,cascade = CascadeType.ALL)

Refer this : https://stackoverflow.com/a/45781533/6572971 参阅此: https : //stackoverflow.com/a/45781533/6572971

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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