我在数据库中指定了以下列:decimal(5,2)
怎么解释这个?
根据SQL Server Management Studio中查看的列上的属性,我可以看到它表示:十进制(数字精度,数字刻度).
精确度和规模在实际意义上是什么意思?
很容易将其解释为带有5位数和2位小数位的十进制数...即12345.12
PS我已经能够从同事那里确定正确的答案,但很难在网上找到答案.因此,我想在stackoverflow上记录问题和答案,以备将来参考.
我正在尝试从Hibernate 3.6.5升级到4.0(从Spring 3.0.5升级到3.1,这是Hibernate 4支持所必需的).
现在,使用MySQL和HSQL,我遇到了持久布尔字段的问题:
Caused by: org.hibernate.HibernateException:
Wrong column type in PUBLIC.PUBLIC.EVENT for column Checked. Found: bit, expected: boolean
at org.hibernate.mapping.Table.validateColumns(Table.java:282)
at org.hibernate.cfg.Configuration.validateSchema(Configuration.java:1268)
at org.hibernate.tool.hbm2ddl.SchemaValidator.validate(SchemaValidator.java:155)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:453)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1737)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1775)
at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:184)
at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:314)
Run Code Online (Sandbox Code Playgroud)
JPA @Entity和@Column注释用于域对象,有问题的字段如下所示:
@Column(name = "Checked")
private boolean checked;
Run Code Online (Sandbox Code Playgroud)
HSQL架构:
Checked bit default 0 not null,
Run Code Online (Sandbox Code Playgroud)
MySQL架构:
`Checked` tinyint(1) NOT NULL default '0',
Run Code Online (Sandbox Code Playgroud)
在坚持使用Hibernate 4的同时解决这个问题最简单的方法是什么?我应该更改数据库架构,Hibernate配置还是域类注释?
我不知道代码和配置之前是否完全"正确",但至少它与Hibernate 3一起运行良好.
我正在使用HSQL在我的java应用程序上运行许多单元测试.我正在使用Spring + Hibernate.从MySQL切换到HSQL时遇到问题.测试在MySQL上完美运行,但每当我改为HSQL时,我都会遇到以下异常:
Caused by: org.hsqldb.HsqlException: invalid schema name: LMS
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.SchemaManager.getSchemaHsqlName(Unknown Source)
at org.hsqldb.SchemaManager.getSchemaName(Unknown Source)
at org.hsqldb.Session.getSchemaName(Unknown Source)
at org.hsqldb.SchemaManager.getTable(Unknown Source)
at org.hsqldb.ParserDQL.readTableName(Unknown Source)
at org.hsqldb.ParserDQL.readSimpleRangeVariable(Unknown Source)
at org.hsqldb.ParserDML.compileInsertStatement(Unknown Source)
at org.hsqldb.ParserCommand.compilePart(Unknown Source)
at org.hsqldb.ParserCommand.compileStatement(Unknown Source)
at org.hsqldb.Session.compileStatement(Unknown Source)
at org.hsqldb.StatementManager.compile(Unknown Source)
at org.hsqldb.Session.execute(Unknown Source)
Run Code Online (Sandbox Code Playgroud)
我的Spring配置如下:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.hsqldb.jdbc.JDBCDriver" />
<property name="url" value="jdbc:hsqldb:file:lms" />
<property name="username" value="SA"/>
<property name="password" value=""/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan"> …Run Code Online (Sandbox Code Playgroud) 我目前正在使用以下设置在嵌入式数据库中创建架构,然后再针对它运行测试
在我的应用程序上下文
<jdbc:embedded-database id="dataSource" type="HSQL">
<jdbc:script location="classpath:createSchema.sql" />
</jdbc:embedded-database>
Run Code Online (Sandbox Code Playgroud)
createSchema.sql
create schema ST_TEST AUTHORIZATION DBA;
Run Code Online (Sandbox Code Playgroud)
休眠属性
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
<property name="hibernate.default_schema" value="ST_TEST"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.use_sql_comments" value="true" />
<property name="hibernate.cache.use_second_level_cache" value="false" />
</properties>
Run Code Online (Sandbox Code Playgroud)
我的问题是,这是最好的方法.或者我可以在我的属性中使用不同的模式名称?或者在jdbc:embedded-database元素中设置模式名称
在我的java项目中,我想为我的应用程序使用HSQLDB数据库,所以我正在研究HSQLDB,我只是想知道是否可以从应用程序内部或从应用程序外部备份和恢复HSQLDB数据库?
我使用Hibernate + JPA作为我的ORM解决方案.
我使用HSQL进行单元测试,将PostgreSQL用作真正的数据库.
我希望能够将Postgres的本机UUID类型与Hibernate一起使用,并在其字符串表示中使用UUID和HSQL进行单元测试(因为HSQL没有UUID类型).
我正在使用具有Postgres和HSQL单元测试的不同配置的持久性XML.
这是我如何让Hibernate"看到"我的自定义UserType:
@Id
@Column(name="UUID", length=36)
@org.hibernate.annotations.Type(type="com.xxx.UUIDStringType")
public UUID getUUID() {
return uuid;
}
public void setUUID(UUID uuid) {
this.uuid = uuid;
}
Run Code Online (Sandbox Code Playgroud)
这很有效.但我需要的是能够在XML中替换注释的"com.xxx.UUIDStringType"部分,或者从可以在不重新编译的情况下更改的属性文件.
有任何想法吗?
我在我的后端使用Hibernate JPA.我正在使用JUnit和DBUnit编写单元测试,以将一组数据插入到内存中的HSQL数据库中.
我的数据集包含:
<order_line order_line_id="1" quantity="2" discount_price="0.3"/>
Run Code Online (Sandbox Code Playgroud)
它映射到OrderLine Java对象,其中discount_price列定义为:
@Column(name = "discount_price", precision = 12, scale = 2)
private BigDecimal discountPrice;
Run Code Online (Sandbox Code Playgroud)
但是,当我运行我的测试用例并声明返回的折扣价格等于0.3时,断言失败并表示存储的值为0.如果我将数据集中的discount_price更改为0.9,则它会向上舍入为1.
我已经检查过以确保HSQLDB没有进行舍入,这肯定不是因为我可以使用类似5.3的值的Java代码插入订单行对象并且它工作正常.
对我来说,似乎DBUtils由于某种原因舍入了我定义的数字.有没有办法可以迫使这种情况发生?任何人都可以解释为什么它可能这样做?
谢谢!
我有一个很常用的项目,我正在进行当前更新.有几个地方可以安装此项目,并且将来不确定将来可能更新到哪个版本的版本和版本.但是现在他们都是一样的.
我的问题源于这样一个事实,即hibernate实体类可能会有很多变化,并且必须很容易更新到更新的版本而不会有任何麻烦,并且不会丢失数据库内容.只需替换WAR并启动它就应该自行迁移.
据我所知,除非hibernate.hbm2ddl.auto = create,否则Hibernate不会更改表,但它实际上会抛弃所有数据?
所以现在当Spring上下文完全加载时,它会执行一个bean,它将数据库迁移到当前版本,方法是执行从versionX到versionY的所有更改(之前的版本保存在数据库中),并手动更改桌子.
使用一些硬编码的ALTER TABLE来添加一些列并不是很麻烦,但是当谈到添加完整的新表时,必须编写所有这些内容感觉很愚蠢......
所以我的问题是这样的:
有没有办法在某处向Hibernate代码发送实体类和方言,并获取有效的SQL查询来创建表?
甚至更好,以某种方式创建一个SQL字符串,用于向表中添加一个列,dialect-safe?
我希望这不是一个愚蠢的问题,在Hibernate方面我没有错过任何明显的东西......
测试在Eclipse中单独工作(右键单击,以Junit身份运行),但是当我使用maven从命令行运行它们时,它会在尝试获取与HSQL数据库的DataSource连接时停止.
LogicalConnectionImpl [DEBUG] Obtaining JDBC connection
Run Code Online (Sandbox Code Playgroud)
对我来说最奇怪的部分是,如果我将测试限制为10(不是类,而是单独的@Test方法),它将起作用.但是,一旦进行了第11次测试,就会爆炸.测试组合没关系,所以如果我有30个测试,并且我选择运行11个或更多,那么它将失败.这让我觉得它是一个DataSource连接最大问题,但到目前为止还没有运气细节.我已经尝试添加堆大小只是为了咯咯笑,但这不起作用.
因此,在此挂起之前,我有多个成功的JDBC连接和发布.
这是我的applicationContext-HSQL.xml,它用于Eclipse和Command Line版本.我知道b/c我通过弄乱下面的Class值强迫他们每个人失败.
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:flow="http://www.springframework.org/schema/webflow-config"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:osgi="http://www.springframework.org/schema/osgi"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/webflow-config http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.0.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.0.xsd
http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.company"/>
<import resource="classpath*:/application-context-cxf.xml"/>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="persistenceXmlLocation" value="META-INF/persistence.xml" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="false" />
<property name="generateDdl" value="false" />
<property name="databasePlatform" value="org.hibernate.dialect.HSQLDialect" />
</bean>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property …Run Code Online (Sandbox Code Playgroud) 我有hsqldb(在内存中)的集成测试,现在我需要在我的测试中设置保存点,在BaseTest类中,如何在测试中设置保存点(hsqldb(内存中))?
BaseTest:
@ContextConfiguration(classes = {TestConfig.class})
public class BaseTest {
@Before
public void savePoint() {
//set savepoint - How can do it this?
}
@After
public void rollBackToSavePoint() {
//roll back to savepoint - How can do it this?
}
}
Run Code Online (Sandbox Code Playgroud)
我的测试:
@RunWith(SpringJUnit4ClassRunner.class)
public class MyTest extends BaseTest {
@Test
public void test1() {
...
}
}
Run Code Online (Sandbox Code Playgroud)
TestConfig:
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
@Configuration
@EnableJpaRepositories("database.dao")
@ComponentScan(basePackageClasses = { MyServiceImpl.class})
@EntityScan({"database.model"})
@Import({DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
public class TestConfig {
...
}
Run Code Online (Sandbox Code Playgroud)