aspose file tools*
The moose likes Object Relational Mapping and the fly likes Hibernate : checking for null values before updating DB Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate : checking for null values before updating DB" Watch "Hibernate : checking for null values before updating DB" New topic
Author

Hibernate : checking for null values before updating DB

Harish V Gupta
Ranch Hand

Joined: Dec 17, 2011
Posts: 37
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.
Emanuel Kadziela
Ranch Hand

Joined: Mar 24, 2005
Posts: 186

... 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

Joined: Dec 17, 2011
Posts: 37
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

Joined: Mar 24, 2005
Posts: 186
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.
 
wood burning stoves
 
subject: Hibernate : checking for null values before updating DB