带有spring和hibernate的hsqldb启动时数据文件中的数据在哪里
Posted
技术标签:
【中文标题】带有spring和hibernate的hsqldb启动时数据文件中的数据在哪里【英文标题】:hsqldb with spring and hibernate where is the data from data file on start up 【发布时间】:2014-02-24 21:37:38 【问题描述】:有很多这样的例子,这有点像这些例子的混合体。所以这是一个使用类路径应用程序上下文的控制台应用程序。这是少量数据,但我想知道,测试运行时数据文件中的记录在哪里。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="productDao" class="com.practice.HibernateProductDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="customerDao" class="com.practice.HibernateCustomerDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- HSQLDB Data Source -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
<property name="url" value="jdbc:hsqldb:file:naturalfoods" />
<property name="username" value="sa" />
<property name="password" value="" />
<property name="initialSize" value="5" />
<property name="maxActive" value="10" />
<property name="poolPreparedStatements" value="true" />
<property name="maxOpenPreparedStatements" value="10" />
</bean>
<!-- Hibernate Session Factory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.practice.Category</value>
<value>com.practice.Product</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.connection.autocommit">true</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>
一些java代码
package com.practice;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="PRODUCT")
public class Product
public Product(int i, String string, int j)
this.id=(long) i;
this.productName=string;
this.productInventory=i;
public Product()
super();
@Id
@Column(name="PRODUCT_ID")
private Long id;
@Column(name="PRODUCT_NAME")
private String productName;
@Column(name="PRODUCT_PRICE")
private Double productPrice;
@Column(name="PRODUCT_DESCRIPTION")
private String productDescription;
@Column(name="PRODUCT_INVENTORY")
private Integer productInventory;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml" )
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
@Transactional
public class TestDBConnection
private static ApplicationContext context;
private HibernateProductDaoImpl productDao;
@BeforeClass
public static void setUpClass()
context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
@Before
public void setUp() throws Exception
productDao = (HibernateProductDaoImpl)context.getBean("productDao");
Session session = productDao.getSessionFactory().getCurrentSession();
ClassMetadata data = productDao.getSessionFactory().getClassMetadata(Product.class);
for(String string:data.getPropertyNames())
System.out.println("name " + string);
Object object = session.get("com.practice.Product", 1L);
Product product = new Product(10,"bahhh",1);
session.save(product);
session.flush();
session.clear();
Query query = session.createQuery("From java.lang.Object");
System.out.print(query.list().get(0));
@Test
public void test()
fail("Not yet implemented");
这里是 naturalfoods.script
CREATE SCHEMA PUBLIC AUTHORIZATION DBA
CREATE MEMORY TABLE CATEGORY(CATEGORY_ID BIGINT GENERATED BY DEFAULT AS IDENTITY(START WITH 1) NOT NULL
PRIMARY KEY,CATEGORY_NAME VARCHAR(255))
CREATE MEMORY TABLE PRODUCT(PRODUCT_ID BIGINT GENERATED BY DEFAULT AS IDENTITY(START WITH 1) NOT NULL
PRIMARY KEY,PRODUCT_NAME VARCHAR(255),PRODUCT_PRICE DOUBLE,PRODUCT_DESCRIPTION VARCHAR(255),
PRODUCT_INVENTORY INTEGER)
ALTER TABLE CATEGORY ALTER COLUMN CATEGORY_ID RESTART WITH 6
ALTER TABLE PRODUCT ALTER COLUMN PRODUCT_ID RESTART WITH 7
CREATE USER SA PASSWORD ""
GRANT DBA TO SA
SET WRITE_DELAY 10
SET SCHEMA PUBLIC
然后是 naturalfoods.data
INSERT INTO PRODUCT VALUES(1,'Apple',0.25E0,'Food',10)
INSERT INTO PRODUCT VALUES(2,'Orange',0.5E0,'Food',10)
INSERT INTO PRODUCT VALUES(3,'Banana',0.75E0,'Food',0)
INSERT INTO PRODUCT VALUES(4,'Peas',1.5E0,'Food',10)
INSERT INTO PRODUCT VALUES(5,'Carrots',1.0E0,'Food',10)
INSERT INTO CATEGORY VALUES(1,'Fruit')
INSERT INTO CATEGORY VALUES(2,'Vegetables')
INSERT INTO CATEGORY VALUES(3,'Meat')
INSERT INTO CATEGORY VALUES(4,'Dairy')
这是一些 log4j 输出:
name productDescription
name productInventory
name productName
name productPrice
[2014-02-24 15:34:22,512] [DEBUG] [org.hibernate.SQL] [select product0_.PRODUCT_ID as PRODUCT1_1_0_, product0_.PRODUCT_DESCRIPTION as PRODUCT2_1_0_, product0_.PRODUCT_INVENTORY as PRODUCT3_1_0_, product0_.PRODUCT_NAME as PRODUCT4_1_0_, product0_.PRODUCT_PRICE as PRODUCT5_1_0_ from PRODUCT product0_ where product0_.PRODUCT_ID=?]
Hibernate: select product0_.PRODUCT_ID as PRODUCT1_1_0_, product0_.PRODUCT_DESCRIPTION as PRODUCT2_1_0_, product0_.PRODUCT_INVENTORY as PRODUCT3_1_0_, product0_.PRODUCT_NAME as PRODUCT4_1_0_, product0_.PRODUCT_PRICE as PRODUCT5_1_0_ from PRODUCT product0_ where product0_.PRODUCT_ID=?
[2014-02-24 15:34:22,552] [DEBUG] [org.hibernate.SQL] [insert into PRODUCT (PRODUCT_DESCRIPTION, PRODUCT_INVENTORY, PRODUCT_NAME, PRODUCT_PRICE, PRODUCT_ID) values (?, ?, ?, ?, ?)]
Hibernate: insert into PRODUCT (PRODUCT_DESCRIPTION, PRODUCT_INVENTORY, PRODUCT_NAME, PRODUCT_PRICE, PRODUCT_ID) values (?, ?, ?, ?, ?)
[2014-02-24 15:34:23,925] [DEBUG] [org.hibernate.SQL] [select product0_.PRODUCT_ID as PRODUCT1_1_, product0_.PRODUCT_DESCRIPTION as PRODUCT2_1_, product0_.PRODUCT_INVENTORY as PRODUCT3_1_, product0_.PRODUCT_NAME as PRODUCT4_1_, product0_.PRODUCT_PRICE as PRODUCT5_1_ from PRODUCT product0_]
Hibernate: select product0_.PRODUCT_ID as PRODUCT1_1_, product0_.PRODUCT_DESCRIPTION as PRODUCT2_1_, product0_.PRODUCT_INVENTORY as PRODUCT3_1_, product0_.PRODUCT_NAME as PRODUCT4_1_, product0_.PRODUCT_PRICE as PRODUCT5_1_ from PRODUCT product0_
[2014-02-24 15:34:24,005] [DEBUG] [org.hibernate.SQL] [select category0_.CATEGORY_ID as CATEGORY1_0_, category0_.CATEGORY_NAME as CATEGORY2_0_ from CATEGORY category0_]
Hibernate: select category0_.CATEGORY_ID as CATEGORY1_0_, category0_.CATEGORY_NAME as CATEGORY2_0_ from CATEGORY category0_
com.practice.Product@1784427
这不是确切的数据和脚本,但很接近。我只是想知道为什么只有 1 个产品?
【问题讨论】:
naturalfoods.data 和 naturalfoods.script 是如何运行的? 所以我改变了这个 a 将 *.script 和 *.data 组合成 *.script。 hsqldb 启动的关键是我希望只有一种产品。你只有一个测试。该测试运行一次@Before
方法,该方法创建并保存一个且只有一个产品。
如果您在多次测试运行后发现只有一种产品,那么您可能希望将您的数据库脚本更改为不创建memory
表。
【讨论】:
我尝试在插入后添加提交。没有效果。我阅读了设置创建文本表而不是创建内存表,但我的理解是使用 csv 文件。我认为 url 选项的“mem”与“file”意味着“file”将通读初始化脚本并执行 crud 操作。 所以我相信我误读了原始设置并错过了'mem'保证表格为空的细则。如果您希望数据在多个会话后出现在表中,请使用“文件”。以上是关于带有spring和hibernate的hsqldb启动时数据文件中的数据在哪里的主要内容,如果未能解决你的问题,请参考以下文章
如何在当前使用 HSQLDB 的 Spring 和 Hibernate 后台使用 MySQL
使用 HSQLDB 作为 Spring、Hibernate 的可移植数据库
org.hibernate.LazyInitializationException:无法初始化代理 - 没有会话。 Spring + Hibernate + HSQLDB
无法使用 JPA 2.0、Spring 2.5.6、Hibernate 3.6.1 和 Maven 在 HSQLDB 中持久化
JavaConfig - Spring JPA Hibernate - @EnableTransactionManagement