This week's book giveaway is in the Java in General forum.
We're giving away four copies of Helidon Revealed: A Practical Guide to Oracle’s Microservices Framework and have Michael Redlich on-line!
See this thread for details.
  • 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Hibernate: Default Binding of database field

 
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all:

In the database I am working with, there is a field called "modifiedDate" in every table. There is a "Default Binding" rule on that field.
Simply, if no date is passed during create or update, the database will insert the current time. However, it is a non-null field. So, I cannot pass null to it. I have to pass a data or nothing at all.

How can I map it to Hibernate beans?
If I create a new instance and don't set the 'modifiedDate', it is null by default, and Hibernate throw an error because it is non-null field.

How can I configuer Hibernate so that it recognize that there is a default binding role?

P.S. I am using SQL Server.


Thanks
[ May 22, 2007: Message edited by: Hanna Habashy ]
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would have expected that the even on passing NULL, the "Default Binding" rule (dont know much about this) would auto-generate the time. However, if that's not the way it works then how about not mapping that column to your hibernate bean at all?
 
Hanna Habashy
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I would have expected that the even on passing NULL, the "Default Binding" rule (dont know much about this) would auto-generate the time. However, if that's not the way it works then how about not mapping that column to your hibernate bean at all?



I need to read the value of that column. If I don't map it, then I won't be able to read it. Unless, I do a special query for that column alone. With all of my tables have the same column 'modifiedBy', it will be inpractical to implement a special function for each table.
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I understand your question correctly, you want the database timestamp -- not the app server timestamp.
Though the table column is not null, could you declare the property to be nullable? This would allow the property to take a null, and database to insert the default timestamp for the modified_date column. When the property was read, it would always be not null.
 
Hanna Habashy
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If I understand your question correctly, you want the database timestamp -- not the app server timestamp.
Though the table column is not null, could you declare the property to be nullable? This would allow the property to take a null, and database to insert the default timestamp for the modified_date column. When the property was read, it would always be not null.



It is a good stratgy, however it won't work.
If I declare the column to be nullable, it will fail on insert and update. The bean property that represents the column will be null, so it will pass Hibernate checking, at this point Hibernate believes that the column is nullable, so Hibernate will insert the null value in the SQL query, because the table declare the field is not null, it will throw an exception.

I found a workround solution. In the .hbn file, I declared the INSERT and UPDATE to be false. This way, Hibernate ignors the value of the property, and does not include it in the SQL query.

It works, however it would be nice if Hibernte can recogniz that there is a default binding values for columns.
 
vu lee
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Simply, if no date is passed during create or update, the database will insert the current time.


It won't work if you do need to pass date in.
 
Hanna Habashy
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by vu lee:

It won't work if you do need to pass date in.



You are abslutly right, however in this case the the column name is 'modifiedDate' as the name suggest it represents the date when the record was inserted or modified. The application should have no business setting this field. To protect the integrity of the data, the best way is to let the database handle it by inserting or updating the field using the current date and time from the database itself.
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe if the database will automatically fill in the date when insert is done. Then that is what the settings update="false" and insert="false" are supposed to do. Don't recall if you also have to set it to nullable in the mapping.

Mark
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The update="false" insert="false" do work as advertised...but what if you want the ability to sometimes supply the value instead of taking the DB default. Any ideas on how to do that in Hibernate? If I take off the insert="false" and don't supply a value, it gets saved with null (as discussed before), but if I leave it on, then it never gets passed in the insert and I can't override it.
 
Right! We're on it! Let's get to work tiny ad!
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic