Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate Delete Not Null Columns

 
Leo Madrid
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everyone.

I'm trying to do a simple delete from one lowly table called 'Employees'. The table has a unique ID 'emp_no' and several columns.
The columns are all 'not null' in the table and in the mapping file, Employees.java. I'm using the code below to perform the delete.

This is the error I get


Now, I know why I'm getting an error message and I know that all I need to do is remove the 'nullable = false' from the getter methods.
But I'm hoping there is another way. If I have the unique ID for this particular record, why should I be forced to populate the rest of the properties?
This same example works fine in HQL and NativeSQL even with the 'not null' in place for all the properties.

Any help would be appreciated. Thanks.
Update : I should mention that I'm using Hibernate 3.3 and Java 6
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not really familiar with hibernate but don't you want:



By the way why is the class named Employees when it only represents a single employee?
 
Arun Kumarr
Ranch Hand
Posts: 661
Eclipse IDE Java Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouter Oet wrote:I'm not really familiar with hibernate but don't you want:



By the way why is the class named Employees when it only represents a single employee?


-- When this is correct, you need to get it from the database to delete. Another quick and dirty solution would be insert temporary values in the not null fields and then fire a delete.
Hibernate anyway would not use them when it is firing a query.

say, Employees has empNo, firstName and lastName as not null fields. We can insert the correct empNo value and any incorrect value (by incorrect, I mean the value which doesn't reflect the actual value in the database) and fire a delete.

Hibernate would simply do, "delete from employees where empNo = 10022".

But this is very dirty. If there is some program routine which intercepts the Employees object and does some processing before delete, then you might end up having wrong information about the delete operation.
 
Leo Madrid
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouter Oet wrote: By the way why is the class named Employees when it only represents a single employee?
Its just a test table.

Arun Kumarr wrote: Another quick and dirty solution would be insert temporary values in the not null fields and then fire a delete.
Hibernate anyway would not use them when it is firing a query.
This solution is better than my solution of removing the not null from the setter methods. Since there doesn't seem to be a perfect solution, I imagine this is one of those things in Hibernate that I will just have to work around.

Thanks Guys.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic