ste*_*wpf 6 spring hibernate hsqldb
我正在将Hibernate与Spring结合使用.作为数据库,我目前正在使用HSQL,它将数据存储在一个文件中(如SQLite).HSQL文件的路径目前在persistence.xml中进行了硬编码.如何在运行时访问和更改此值,以便用户可以从/向任意HSQL文件加载和保存?
persistence.xml中:
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="something-unit">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver" />
<property name="hibernate.connection.url" value="jdbc:hsqldb:file:~/something-db/somethingdb" />
<property name="hibernate.connection.username" value="sa" />
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
</persistence>
Run Code Online (Sandbox Code Playgroud)
Spring applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:data="http://www.springframework.org/schema/data/jpa"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!-- Database Setup -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="something-unit" />
</bean>
<data:repositories base-package="com.something.playlist"/>
<!-- Transaction Setup -->
<tx:annotation-driven/>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
</beans>
Run Code Online (Sandbox Code Playgroud)
谢谢你的提示!
您可以指定 JNDI 数据源并将其传递给 Hibernate。或者您可以通过实现接口 org.hibernate.connection.ConnectionProvider 来定义自己的插件策略来获取 JDBC 连接
有关更多提示,请参阅:http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/session-configuration.html
编辑2/16: StackOverflow上有一个关于创建自定义ConnectionProvider的示例:How can I set Datasource when I'm create Hibernate SessionFactory?
如果您要动态更改数据源,而不是在启动时更改,则必须重新启动 Hibernate 会话工厂。要正确执行此操作,您必须确保重新启动时其中没有任何事务正在运行。以下问题/答案将帮助您:Hibernate Sessionfactory restart | Hibernate Sessionfactory restart | Hibernate Sessionfactory restart | Hibernate Sessionfactory restart | Hibernate Sessionfactory restart 春天
| 归档时间: |
|
| 查看次数: |
3175 次 |
| 最近记录: |