简体   繁体   中英

Insert and update data in table using Java Persistence Query Language

I need to update and insert data in table using Java Persistence Query Language. Here is my DAO class code:

public class ApprovalDao {


    @Autowired
    private SessionFactory sessionFactory;


    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }
    public void addEntity(EntityValues entity) {
        getSessionFactory().getCurrentSession().save(entity);
    }


    public void deleteEntityValue(EntityValues entity) {
        getSessionFactory().getCurrentSession().delete(entity);
    }

At first, if you use @Autowired annotation setter/getteer methods are redundant. In your case you should youse something like this:

session = sessionFactory.openSession();
session.beginTransaction();
session.update(user);
session.getTransaction().commit();

And surround it with try/catch construction.

for updating data of fields in table :

public void updateField(){
    Query q=getSessionFactory().getCurrentSession().createQuery(
        " update SapOrderHDR set
            SAP_ORDER_STATUS = 'A' where BC_ORDER_NO='" + bcOrderNo + "'");
    q.executeUpdate();
}

This query will update table where BC_ORDER_NO=var and set BC_ORDER_NO ='A' . SapOrderHDR is table entity class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM