Your SlideShare is downloading. ×
  • Like
Hibernate Import.Sql I18n
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Hibernate Import.Sql I18n

  • 983 views
Published

 

Published in Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
983
On SlideShare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
6
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. hibernate sql import.sql LTEX Documented By A http://blog.csdn.net/shadowkiss 2009-7-6 Hibernate ddl im- port.sql classpath hibernate.cfg.xml 1 < property name = " hbm2ddl . auto " > create </ property > 2 or 3 < property name = " hbm2ddl . auto " > create - drop </ property > import.sql JVM file.encoding *nix Windows OS JVM -Dfile.encoding=UTF-8 console UTF-8 SchemaExport.java : 1 package org . hibernate . tool . hbm2ddl ; 2 ... 3 public class SchemaExport { 4 ... 5 public void execute ( boolean script , boolean export , boolean justDrop , boolean justCreate ) { 1
  • 2. 6 ... 7 InputStream stream = ConfigHelper . getResourc e AsSt r eam ( importFile ) ; 8 importFileReader = new InputS treamR eader ( stream ) ; 9 ... Hibernate JVM ( new InputStreamReader ) SchemaExport (I) classpath import.sql.UTF-8 (II) import.sql 1 package cn . qdqn . bms . web . listener ; 2 3 import java . io . File ; 4 import java . io . FileOutputStream ; 5 import java . io . IOException ; 6 import java . io . InputStreamReader ; 7 import java . io . OutputSt re amWrit er ; 8 import java . io . Reader ; 9 import java . io . Writer ; 10 import java . net . URI ; 11 import java . net . URISyntaxE xc eption ; 12 13 import javax . servlet . Ser vlet C onte x tE v e nt ; 14 import javax . servlet . S er v l e t Co n te x t L i s t e n e r ; 15 16 public class HibListener implements S e r v l e t C o n t e x t L i s t e n e r { 17 18 /* * 19 * 20 */ 21 public void contextDestroyed ( Se r v le t C on t e xt E v en t sce ) { 22 23 } 2
  • 3. 24 25 /* * 26 * 27 */ 28 public void contextIn it ialize d ( S er v l et C o nt e x tE v e nt sce ) { 29 String fileName = sce . getSe rvletC ontext () . getInitParameter ( " ddl " ) ; 30 sce . getServletContext () . log ( " Convert " + fileName + " to import . sql " ) ; 31 String srcEnc = fileName . substring ( fileName . lastIndexOf ( " . " ) + 1) ; 32 // import.sql 33 File importsqlfile = null ; 34 try { 35 importsqlfile = new File ( new URI ( Thread . currentThread () 36 . ge t Con tex t Cl a s s L o a d e r () . getResource ( " " ) 37 + " import . sql " ) ) ; 38 } catch ( URISynt ax Except io n e ) { 39 e . printStackTrace () ; 40 } 41 42 // 43 Reader reader = null ; 44 Writer writer = null ; 45 try { 46 reader = new InputStreamR eader ( Thread . currentThread () 47 . ge t Con tex t Cl a s s L o a d e r () . ge tRes o urce A sS t r ea m ( fileName ) , 48 srcEnc ) ; 49 50 writer = new OutputS tr ea mW ri te r ( 51 new FileOutputStream ( importsqlfile ) , System 52 . getProperty ( " file . encoding " ) ) ; 53 54 int c = -1; 55 StringBuffer tempSB = new StringBuffer () ; 56 while (( c = reader . read () ) != -1) { 3
  • 4. 57 writer . write ( c ) ; 58 tempSB . append (( char ) c ) ; 59 } 60 sce . getServlet Contex t () . log ( tempSB . toString () ) ; 61 // 62 sce . getServlet Contex t () . log ( 63 " Generate import . sql ( " 64 + System . getProperty ( " file . encoding " ) 65 + " ) success ! " ) ; 66 } catch ( Exception e ) { 67 e . printStackTrace () ; 68 } finally { 69 try { 70 reader . close () ; 71 writer . flush () ; 72 writer . close () ; 73 } catch ( IOException e ) { 74 e . printStackTrace () ; 75 } 76 } 77 } 78 79 } (III) web.xml SessionFactory , ssh Spring ContextLoaderListener 1 <! -- hibernate ddl import.sql -- > 2 <! -- -- > 3 < context - param > 4 < param - name > ddl </ param - name > 5 < param - value > import . sql . UTF -8 </ param - value > 6 </ context - param > 7 < listener > 8 < listener - class > 9 cn . qdqn . bms . web . listener . HibListener 10 </ listener - class > 11 </ listener > 4
  • 5. 5