Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6230389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:44:14+00:00 2026-05-24T09:44:14+00:00

In a web app,I am letting the user create an Item .The db manipulations

  • 0

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);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T09:44:15+00:00Added an answer on May 24, 2026 at 9:44 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Background I have a web app that will create an image from user input.
A web app I am writing in JavaScript using node.js. I use Foreman, but
Im trying to create a website with Web.py but its not letting me open
I am about 70% of the way through developing a web application which contains
My web app generates a CSV file on the fly, but whenever I use
My web-app has this secret place only some people are allowed to use. When
I am currently developing an asp.net web app. I have a class which is
My web app has many jquery custom components, in which most of them needs
When developing a mobile app, and letting the user take photos (That later will
I have a web app in which I have set the maximum inactivity time

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.