Java 无法删除实体多对一关系
我的申请有三个实体:作者、书籍、评论。书籍和评论之间的关系是一对多的。当我想删除Review时,一切都正常,没有例外,但from DB row并没有删除 我认为hibernate可以在书评中保持书评的大小,并且不允许从数据库中删除。如果这是真的,我如何解决这个问题 或者可能是另一个地方的根本问题?需要帮助:) 账簿实体:Java 无法删除实体多对一关系,java,mysql,hibernate,entity-relationship,Java,Mysql,Hibernate,Entity Relationship,我的申请有三个实体:作者、书籍、评论。书籍和评论之间的关系是一对多的。当我想删除Review时,一切都正常,没有例外,但from DB row并没有删除 我认为hibernate可以在书评中保持书评的大小,并且不允许从数据库中删除。如果这是真的,我如何解决这个问题 或者可能是另一个地方的根本问题?需要帮助:) 账簿实体: @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "bookId", unique = tr
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "bookId", unique = true, nullable = false)
private Integer bookId;
@Size(min = 4, max = 50)
@Column(name = "name")
private String name;
@Column(name = "yearPublished")
private Integer yearPublished;
@Column(name = "description")
private String description;
@Pattern(regexp = "^([0-9]{3})([-])([0-9]{1})([-])([0-9]{4})([-])([0-9]{4})([-])([0-9]{1})", message = "Not valid isbn, XXX-X-XXXX-XXXX-X")
@Column(name = "isbn", unique = true)
private String isbn;
@Size(min = 4, max = 50)
@Column(name = "publisher")
private String publisher;
@Column(name = "createDate")
private Date createDate;
@Enumerated(EnumType.STRING)
@Column(name = "status")
private Status status;
@Column(name = "averageRating")
private Double averageRating;
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "booksAuthor", joinColumns = { @JoinColumn(name = "bookId") }, inverseJoinColumns = {
@JoinColumn(name = "authorId") })
private List<Author> authors = new ArrayList<Author>();
@OneToMany(mappedBy = "book", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<Review> reviews;
My persistence.xml:
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="persistence" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/mysql</jta-data-source>
<class>com.softserve.model.Author</class>
<class>com.softserve.model.Book</class>
<class>com.softserve.model.Review</class>
<properties>
<property name="hibernate.show_sql" value="true" />
<property name="format_sql" value="true" />
<property name="use_sql_comments" value="true" />
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
公共类AbstractGenericDAOImpl实现GenericDAO{
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractGenericDAOImpl.class);
@PersistenceContext(unitName = "persistence", type = PersistenceContextType.TRANSACTION)
protected EntityManager em;
protected Class<E> entityClass;
public AbstractGenericDAOImpl() {
}
public AbstractGenericDAOImpl(Class<E> entityClass) {
this.entityClass = entityClass;
}
public void create(E entity) {
try {
LOGGER.info("void create{}({})", entityClass, entity);
em.persist(entity);
} catch (Exception e) {
LOGGER.error("Exception: {}", e);
}
}
public E readById(I id) {
LOGGER.info(entityClass.getCanonicalName() + " readbyPk({})", id);
E entity = (E) em.find(entityClass, id);
return entity;
}
public void update(E entity) {
try {
LOGGER.info("void update{}({})", entityClass, entity);
em.merge(entity);
} catch (Exception e) {
LOGGER.error("Exception: {}", e);
}
}
public void removeByPk(I id) {
try {
LOGGER.info("remove {} by pk {}", entityClass, id);
E e = em.find(entityClass, id);
em.remove(e);
} catch (Exception e) {
LOGGER.error("Exception: {}", e);
}
}
private static final Logger Logger=LoggerFactory.getLogger(AbstractGenericDAOImpl.class);
@PersistenceContext(unitName=“persistence”,type=PersistenceContextType.TRANSACTION)
受保护的实体管理器em;
保护类实体类;
公共抽象GenericDaoImpl(){
}
公共抽象GenericDaoImpl(类entityClass){
this.entityClass=entityClass;
}
公共作废创建(E实体){
试一试{
info(“void create{}({})”,entityClass,entity;
em.persist(实体);
}捕获(例外e){
LOGGER.error(“异常:{}”,e);
}
}
公共E readById(I id){
info(entityClass.getCanonicalName()+“readbyPk({})”,id);
E实体=(E)em.find(entityClass,id);
返回实体;
}
公共作废更新(E实体){
试一试{
info(“void update{}({})”,entityClass,entity;
合并(实体);
}捕获(例外e){
LOGGER.error(“异常:{}”,e);
}
}
公共无效删除BYPK(I id){
试一试{
info(“通过pk{}删除{}”,entityClass,id);
E=em.find(entityClass,id);
em.remove(e);
}捕获(例外e){
LOGGER.error(“异常:{}”,e);
}
}
}
删除书籍或作者作品好!Only can not remove Review看起来您忘了调用flush()来写入对数据库的更改您到底遇到了什么异常?我没有异常,但行没有从数据库中删除
@Stateless
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractGenericDAOImpl.class);
@PersistenceContext(unitName = "persistence", type = PersistenceContextType.TRANSACTION)
protected EntityManager em;
protected Class<E> entityClass;
public AbstractGenericDAOImpl() {
}
public AbstractGenericDAOImpl(Class<E> entityClass) {
this.entityClass = entityClass;
}
public void create(E entity) {
try {
LOGGER.info("void create{}({})", entityClass, entity);
em.persist(entity);
} catch (Exception e) {
LOGGER.error("Exception: {}", e);
}
}
public E readById(I id) {
LOGGER.info(entityClass.getCanonicalName() + " readbyPk({})", id);
E entity = (E) em.find(entityClass, id);
return entity;
}
public void update(E entity) {
try {
LOGGER.info("void update{}({})", entityClass, entity);
em.merge(entity);
} catch (Exception e) {
LOGGER.error("Exception: {}", e);
}
}
public void removeByPk(I id) {
try {
LOGGER.info("remove {} by pk {}", entityClass, id);
E e = em.find(entityClass, id);
em.remove(e);
} catch (Exception e) {
LOGGER.error("Exception: {}", e);
}
}