EntityManager“删除”不会删除

时间:2014-06-04 17:01:39

标签: jpa hibernate-entitymanager remove-method

我有很多实体,对于CUD操作,我分别使用persist(),merge()和remove()方法。但是下面的删除方法不起作用。所以我不得不使用查询来删除给定的对象。我用谷歌搜索了它,但找不到任何令人满意的答案。

@Stateless
@LocalBean
public class PickListFacade {

    @PersistenceContext
    private transient EntityManager  em;

    // ...

    public boolean deletePickList(final PickList pickList) {
        try {
            // below commented out line did not work; so one below that is employed
            // this.em.remove(this.em.find(PickList.class, pickList.getId()));
            this.em.createQuery("DELETE FROM PickList p WHERE p.id = :id").setParameter("id", pickList.getId()).executeUpdate();
            return true;
        } catch (final PersistenceException pe) {
            logger.error("[deletePickList] : Error : {}", pe.getMessage(), pe);
            return false;
        }
    }
}

为了简化起见,我用Tuple替换了PickList。但并不认为这会引起混乱。所以这就是:原始代码......

@Entity
@Table(name = "PICK_LIST_VALUES")
public class PickListValue implements Serializable, ILabelValue {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "list_value_generator")
    @TableGenerator(initialValue = 1, 
                    allocationSize = 1, 
                    name = "list_value_generator", 
                    table = "SEQUENCES", 
                    pkColumnName = "SEQUENCE_NAME", 
                    valueColumnName = "SEQUENCE_NEXT_HI_VALUE", 
                    pkColumnValue = "PICK_LIST_VALUES")
    @Column(name = "PICK_LIST_VALUE_ID")
    private Long              id;

    @Column(name = "PICK_LIST_VALUE")
    private String            value;

    @Column(name = "PICK_LIST_VALUE_CODE")
    private Long              code;

    @Column(name = "RANK")
    private Integer           rank;

    // bi-directional many-to-one association to PickList
    @ManyToOne
    @JoinColumn(name = "PICK_LIST_ID")
    private PickList          pickList;

    public PickListValue() {
    }

    // getters and setters ...
}

@Entity
@Table(name = "PICK_LISTS")
public class PickList implements Serializable {

    private static final long   serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "list_generator")
    @TableGenerator(initialValue = 1,
                    allocationSize = 1,
                    name = "list_generator",
                    table = "SEQUENCES",
                    pkColumnName = "SEQUENCE_NAME",
                    valueColumnName = "SEQUENCE_NEXT_HI_VALUE",
                    pkColumnValue = "PICK_LISTS")
    @Column(name = "PICK_LIST_ID")
    private Long                id;

    @Column(name = "PICK_LIST_NAME", unique = true)
    private String              name;

    @Column(name = "PICK_LIST_DESCRIPTION")
    private String              description;

    @Column(name = "CREATED_BY")
    private String              createdBy;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "DATE_CREATED")
    private Date                dateCreated;

    @OneToMany(mappedBy = "pickList", fetch = FetchType.EAGER, cascade = { CascadeType.ALL }, orphanRemoval = true)
    private List<PickListValue> pickListValues;

    public PickList() {
    }

    // getters and setters ...
}

0 个答案:

没有答案