Abu Bakar Siddik
Abu Bakar Siddik

Reputation: 527

How to delete an object in jpa

I want to delete an object. When I debug code its work well, but object didn't delete from DB. Look at following below.

xhtml code:

    <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 Codes:

ManageBean Method Here:

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

Here is StaffRegiAddressInfo Entity:

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

Here is StaffRegiBasicInfo Entity:

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

I am using Spring 4, Hibernate 4, primefaces 5.2

Upvotes: 0

Views: 3627

Answers (1)

Sai prateek
Sai prateek

Reputation: 11906

The EntityManager.remove() operation is used to delete an object from the database. remove does not directly delete the object from the database, it marks the object to be deleted in the persistence context (transaction). When the transaction is committed, or if the persistence context is flushed, then the object will be deleted from the database.

The remove operation can only be called within a transaction, an exception will be thrown outside of a transaction. The remove operation must be called on a managed object, not on a detached object. Generally you must first find the object before removing it, although it is possible to call EntityManager.getReference() on the object's Id and call remove on the reference. Depending on how you JPA provider optimizes getReference and remove, it may not require reading the object from the database.

For example (in case of Bean managed transaction)

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

For detail go through Documentation Link.

Upvotes: 2

Related Questions