Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
HSQLDB使用JavaConfig处理Spring应用程序的新功能_Java_Spring_Hsqldb - Fatal编程技术网

HSQLDB使用JavaConfig处理Spring应用程序的新功能

HSQLDB使用JavaConfig处理Spring应用程序的新功能,java,spring,hsqldb,Java,Spring,Hsqldb,我是HSQLDB新手,使用JavaConfig处理Spring应用程序。我希望我的示例设置一个内存数据库(HSQLDB)并插入一行 我想我的东西都整理好了,但我不知道在哪里创建数据库和表 下面是我的main.app代码 public class MainApp { private static final Logger LOGGER = getLogger(MainApp.class); @Autowired protected MessageService mSer

我是HSQLDB新手,使用JavaConfig处理Spring应用程序。我希望我的示例设置一个内存数据库(HSQLDB)并插入一行

我想我的东西都整理好了,但我不知道在哪里创建数据库和表

下面是我的main.app代码

public class MainApp
{

    private static final Logger LOGGER = getLogger(MainApp.class);

    @Autowired
    protected MessageService mService;

    public static void main(String[] args)
    {
        ApplicationContext context = new AnnotationConfigApplicationContext(HelloWorldConfig.class);
        HelloWorld helloWorld = context.getBean(HelloWorld.class);

        LOGGER.debug("Message from HelloWorld Bean: " + helloWorld.getMessage());

        /**
         *  I removed the following line... we are now using log4j
         */
        //System.out.println(helloWorld.getMessage());

        helloWorld.setMessage("I am in Staten Island, New York");

        /**
         *  I removed the following line... we are now using log4j
         */
        //System.out.println(helloWorld.getMessage());
        LOGGER.debug("Message from HelloWorld Bean: " + helloWorld.getMessage());
    }
这是我的DatabaseConfig.class

public class DatabaseConfig
{

    private static final Logger LOGGER = getLogger(DatabaseConfig.class);


    @Autowired
    Environment env;

    @Bean
    public DataSource dataSource() {
        EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
        return builder.setType(EmbeddedDatabaseType.HSQL).build();
    }

    @Bean
    public SessionFactory sessionFactory()
    {

        LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean();
        factoryBean.setDataSource(dataSource());
        factoryBean.setHibernateProperties(getHibernateProperties());
        factoryBean.setPackagesToScan(new String[]{"com.xxxx.model"});

        try
        {
            factoryBean.afterPropertiesSet();
        } catch (IOException e)
        {
            LOGGER.error(e.getMessage());
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }


        return factoryBean.getObject();
    }

    @Bean
    public Properties getHibernateProperties()
    {
        Properties hibernateProperties = new Properties();

        hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
        hibernateProperties.setProperty("hibernate.show_sql", env.getProperty("hibernate.show_sql"));
        hibernateProperties.setProperty("hibernate.use_sql_comments", env.getProperty("hibernate.use_sql_comments"));
        hibernateProperties.setProperty("hibernate.format_sql", env.getProperty("hibernate.format_sql"));
        hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));

        hibernateProperties.setProperty("hibernate.generate_statistics", env.getProperty("hibernate.generate_statistics"));

        hibernateProperties.setProperty("javax.persistence.validation.mode", env.getProperty("javax.persistence.validation.mode"));

        //Audit History flags
        hibernateProperties.setProperty("org.hibernate.envers.store_data_at_delete", env.getProperty("org.hibernate.envers.store_data_at_delete"));
        hibernateProperties.setProperty("org.hibernate.envers.global_with_modified_flag", env.getProperty("org.hibernate.envers.global_with_modified_flag"));

        return hibernateProperties;
    }

    @Bean
    public HibernateTransactionManager hibernateTransactionManager()
    {
        HibernateTransactionManager htm = new HibernateTransactionManager();
        htm.setSessionFactory(sessionFactory());
        htm.afterPropertiesSet();
        return htm;
    }
但我不知道数据库是在哪里创建的,在项目运行之前如何插入表

您可以从以下位置下载源代码:

以前有人问过这个问题,请看一下

如何使用JavaConfig实现这一点