ant
ant

Reputation: 22948

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)

Upvotes: 2

Views: 263

Answers (2)

codefinger
codefinger

Reputation: 10338

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

Upvotes: 1

matt b
matt b

Reputation: 140041

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.

Upvotes: 0

Related Questions