如何删除jpa中的对象

时间:2016-03-01 09:27:37

标签: jpa

我想删除一个对象。当我调试代码时它工作得很好,但是对象没有从DB中删除。请看下面的内容。

xhtml代码:

    <p:dataGrid value="#{staffUpdateMB.addressInfoList}"
        id="addesList" var="address" columns="1" layout="grid"
        styleClass="NoPadding NoIndent">
        <p:panel>
            <div
                class="Container100 Responsive100 TealGreenBack BordRad5 White NoIndent">

                <div class="Container50 Responsive100 NoIndent">
                    <h:outputText value="#{address.addressType}" styleClass="Fs22" />
                </div>
                <div class="Container50 Responsive100 TexAlRight NoIndent">
                    <p:commandButton class="TealGreenBack" icon="fa fa-edit"
                    onstart="PF('staffAddressEditDialog').show()">
                    <f:setPropertyActionListener value="#{address}"
                            target="#{staffUpdateMB.beanAddressInfo}"/>
                    </p:commandButton>
                    <p:commandButton styleClass="RedButton RaisedButton"
                        action="#{staffUpdateMB.removeAddress}" icon="fa fa-trash-o"
                        update="addesList" ajax="false">
                        <f:setPropertyActionListener value="#{address}"
                            target="#{staffUpdateMB.beanAddressInfo}" />
                    </p:commandButton>
                </div>
            </div>
        ... .... ..
    </p:panel>
    </p:dataGrid>

Java代码:

ManageBean方法:

    public void removeAddress() {
    try {

        System.out.println("Address ID :"+this.beanAddressInfo.getAddressID());

        addressInfoDao.remove(this.beanAddressInfo.getAddressID());


        context.addMessage(null, new FacesMessage(msg.getPropValue("deleteSuccess")));
    } catch (Exception e) {
        context.addMessage(null, new FacesMessage(msg.getPropValue("deleteError")));
        logger.error("This is error : " + e);
        logger.fatal("This is fatal : " + e);
    }
}

这是StaffRegiAddressInfo实体:

    @Entity
    @Table(name="staffregi_addressinfo")
    public class StaffRegiAddressInfo implements Serializable{


private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="addressID")
private int addressID;

@Column(name="addressType")
private String addressType;

@Column(name="village")
private String village;

@Column(name="postOffice")
private String postOffice;

@Column(name="postalCode")
private String postalCode;

@Column(name="thanaName")
private String thanaName;

@Column(name="districtName")
private String districtName;

@Column(name="divisionName")
private String divisionName;

@Column(name="countryName")
private String countryName;

@Column(name="instituteID")
private String instituteID;

@Column(name="recordNote")
private String recordNote;

@Column(name="userExecuted")
private String userExecuted;

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

@Column(name="ipExecuted")
private String ipExecuted;

@Column(name="recordStatus")
private int recordStatus;


@ManyToOne
@JoinColumn(name="staffID")
private StaffRegiBasicInfo basicInfoAddress;
    //setter, getter also..

这是StaffRegiBasicInfo实体:

    @Entity
    @Table(name="staffregi_basicinfo")
    public class StaffRegiBasicInfo implements Serializable{


     private static final long serialVersionUID = 1L;

     @Id
     @Column(name="staffID")
     private String staffID;

    @OneToMany(cascade=CascadeType.ALL, mappedBy="basicInfoAddress")
    @LazyCollection(LazyCollectionOption.FALSE)
    private Set<StaffRegiAddressInfo> addressInfoList;

我正在使用Spring 4,Hibernate 4,primefaces 5.2

1 个答案:

答案 0 :(得分:0)

EntityManager.remove()操作用于从数据库中删除object。 remove不会直接从数据库中删除对象,它会在persistence context(事务)中标记要删除的对象。提交事务时,或者如果刷新persistence context,则将从数据库中删除该对象。

remove操作只能在事务中调用,异常将在事务之外抛出。必须在托管对象上调用remove操作,而不是在分离的对象上调用。通常,您必须先删除对象,然后才能删除它,尽管可以在对象的ID上调用EntityManager.getReference()并在引用上调用remove。根据JPA提供程序优化getReference和remove的方式,可能不需要从数据库中读取对象。

例如(如果是Bean managed transaction

EntityManager em = getEntityManager();
em.getTransaction().begin();
Employee employee = em.find(Employee.class, id);
em.remove(employee);
em.getTransaction().commit();

有关详细信息,请浏览Documentation Link.