- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 MS SQL Server 中有一个从 Java 代码调用的用户定义函数,在 H2 数据库中运行集成测试时该函数似乎未定义。你可以在 the previous question 找到我的代码.
测试代码:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {H2Config.class})
@TestExecutionListeners({
DependencyInjectionTestExecutionListener.class,
DbUnitTestExecutionListener.class,
TransactionalTestExecutionListener.class
})
@TransactionConfiguration(defaultRollback = true)
public class TableDaoTest {
@Autowired
private TableDao tableDao;
@Test
@DatabaseSetup("/datasets/import.xml")
public void testMethod01() {
tableDao.getRecordsByGroup();
...
数据库模式由 Hibernate 自动生成。正如您所看到的,测试数据是由 DbUnit 使用 xml 数据集填充的。这个测试失败了,因为我的 MS SQL Server DB 中存在的函数在 H2 数据库中未定义。
应用程序日志:
Caused by: org.hibernate.exception.GenericJDBCException: could not prepare statement
...
Caused by: org.h2.jdbc.JdbcSQLException: Function "SAFE_MOD" not found; SQL statement:
select table10_.id, table10_.value, ... from Table1 table10_ where table10_.group1=dbo.safe_mod(?, ?);
...
如何在 DbUnit 测试之前导入/创建函数?
最佳答案
H2数据库不支持用户定义的SQL函数。但是,在此数据库中,Java 函数也可以用作存储过程。
@SuppressWarnings("unused")
public class H2Function {
public static int safeMod(Integer n, Integer divider) {
if (divider == null) {
divider = 5000;
}
return n % divider;
}
}
注意,仅支持静态 Java 方法;类和方法都必须是公共(public)的。
Java 函数必须通过调用 CREATE ALIAS ... FOR
进行声明(在数据库中注册),然后才能使用:
CREATE ALIAS IF NOT EXISTS safe_mod DETERMINISTIC FOR "by.naxa.H2Function.safeMod";
该语句应该在任何测试之前执行,因此我决定将其放在连接初始化 SQL 中:
@Bean
public DataSource dataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName("org.h2.Driver");
dataSource.setUrl("jdbc:h2:mem:my_db_name");
dataSource.setUsername("sa");
dataSource.setPassword("");
dataSource.setConnectionInitSqls(Collections.singleton(
"CREATE ALIAS IF NOT EXISTS safe_mod DETERMINISTIC FOR \"by.naxa.H2Function.safeMod\";"));
return dataSource;
}
关于java - DbUnit-JdbcSQLException : Function "*" not found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46408493/
我是 grails 的新手,我创建了一个用户域类和用户配置文件域类。而这些类(class)是有亲戚关系的。域类如下所示 class User { transient springSecurit
每当我尝试执行包含超过 40 个测试的测试类时,多余的测试都会失败。测试类由43个测试组成。无论我删除哪三个测试,其余的测试都会运行绿色。 使用的框架: 朱尼特 Mockmvc h2 数据库 堆栈跟踪
我在 MS SQL Server 中有一个从 Java 代码调用的用户定义函数,在 H2 数据库中运行集成测试时该函数似乎未定义。你可以在 the previous question 找到我的代码.
我用另一个应该连接到内存数据库的配置替换了一个连接到真实数据库的配置。我想使用此配置进行集成测试。一个简单的测试如下所示 import org.junit.Test; import org.junit
我想设置一个计数器来从数据库中获取记录数: String sql = " select count(*) as counter from CLIENT "; conn = DBConnector.ge
我正在尝试使用嵌入式 h2 数据库运行我的应用程序。 我收到以下错误: Caused by: org.springframework.jdbc.BadSqlGrammarException: Prep
我是 Grails 的新手,不太熟悉 GORM 如何将列映射到 jdbc 实现。 基本上我有以下错误: org.h2.jdbc.JdbcSQLException 信息: Value too long
我正在尝试编写一个内存数据库来测试我的 DAO 对象。我得到以下堆栈跟踪: Caused by: org.h2.jdbc.JdbcSQLException: Syntax error in SQL s
我已经设置了以下 POJO: @Getter @Setter @AllArgsConstructor @Entity @Table(name = "SpreadsheetRequest") publi
这是 Hibernate version mismatch in WildFly 10.0 的后续问题。 为了完整起见,简要重述问题。我有2个项目,一个是普通的java项目(带有maven):core
我参与了一个 Spring Boot 项目,该项目读取 CSV 文件并将数据保存在 H2 数据库中。 下面提供了 CSV 文件 - authors.csv email;firstname;lastna
在java中使用h2(调用getBytes函数)时,我收到以下错误。谷歌没有找到我任何地方。我想知道是否有人知道发生了什么事。 Caused by: org.h2.jdbc.JdbcSQLExcept
所以简短的版本,我猜我有某种字符编码问题,或者数据库正在以 Hibernate/Spring-jpa 出于某种原因不喜欢的格式存储/返回日期。 但如果我能弄清楚哪里出了问题,我会很兴奋! 使用 Hib
几天以来,我一直在努力解决这个问题,但并未真正了解其背后的原因。 使用 Liquibase、PostgreSQL 和 H2DB(用于测试)。 Liquibase 在使用 PostgreSQL 时工作正
我已经尝试过在网上找到的一些解决方案,但都没有奏效。我正在使用内存中持久 hibernate 数据库。 @Entity @Table(name = "Users") public class User
尝试使用以下 URL 创建 H2 数据库连接时, Connection conn = DriverManager.getConnection("jdbc:h2:D:/HATBOX/Data;AUTO
我在内存数据库中使用 H2 和 play 框架 我是初学者所以我决定制作一个小的登录注册应用程序来开始使用 scala 并玩我决定在内存数据库中使用 H2 但它抛出 Table未找到异常我在 sql
我在 openshift 上部署应用程序时遇到下一个问题。我使用 wildfly 应用程序服务器和 PostgreSQL cartrige。 在 persistence.xml 中,我设置了属性“hi
我正在尝试以开发模式(使用 H2)运行当前在生产环境中使用 PostgreSQL 数据库的设置,但出现错误。如果我可以在不进行任何更改的情况下重用生产 SQL,那将是最好的。 使用此设置: # H2
Spring Boot 1.1.5.RELEASE public class DataAccessTests { private EmbeddedDatabase db; @Befor
我是一名优秀的程序员,十分优秀!