java - Timezone name stored in database is incorrect using hibernate/JPA -
i using hibernate 4.2 jpa , oracle 11g database. using simple java program store date timezone information in database.below defination of table
create table "aw"."aw_request_details_test" ( "id" number, "request_type" varchar2(20 byte), "requestor" varchar2(20 byte), "requestor_region" varchar2(20 byte), "requestor_type" varchar2(20 byte), "event_datetime" timestamp (6), "event_type_id" number, "printed_mtrl" varchar2(1 byte), "is_location_usa" varchar2(1 byte), "is_analyst_attestation" varchar2(1 byte), "analyst_attestation" varchar2(500 byte), "analyst_disclosure" varchar2(500 byte), "analyst_derv_pos" varchar2(1 byte), "analyst_pos_details" varchar2(500 byte), "last_modified_by" varchar2(20 byte), "last_modified_date" timestamp (6), "req_created_by" varchar2(20 byte), "req_create_date" timestamp (6) time zone default current_timestamp, "request_status" varchar2(50 byte), "process_instance_id" number, "rsch_apprvl_file" blob, "is_apprvl_by_rsch" varchar2(1 byte), "is_apprvl_by_corp" varchar2(1 byte), "corp_apprvl_file" blob, "taskready_id" number, "taskready_name" varchar2(50 byte), "reqcompdiscussdetialscs" varchar2(255 byte), "reqcompmaydiscussdetailscs" varchar2(255 byte), "reqcontdiscussdetailscs" varchar2(255 byte), "reqcontmaydiscussdetailscs" varchar2(255 byte) )
this java bean class appropriate hibernate annotation contains mapping above table.
@entity @table(name = "aw_request_details_test") public class mediarequestdetails implements wfpayload , serializable{ /** * */ private static final long serialversionuid = 1l; @id @sequencegenerator(name="request_id_seq", sequencename="request_id_dbseq", allocationsize=1, initialvalue = 1) @generatedvalue(strategy = generationtype.sequence, generator = "request_id_seq") @column(name = "id", nullable = false) long requestid;//id of request @column(name="request_type", nullable = false) string requesttype;//type of request media/non media @column(name="requestor", nullable = false) string requestor;//soe id of requestor @column(name="requestor_region") string requestorregion;//this auto populated once requestor been added. @column(name="requestor_type") string requestortype;//requestortype obo,analyst or coprorate affairs @column(name="event_datetime") //@temporal(temporaltype.timestamp) @org.hibernate.annotations.type(type = "com.cira.raws.mediawf.bean.impl.utctimestamptype") date eventdate;//date of event // please capture sate timezone @column(name="event_type_id") long eventtype;//type of event @column(name="printed_mtrl") @type(type="yes_no") boolean printedmtrl;//printed material produced or not @column(name="is_location_usa") @type(type="yes_no") boolean locationisusa;//flag take place in usa or not @column(name="is_analyst_attestation") @type(type="yes_no") boolean hasanalystatts; @column(name="analyst_attestation") string analystatts;//analystattastation @column(name="analyst_disclosure") string analystdisclosure;//analystdisclosure @column(name="analyst_derv_pos") @type(type="yes_no") boolean analystdervpos;//information regarding derivative position(y/n) @column(name="analyst_pos_details") string analystposdetails;//information regarding disclosure @column(name="last_modified_by") string lastmodifiedby;//media request modified name @column(name="last_modified_date") @temporal(temporaltype.timestamp) date lastmodifieddate;//date of media request modified @column(name="req_created_by") string createdby;//media requestor creator user soe id. @column(name="req_create_date") //temporal(temporaltype.timestamp) @org.hibernate.annotations.type(type = "com.cira.raws.mediawf.bean.impl.utctimestamptype") date createdate;//media requestor creator user soe id. @column(name="request_status") string requeststatus;//setting status of request @column(name="process_instance_id") long processinstaceid; @column(name="taskready_id") long taskreadyid; @column(name="taskready_name") string taskreadyname; @column(name="rsch_apprvl_file") @lob byte[] rschapprvlfile;//attached approval of research management group @column(name="corp_apprvl_file") @lob byte[] coprapprvlfile;//attached approval of coprorate group. @column(name="is_apprvl_by_corp") @type(type="yes_no") boolean hasapprovedbycorporate;//is request been approved corporate @column(name="is_apprvl_by_rsch") @type(type="yes_no") boolean hasapprovedbyrschmgmt;//is request been approved rsch management //rest of getters , setters }
i have created custom type utctimestamptype convert current date/time information utc timezone , store in database. below source code it.in below class have passed calendar.getinstance(utc) object st.settimestamp convert current date/time in ist timezone utc.
public class utctimestamptypedescriptor extends timestamptypedescriptor { /** * */ private static final long serialversionuid = 1l; public static final utctimestamptypedescriptor instance = new utctimestamptypedescriptor(); private static final timezone utc = timezone.gettimezone("utc"); public <x> valuebinder<x> getbinder(final javatypedescriptor<x> javatypedescriptor) { return new basicbinder<x>( javatypedescriptor, ) { @override protected void dobind(preparedstatement st, x value, int index, wrapperoptions options) throws sqlexception { system.out.println("do bind method gets called " + value); st.settimestamp( index, javatypedescriptor.unwrap( value, timestamp.class, options ), calendar.getinstance(utc) ); } }; } public <x> valueextractor<x> getextractor(final javatypedescriptor<x> javatypedescriptor) { return new basicextractor<x>( javatypedescriptor, ) { @override protected x doextract(resultset rs, string name, wrapperoptions options) throws sqlexception { system.out.println("do extract method gets called "); return javatypedescriptor.wrap( rs.gettimestamp( name, calendar.getinstance(utc) ), options ); } }; } }
below main program execute store data in table aw_request_details_test. below program executed jvm having timezone ist.
public class testrawsconnection { public static void main(string args[]) throws exception{ entitymanager entitymanager = persistence.createentitymanagerfactory("rawsjpa").createentitymanager(); if(entitymanager!=null){ system.out.println("************* entitymanager obtained 1234*****************"); date dt=new date(); //date finaldate=convertdateutc(dt); entitymanager.gettransaction().begin(); mediarequestdetails obj=new mediarequestdetails(); obj.setrequestor("ns52289"); obj.setrequestorregion("usa"); obj.setrequestortype("obo"); obj.setrequesttype("mediaappr"); obj.seteventdate(dt); obj.seteventtype(12); obj.setlocationisusa(true); obj.setprintedmtrl(false); obj.setlocationisusa(false); obj.sethasanalystatts(true); obj.setanalystatts("analystattestation"); obj.setanalystdisclosure("analystdisclosure"); obj.setanalystdervpos(true); obj.setanalystposdetails("analystposdetails"); obj.setlastmodifiedby("jinesh"); obj.setlastmodifieddate(dt); obj.setcreatedby("jinesh123"); obj.setcreatedate(dt); obj.setrequeststatus("pending"); obj.setprocessinstaceid(1234l); obj.settaskreadyid(1245l); obj.settaskreadyname("jinesh"); obj.sethasapprovedbycorporate(true); obj.sethasapprovedbyrschmgmt(true); obj.setcoprapprvlfile(new string("jinesh parikh").getbytes()); obj.setrschapprvlfile(new string("sejal mehta").getbytes()); entitymanager.persist(obj); entitymanager.gettransaction().commit(); system.out.println("committing data done"); } //entitymanager.gettransaction().commit(); system.out.println("****************** existed fetching record *************************"); } }
above program works absolutly fine. stores date/time in column (req_create_date) per utc timezone name of timezone stored in column asia/calcutta of jvm , incorrect expecting should utc.my database server in est time zone. below values when execute select query.
select req_create_date aw_request_details_test; 10-jun-15 02.13.48.000000000 pm asia/calcutta 10-jun-15 02.16.41.513000000 pm asia/calcutta
in above case date/time info correct timezone info incorrect? know how solve ?
Comments
Post a Comment