如何使用 dao、hibernate 检查同名项目是否存在

发布于 2024-11-27 23:48:44 字数 1089 浏览 3 评论 0原文

在网络应用程序中,我让用户创建一个Item。数据库操作是通过dao 实现完成的 它使用hibernate。通用dao实现使用Criteria进行查询。

项目的名称是唯一的。因此,我必须防止用户创建两个具有相同名称的项目。 我应该如何在代码中执行此操作?每次用户尝试创建项目时,我应该调用 itemDao.findItemByName(newname), 如果项目存在,则给用户一条错误消息?或者我应该将项目创建代码放在 try catch 块中,捕获异常, 并告诉用户,尝试创建新项目失败了?

在我看来,第一种方法可以让我向用户提供更精确的错误消息。但它会涉及一个数据库检查调用 对于每次创建 item 的尝试。第二个是从 dao 类中升起的一些异常,并且不太具体。

我很感激对此的一些建议..

真诚的

Jim

GenericDaoImpl

...
public T findUniqueItemByProperty(String propName,String propVal){
    Class clz = getPersistentClass();
    Session session = getSession();
    logger.info("session="+session.hashCode());
    Criteria cri = session.createCriteria(clz).add(Restrictions.eq(propName,propVal));
    return (T)cri.uniqueResult();
}

public void saveOrUpdate(T obj) {
    getSession().saveOrUpdate(obj);
}
...

ItemDao

...
public Item findItemByName(String name){
    return findUniqueItemByProperty("name",name);
}
public void saveOrUpdateItem(Item item){
    saveOrUpdate(item);
}

In a web app,I am letting the user create an Item.The db manipulations are done through dao implementations
which use hibernate
.The generic dao implementation uses Criteria for the query

The name of an Item is unique.So,I have to prevent a user from creating two items with the same name.
How should I do this in the code?Each time a user attempts to create an item,should I call itemDao.findItemByName(newname),
and if an item exists, give the user an error message?Or should I put the item creation code in a try catch block,catch exception,
and tell the user,the attempt to create a new item failed?

It seems to me ,the first approach will let me give a more precise error message to the user.But it will involve one db check call
for every attempt to create item.The second will be some exception boiling up from the dao class and less specific.

I would appreciate some advice on this..

sincerely

Jim

GenericDaoImpl

...
public T findUniqueItemByProperty(String propName,String propVal){
    Class clz = getPersistentClass();
    Session session = getSession();
    logger.info("session="+session.hashCode());
    Criteria cri = session.createCriteria(clz).add(Restrictions.eq(propName,propVal));
    return (T)cri.uniqueResult();
}

public void saveOrUpdate(T obj) {
    getSession().saveOrUpdate(obj);
}
...

ItemDao

...
public Item findItemByName(String name){
    return findUniqueItemByProperty("name",name);
}
public void saveOrUpdateItem(Item item){
    saveOrUpdate(item);
}

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

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

发布评论

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

评论(2

就是爱搞怪 2024-12-04 23:48:44

两者都有效。亚历克斯在他的回答中给出了一些其他选择。如果您担心性能,通常不需要担心。按照看起来正确的方式来写。稍后在证明需要时进行优化。与其他编写方式相比,这段代码非常干净且易于理解:

if (dao.checkForExistence(something)) {
    return duplicateSomethingResponse();
}
dao.makePersistent(something);

Either is valid. Alex gives some other alternatives in his answer. If you're worried about performance, you usually don't need to. Write it the way that seems right. Optimize later, when it's proven to be needed. This code is very clean and easy to understand compared to how else it could be written:

if (dao.checkForExistence(something)) {
    return duplicateSomethingResponse();
}
dao.makePersistent(something);
琴流音 2024-12-04 23:48:44

您可以使用 where name=? 进行条件更新,如果更新的行数为 0,则插入。如果您将 name 设置为键,Hibernate 将通过 saveOrUpdate 为您处理它。无论如何,名称可能应该被索引。

You can do conditional update with where name=? and if number of updated rows is 0 do insert. And if you make name a key, Hibernate will take care of it for you with saveOrUpdate. Anyway, name, probably, should be indexed.

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