使用HSQLDB来作EJB3 EntityBean到Unit Test要点

阅读更多
  • add to hibernate property this will re-create table when SessionFactory init, drop table when session factory close
  • add HSQLDB dependency into pom.xml
 

          hsqldb
          hsqldb
          1.8.0.7
          test
        
  •  prepare meta data, and insert the meta data into MemDB, now I use @Before annotation from Junit, so I need to test if we need to insert the data indead, because the method annotated by  @Before will be executed each time for test method

 

@Before
    public void prepareData() throws ServiceException {
        if($needPrePareData){
            executeSqlFile("init_data.sql");
        }
    }
private void executeSqlFile(String fileName){
        try{
            File refFile = new File(ClassLoader.getSystemResource(fileName).toURI());
            SqlFile sqlFile = new SqlFile(refFile,false,null);
            sqlFile.execute(dataSource.getConnection(), true);
        }catch(Exception e){
            System.out.println("exception when prepare Data ");
            e.printStackTrace();
        }
    }
  •  use HSQLDB releated property
db.dialect=org.hibernate.dialect.HSQLDialect
db.driver=org.hsqldb.jdbcDriver
db.url=jdbc:hsqldb:mem:DBName
db.username=sa
db.passwd=
  • we can also use hibernate3-maven-plugin to generate ddl from JPA annotation
    •  http://mojo.codehaus.org/maven-hibernate3/hibernate3-maven-plugin/
    •  ref article here http://unmaintainable.wordpress.com/2008/04/12/hibernate3-schema-creation/
    • pay more attention on the jpaconfiguration, it indicate that the plugin will use jpa as default implemtation

  
    
    
      org.codehaus.mojo
      hibernate3-maven-plugin
      2.2
      
        
          process-classes
          
            hbm2ddl
          
        
      
      
        
          
            hbm2ddl
            jpaconfiguration
          
        
        
          Default
          schema.ddl
          false
          true
          false
          true
        
      
    
  

 

 

 

 

 

你可能感兴趣的:(HSQLDB,maven,Hibernate,JPA,WordPress)