我有两个与多对多关系相关的类:
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
@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;
}
当我想将书添加到数据库时,出现以下异常:
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
有趣的是,从Category类中删除@GeneratedValue批注会使所有事情都尽可能正常地工作。要添加书,我扩展了JpaRepository:
public interface Repository extends JpaRepository<Book, Long>{
}
然后
Repository repository;
repository.save(book);
我正在使用 h2 数据库也应注意的重要事项。要查看所有代码,您可以访问我的github:Why does pip freeze list "pkg-resources==0.0.0"?
编辑
添加@Column批注后,InvalidDataAccessApiUsageException
的问题已解决。但是,出现映射收集问题。这是我收到的堆栈跟踪:
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
编辑2
当我在@JoinTable中将referencedColumnName
从id更改为book_id时,我收到了如下所示的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
答案 0 :(得分:0)
可能的解决方案1:删除(cascade = CascadeType.ALL)
可能的解决方案2:在(cascade = CascadeType.ALL)
中添加@ManyToMany(mappedBy = "categories", fetch = FetchType.LAZY,cascade = CascadeType.ALL)