无法删除实体多对一的Ralationship

时间:2016-03-02 13:09:32

标签: java mysql hibernate entity-relationship

我的申请有3个实体:作者,书籍,评论。书与评论之间的关系是一对多的。当我想删除评论一切正常,没有例外,但从DB行不删除。

我认为hibernate可以在评论中保持对书籍大小的评论,并且不允许从DB中删除。如果这是真的,我该如何解决这个问题?

或者可能是另一个地方的根问题?需要帮助:)

图书实体:

@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;

审核实体:

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "reviewId", unique = true, nullable = false)
private Integer reviewId;

@NotNull
@Size(min = 4, max = 15)
@Column(name = "commenterName", nullable = false)
private String commenterName;

@Size(min = 1, max = 255)
@Column(name = "comment")
private String comment;

@Column(name = "rating")
private Integer rating;

@Column(name = "createDate")
private Date createDate;

@Column(name = "modified")
private boolean modified;

@Column(name = "updatedDay")
private Date updatedDay;

@ManyToOne
@JoinColumn(name = "bookId")
private Book book;

我的persistance.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>

我的GenericDAO:

@Stateless

public class 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);
    }
}

}

删除图书或作者效果很好!只能删除评论

0 个答案:

没有答案