关于 hibernate-spring-dao 的问题
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来你想做这样的事情:
It sounds like you want to do something like this:
我认为您真正要问的(没有意识到)是“如何使用 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.