views:

51

answers:

2

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)

A: 

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.

matt b
@matt b I went trought it and still nothing .. do you perhaps something more from ground up .. this seems pretty advanced to me
c0mrade
+1  A: 

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);
}
joekutner