Friday, January 8, 2010

Hibernate Id generation using sequence

Mapped classes must declare the primary key column of the database table. The element is used to define the mapping property to the primary key column.
We can specify the id with class to generate the auto increment value for this id property.
We can use hibernate generated sequence or data base sequence.

Hibernate sequence we can use the “increment” and “native” to use auto increment of the value.
We “increment” for numeric data to auto increment the data, “native” is also used same purpose but the hibernate will select the appropriate value based on the data base. For example on oracle native, increment as same effect which is used to get the value from “hibernate_sequence.NEXT_VALUE”.

To make the id has the database sequence then we need pass the sequence name with
element.



So while you are saving the object it is not required to the value for this property, since we set the value it overrides the value with the sequence generated value.
If we try to persist the object with the by setting the id value, it throws the exception entity cache is detached or not persistent object.
Note:- we can mix 2 strategies for the id property.

Saturday, October 24, 2009

SQL Supports two date/time functions NEXT_ DAY and LAST_DAY using this functions we can find records between dates.

Syntax: NEXT_DAY (, ) 

LAST_DAY (date) 

Options are SUN, MON, TUE, WED, THU, FRI, and SAT


I give some examples which will explain how to find the THIS WEEK, Next WEEK and THIS MONTH, NEXT MONTH.
Note:- Assuming SUNDAY - SATURDAY as week


1) Finding this week dates


WHERE pending_date between TO_CHAR (NEXT_DAY(sysdate - 7, 'SUN'), 'dd-Mon-yyyy') and TO_CHAR (NEXT_DAY(sysdate - 1, 'SAT'), 'dd-Mon-yyyy')


2) Finding next week dates


WHERE pending_date between TO_CHAR (NEXT_DAY(sysdate, 'SUN'), 'dd-Mon-yyyy') and TO_CHAR (NEXT_DAY(sysdate + 6, 'SAT'), 'dd-Mon-yyyy')


3) Finding this month dates


TO_CHAR (TRUNC(sysdate, 'MM'), 'dd-Mon-yyyy') and TO_CHAR (LAST_DAY(sysdate), 'dd-Mon-yyyy')


4) Finding next month dates


TO_CHAR (LAST_DAY(sysdate) + 1, 'dd-Mon-yyyy') and TO_CHAR (LAST_DAY(ADD_MONTHS(sysdate)), 'dd-Mon-yyyy')


Quick Points :


1) LAST_DAY returns last day of the month.
2) NEXT_day returns next occurance of the given week in it.
3) TRUNC(sysdate, 'MM') always returns starting date of the month.
4) ADD_MONTHS(sysdate, 2) adds 2 months to current date. e.g: present month is oct then it will become    DEC. if you use -2 negative number it will reduce in our case it becomes AUG.