关于 hibernate-spring-dao 的问题

发布于 2024-08-23 18:43:14 字数 1030 浏览 2 评论 0原文

我有一个 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,请对我轻松点)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

神回复 2024-08-30 18:43:14

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

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

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);
}
青朷 2024-08-30 18:43:14

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

您应该查看参考手册中的章节关于使用 HQL(Hibernate 查询语言),它可以让您做到这一点。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文