在Hibernate 3.3.1ga和HSQLDB中使用@Table与模式名称 [英] Using @Table with schema name in Hibernate 3.3.1ga and HSQLDB

查看:96
本文介绍了在Hibernate 3.3.1ga和HSQLDB中使用@Table与模式名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  @Entity 
如何在使用Hibernate 3.3.1ga和HSQLDB的单元测试中完成这项工作@Table(name =CATEGORY,schema =TEST)
public static class Category {...}

问题在于Hibernate希望模式存在。第二个问题是,在我的任何代码运行之前(这发生在Spring的测试设置内部),Hibernate发出了 CREATE TABLE TEST.CATEGORY ,所以我无法获得连接到Hibernate之前的数据库并手动创建模式。

但我需要这个模式,因为我必须访问真实代码中的不同数据库。我应该怎么做?



Hibernate 3.3.1ga,HSQLDB,Spring 2.5

解决方案

您可以编写一个实现 InitializingBean

 公共类SchemaCreator实现InitializingBean {

private String模式;
私有DataSource数据源;

public String getSchema(){
return schema;
}

public void setSchema(String schema){
this.schema = schema;
}

public DataSource getDataSource(){
return dataSource;
}

public void setDataSource(DataSource dataSource){
this.dataSource = dataSource;

$ b @Override
public void afterPropertiesSet()throws Exception {
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.execute(CREATE SCHEMA+ schema +AUTHORIZATION DBA);
}

}

然后你必须定义一个bean在这个类的bean定义文件中(我在黑暗中拍摄了你现有的bean定义的样子)。

 < bean id =dataSourceclass =...> 
< property name =driverClassNamevalue =org.hsqldb.jdbcDriver/>
< property name =urlvalue =jdbc:hsqldb:mem:test/>
< property name =usernamevalue =sa/>
< property name =passwordvalue =/>
< / bean>

< bean id =sessionFactorydepends-on =schemaCreatorclass =org.springframework.orm.hibernate3.LocalSessionFactoryBean>
< property name =dataSourceref =dataSource/>
...
< / bean>

< bean id =schemaCreatorclass =SchemaCreator>
< property name =dataSourceref =dataSource/>
< property name =schemavalue =TEST/>
< / bean>

通过使用 depends-on Hibernate bean的属性,Spring将会确保 schemaCreator bean将被首先初始化,导致模式恰好存在。这也应该让你的意图更清楚。


How can I make this work in unit tests using Hibernate 3.3.1ga and HSQLDB:

@Entity
@Table(name="CATEGORY", schema="TEST")
public static class Category { ... }

The problem is that Hibernate expects the schema to exist. The second problem is that Hibernate issues the CREATE TABLE TEST.CATEGORY before any of my code runs (this happens deep inside Spring's test setup), so I can't get a connection to the DB before Hibernate and create the schema manually.

But I need the schema because I have to access different databases in the real code. What should I do?

Hibernate 3.3.1ga, HSQLDB, Spring 2.5

解决方案

You could write a class that implements InitializingBean:

public class SchemaCreator implements InitializingBean {

    private String schema;
    private DataSource dataSource;

    public String getSchema() {
        return schema;
    }

    public void setSchema(String schema) {
        this.schema = schema;
    }

    public DataSource getDataSource() {
        return dataSource;
    }

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        jdbcTemplate.execute("CREATE SCHEMA " + schema + " AUTHORIZATION DBA");
    }

}

You then have to define a bean in your bean definition file of this class (I'm taking a shot in the dark as to what your existing bean definitions look like).

<bean id="dataSource" class="...">
    <property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
    <property name="url" value="jdbc:hsqldb:mem:test"/>
    <property name="username" value="sa"/>
    <property name="password" value=""/>
</bean>

<bean id="sessionFactory" depends-on="schemaCreator" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    ...
</bean>

<bean id="schemaCreator" class="SchemaCreator">
    <property name="dataSource" ref="dataSource"/>
    <property name="schema" value="TEST"/>
</bean>

By using the depends-on attribute of Hibernate's bean, Spring will ensure that the schemaCreator bean will be initialized first, causing the schema to exist just in time. This should also make your intentions clearer.

这篇关于在Hibernate 3.3.1ga和HSQLDB中使用@Table与模式名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆