我有两个类与多对多关系相连:
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
public interface Repository extends JpaRepository<Book, Long>{
}
Repository repository;
repository.save(book);
添加@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
可能的解决方案1:删除(cascade=CascadeType.All)
可能的解决方案2:在@manytomany(mappedBy=“categories”,fetch=fetchtype.lazy,cascade=cascadetype.all)
中添加(cascade=cascadetype.all)
请参阅:https://stackoverflow.com/A/45781533/6572971
注释是Tapestry利用的一个非常重要的特性,用于简化Web应用程序开发。 Tapestry提供了许多自定义注释。 它具有类,方法和成员字段的注释。 如前一节所述,Annotation也可用于覆盖功能的默认约定。 Tapestry注释分为四个主要类别,它们如下所示。 组件注释 用于Pages,Components和Mixins类。 一些有用的注释是 - @Property - 适用于领域。 用于
问题内容: 我是新来的hibernate。我不了解以下两种主要的密钥生成策略: 身分识别 序列 有人可以解释这两个是如何工作的,两者之间有什么区别? 问题答案: 引用Java持久性/标识和排序: 身份 排序使用数据库中的 特殊IDENTITY列 来允许数据库在插入对象的行时自动为其分配ID。许多数据库(例如 MySQL,DB2,SQL Server,Sybase和Postgres) 都支持标识列。
Annotation,顾名思义,就是注解。Annotation可以将Kubernetes资源对象关联到任意的非标识性元数据。使用客户端(如工具和库)可以检索到这些元数据。 关联元数据到对象 Label和Annotation都可以将元数据关联到Kubernetes资源对象。Label主要用于选择对象,可以挑选出满足特定条件的对象。相比之下,annotation 不能用于标识及选择对象。annotat
问题内容: 我的实体类映射如下: 我创建干净的新数据库和属性: 创建 为什么在创建数据库后,地狱(抱歉,此错误浪费了将近两天),我的postgres数据库中有一个序列? 我不想有一个序列,我只是想自动增加自动生成的值。 问题答案: 在PostgreSQL中,自动增量使用伪类型进行处理。您在执行时使用此类型。 现在要点-这种伪类型创建一个序列。使用创建的序列来处理自动增量。该列的默认值为- 。 在H
问题内容: 对于映射到HSQLDB中的表的实体中的id字段,我具有以下定义。 但这似乎不会生成唯一的ID。而是尝试将null插入列中,这将导致失败。如果我手动创建一个序列和生成策略以使用该序列,那么数据将按预期持久保存。 自动生成策略不是暗示提供者(在这种情况下为hibernate)将自动选择正确的方法并根据需要进行所有繁重的工作(创建序列,使用本机方法或适用于该特定平台的任何方法)吗?我的理解不
例如,如果我希望生成的ID开始为100,XXX而不是10,XXX并增加100,我将如何进行更改? 请帮个忙。提前谢了。