1

I am stuck over hibernate delete having collection type that is giving exception with foreign constraints.

Here is my Hibernate table

@Entity
@Table
public class FrontRequest implements Serializable {

    private static final long serialVersionUID = 1L;


    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    private Long tutorfront_id;

    @Column
    private String tutorialType;

    @Column
    private String tutorialLevel;

    @ElementCollection(fetch=FetchType.EAGER)
    @Fetch(value = FetchMode.SELECT)
    private Collection<String> tutorialSubjects= new HashSet<String>();

    @Column
    private String tutorialCity;

    @Column
    private String noOfStudent;

    @Column
    private String classWeek;

    @Column
    private String tutionFee;

    @Column
    private String tutionFeerate;

    @Column
    private String tutorSex;

    @Column
    private String tutorEducation;


    @Column
    private Date postingDate;

    @Column
    private String studentSchool;
}

and here is my service method to delete the object

public boolean removeFrontRequest(Long id) {
        // TODO Auto-generated method stub
        Session session= SessionFactoryImpl.returnService().getCurrentSession();
        Transaction tx;

        if(session.getTransaction() != null
                && session.getTransaction().isActive()){
            tx=session.getTransaction();
        }
        else{
            tx= session.beginTransaction();
        }


        try{

            Query query=session.createQuery("from FrontRequest where tutorfront_id =:id");
            query.setParameter("id", id);
             FrontRequest frontRequest=(FrontRequest) query.list().get(0);
             session.delete(frontRequest);
             if(!tx.wasCommitted()) {
                    tx.commit();
             }
        }
        catch(Exception e){
        return false;
        }
        return true;
    }

But it is giving exception with lock: SQL Error: 1205, SQLState: 40001 Lock wait timeout exceeded; try restarting transaction

Please can you suggest where I am doing wrong. Here..

Thanks

  • Have a look at [this SO post.](http://stackoverflow.com/questions/5836623/getting-lock-wait-timeout-exceeded-try-restarting-transaction-even-though-im) It might be helpful. – rcyza Mar 24 '16 at 11:07
  • Some other process has a lock on the table or record which you are trying to delete. Find out what it is and stop it. Read [here](http://www.coderanch.com/mobile/t/447114/ORM/databases/Error-Deleting-Persisted-Objects-Hibernate?foo=a). – Tim Biegeleisen Mar 24 '16 at 11:08

1 Answers1

0

correct your if condition by replacing != with ==. Also end the transaction after committing.

Rahul
  • 309
  • 1
  • 11