0

My delete operation is not deleting recode in database.

My entity classes :

    @Entity  
@Table(name= "User")
public class User {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="id")
    private int id;  
    private String userName,password,type,fullName; 

    public int getId() {  
        return id;  
    }  
    public void setId(int id) {  
        this.id = id;  
    }  

    public String getFullName() {  
        return fullName;  
    }  
    public void setFullName(String fName) {  
        this.fullName = fName;  
    }

    @Column(name = "USER_ID")
    public String getUserName() {  
        return userName;  
    }  
    public void setUserName(String userName) {  
        this.userName = userName;  
    }  
    public String getPassword() {  
        return password;  
    }  
    public void setPassword(String password) {  
        this.password = password;  
    }     
    public String getType() {  
        return type;  
    }  
    public void setType(String type) {  
        this.type = type;  
    }     



}

    @Entity  
@Table(name= "Cart")
public class Cart {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="id")
    private int id; 
    private String userName;    
    private String itemSkus;
    private String itemsQuantity;


    public int getId() {  
        return id;  
    }  
    public void setId(int id) {  
        this.id = id;  
    } 

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "USER_ID")
    public String getUserName() {  
        return userName;  
    }  
    public void setUserName(String uName) {  
        this.userName = uName;  
    }

    public String getItems(){
        return this.itemSkus;
    }

    public void setItems(String items){
        this.itemSkus=items;
    }


    public String getItemsQuantity(){
        return this.itemsQuantity;
    }

    public void setItemsQuantity(String itemsQ){
        this.itemsQuantity=itemsQ;
    }



}

The DAO class method is for cart is as below:

public int deleteCart(String userID){
    Session currentSession=sessionObject.getSession();
    //String query="from Cart Where  userName :uName";
    String query="DELETE FROM Cart where userName = :uName";

    Query queryResult = currentSession.createQuery(query);
    queryResult.setParameter("uName", userID);

    int result = queryResult.executeUpdate();

    System.out.println("Cart delete result "+result); 

    return result;
}

I tried running query in MySQL workbench and table updated nicely. Only with hibernate its not updating.

I end up having number of results in update query result.

2 Answers 2

1

put the quesry inside a transaction, begin it, execute the delete query and finally commit the transaction and rollback it when an error or exception occured:

public int deleteCart(String userID){

    Session currentSession=sessionObject.getSession();
    Transaction tr= currentSession.beginTransaction();
    //String query="from Cart Where  userName :uName";
  try{
    String query="DELETE FROM Cart where userName = :uName";

    Query queryResult = currentSession.createQuery(query);
    queryResult.setParameter("uName", userID);

    int result = queryResult.executeUpdate();
    tr.commit();

    System.out.println("Cart delete result "+result); 

    return result;
  } catch (Exception ex) {
       tr.rollback();
       return null;
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Your session needs to be flushed. Add this line before you prinltln like this.

currentSession.flush();
System.out.println("Cart delete result "+result);

3 Comments

question owner says:_with hibernate its not updating._ notice: flush() will sync the database with the current state of object/objects held in the memory but it does not commit the transaction(depends on flash mode). but commit() will update the database permanent. he can use the flash after updating and any time he want
Farhang Amary 'Ferhęg'. Hibernate's default mode will flush on: +before some query executions, +from org.hibernate.Transaction.commit() +from Session.flush() This answers the owner's question - "hibernate its not updating" Reference:docs.jboss.org/hibernate/orm/3.3/reference/en/html/…
Great, so read the link carefully and do some tests then you'll get this result: when tr.commit() is called in the default FlushMode , currentSession.flush() will be executed implicitly before execution of tr.commit(). good luck.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.