• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Hibernate : checking for null values before updating DB

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys, i need to upload contect of an excel sheet into the database. There are 5 different types of sheet with different number of columns. In the sheet, some of the columns should not be null. There is one table each for these 5 different types of sheet. I considered all the columns which should not be null and created the tables accordingly. Now as far as Java part is concerned before updating to DB I want to check the data for being null or not based on which I will throw an error or I will proceed with the updation. One way to do it is to check it manually by giving the column names which shouldn't be empty which is a cumbersome process. I have pojo classes mapped to the tables and it's corresponding columns. Is there any easy (generic) way to do it? Tell me if you haven't got the problem.
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


... I will throw an error or I will proceed with the updation ...



If you do nothing to validate the data from the spreadsheet in Java, the database will throw the error for you when you violate the non-null constraint. You can just handle the error. If you want anything more fancy than that, I'm afraid you will have to actually implement the data validation before you try to insert/update.
 
Harish V Gupta
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Emanuel. I want to do it before i hit the DB for updation. Since i am new to hibernate i am not sure if there is any way to use the class i am using to map to DB to do the validation. Is their any way to get the nullable property of the table at least?
 
Emanuel Kadziela
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you can annotate it not-null or mark it that way in the config xml file. I'm not sure that helps much, however, as this validation will most likely also apply only at update time.

The other option is to make your domain objects do the validation. Do not allow the object to be constructed with those columns null (or without them set to something non-null), and make sure all the setters validate the input argument is not null. That way, you won't even be able to construct the domain object with the null values where you don't want them, and you will catch the errors earlier.
reply
    Bookmark Topic Watch Topic
  • New Topic