简体   繁体   English

使用hibernate创建夹具(不是import.sql方法)

[英]create fixture using hibernate(not import.sql method)

Is there a way to use java code and those hibernate entity classes to create fixture data. 有没有一种方法可以使用Java代码和那些休眠实体类来创建夹具数据。

I find using import.sql is not good when it comes to database migration 我发现在数据库迁移方面使用import.sql不好

Since you are using hibernate, the way to create sample data is to use hibernate itself to create it. 由于您正在使用休眠方式,因此创建示例数据的方法是使用休眠本身来创建它。 You have two options: 您有两种选择:

  • generate random data - look from 1 to X, instantiate entities and set random values, then save each entity 生成随机数据-从1到X进行查找,实例化实体并设置随机值,然后保存每个实体
  • predefined data - use some meta format, like .properties, .json or .xml to define field=value pairs, parse that file, instantiate entities for each record, set the fields, and save it. 预定义的数据-使用某种元格式,例如.properties,.json或.xml来定义field = value对,解析该文件,为每个记录实例化实体,设置字段并保存。

That way your data generation is tied to your entity model, and when the model changes, your data won't become out-of-sync. 这样,您的数据生成将与实体模型联系在一起,并且当模型更改时,您的数据将不会变得不同步。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM