简体   繁体   中英

Question about hibernate-spring-dao

I have a DAO class which I'm using to try select/update/insert with hibernate and Mysql database. I'm writing methods for these now, I already wrote insert like this :

public Long save(People transientInstance) {
        log.debug("Saving People instance");
        try {
            Long id = (Long)getHibernateTemplate().save(transientInstance);
            log.debug("save successful with id #" + id);
            return id;
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }

I have a 3 columns, one is id , second is name , third is surname . Using the same logic how can I get person by ID or update person. Now I can wrote delete also :

public void delete(People persistentInstance) {
        log.debug("deleting People instance");
        try {
            getHibernateTemplate().delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }

I could delete or update if I could get the People object by ID but I don't know how. Thank you( yes I'm trying to learn java-spring-hibernate go easy on me pls)

It sounds like you want to do something like this:

public void updatePeople(Long id, String surname) {
    People p = getHibernateTemplate().get(People.class, id)
    p.setSurname(surname);
    getHibernateTemplate().update(p);
}

I think what you are really asking (without realizing it) is "how do I query for arbitrary non-ID fields with Hibernate?".

You should take a look at the chapter in the reference manual about using HQL (Hibernate Query Language), which will allow you to do this.

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