休眠:DDL模式生成

不久前,我必须使用内存数据库。 该活动与集成测试有关。 如您所知,通常将内存数据库用于集成测试。 造成这种情况的原因有很多:可移植性,完善的环境基础结构,高性能,原始数据库的一致性。

问题在于如何将生产DDL模式整合到测试内存数据库中。 第一个是MySQL ,第二个是HSQLDB 。 MySQL的语法不同于HSQL语法。 因此,如果不进行适当的转换,就不可能将MySQL表模式导入HSQLDB。

坦白说,我已经花了很多时间在寻找一些解决方案,这将有助于我在HSQL中导入MySQL DDL模式 。 结果不是我想要的那么好。 所有解决方案都是商业性的或非自动化的,例如,替换HSQL上所有MySQL特定的代码。

幸运的是,我的项目使用Hibernate作为JPA实现。 所有实体都装饰有适当的Hibernate注释。 正如您将进一步看到的那样,这对于MySQL模式的转换非常有帮助。 Hibernate提供了基于DDL生成实体的机制,反之亦然。 因此,任何使用Hibernate注释修饰的实体都可以使用Hibernate支持的DB语言来表示为表模式。

这是一个解决我的问题的类:

public class SchemaTranslator {
	private Configuration config = null;

	public SchemaTranslator() {
		config = new Configuration();
	}

	public SchemaTranslator setDialect(String dialect) {
		config.setProperty(AvailableSettings.DIALECT, dialect);
		return this;
	}

	/**
	 * Method determines classes which will be used for DDL generation. 
	 * @param annotatedClasses - entities annotated with Hibernate annotations.
	 */
	public SchemaTranslator addAnnotatedClasses(Class[] annotatedClasses) {
		for (Class clazz : annotatedClasses)
			config.addAnnotatedClass(clazz);
		return this;
	}

	/**
	 * Method performs translation of entities in table schemas.
	 * It generates 'CREATE' and 'DELETE' scripts for the Hibernate entities.
	 * Current implementation involves usage of {@link #write(FileOutputStream, String[], Formatter)} method.
	 * @param outputStream - stream will be used for *.sql file creation.
	 * @throws IOException
	 */
	public SchemaTranslator translate(FileOutputStream outputStream) throws IOException {
		Dialect requiredDialect = Dialect.getDialect(config.getProperties());
		String[] query = null;

		query = config.generateDropSchemaScript(requiredDialect);
		write(outputStream, query, FormatStyle.DDL.getFormatter());

		query = config.generateSchemaCreationScript(requiredDialect);
		write(outputStream, query, FormatStyle.DDL.getFormatter());

		return this;
	}

	/**
	 * Method writes line by line DDL scripts in the output stream.
	 * Also each line logs in the console.
	 * @throws IOException
	 */
	private void write(FileOutputStream outputStream, String[] lines, Formatter formatter) 
			throws IOException {
		String tempStr = null;

		for (String line : lines) {
			tempStr = formatter.format(line)+";";
			System.out.println(tempStr);
			outputStream.write(tempStr.getBytes());
		}
	}

	public static void main(String[] args) throws IOException {
		SchemaTranslator translator = new SchemaTranslator();
		Class[] entityClasses = {Smartphone.class};

		translator.setDialect("org.hibernate.dialect.HSQLDialect")
			.addAnnotatedClasses(entityClasses)
			.translate(new FileOutputStream(new File("db-schema.sql")));

	}

}

上面的代码非常冗长,但我对此进行了评论。 因此,我希望它或多或少容易理解。 整个应用程序的代码可以在GitHub找到SchemaTranslator类在项目结构中具有以下位置:

  • / src / test / java / com / mobapp / test / util /

借助此类,您可以将您的实体采用到Hibernate支持的任何必需数据库中。

祝好运!

参考: Hibernate:我们的JCG合作伙伴 Alexey Zvolinskiy在Fruzenshtein的注释博客中生成DDL Schema

翻译自: https://www.javacodegeeks.com/2014/01/hibernate-ddl-schema-generation.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值