user471011 user471011 - 1 year ago 219
Java Question

HibernateTemplate. How delete by Id?

For work with database my class extends

class and inside class methods I use

So, for delete row in database I use this method:

public void delete(MyObject obj)

all ok!

But, at this moment I try implement method, that can delete row based on Id:

public void delete(final long id)
// some code here

And I cant find HibernateTemplate method some like this:

How solution for me in this case?

Answer Source

delete using particular id,

public void delete(long id)
    Session session ;
    MyObject myObject ;

    session = sessionFactory.getCurrentSession();
    myObject = (MyObject)session.load(MyObject.class,id);

    //This makes the pending delete to be done
    session.flush() ;


Also consider encapuslate this methods in try/catch/finally and log the error as needed

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download