【问题标题】:Question about hibernate-spring-dao关于hibernate-spring-dao的问题
【发布时间】:2010-03-05 16:47:43
【问题描述】:

我有一个 DAO 类,我用它来尝试使用 hibernate 和 Mysql 数据库进行选择/更新/插入。我现在正在为这些编写方法,我已经写过这样的插入:

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

我有 3 列,一是id,二是name,三是surname。使用相同的逻辑如何通过 ID 获取人员或更新人员。现在我也可以写删除了:

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

如果我可以通过 ID 获取 People 对象,我可以删除或更新,但我不知道如何。谢谢(是的,我正在努力学习 java-spring-hibernate,请放轻松)

【问题讨论】:

    标签: java hibernate dao


    【解决方案1】:

    听起来你想做这样的事情:

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

    【讨论】:

      【解决方案2】:

      我认为您真正要问的是(没有意识到)“如何使用 Hibernate 查询任意非 ID 字段?”。

      你应该看看the chapter in the reference manual about using HQL(Hibernate Query Language),它可以让你这样做。

      【讨论】:

      • @matt b 我试过了,但还是一无所获..您是否可能从头开始做一些更重要的事情..这对我来说似乎很先进
      猜你喜欢
      • 2010-12-27
      • 2011-03-24
      • 2011-01-27
      • 2010-12-07
      • 2011-10-01
      • 1970-01-01
      • 2011-05-06
      • 2012-02-14
      • 2018-01-10
      相关资源
      最近更新 更多