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