This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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.
... 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
Joined: Dec 17, 2011
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?
Joined: Mar 24, 2005
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.