[英]using hibernate 5 how to create a table with a ROW at the time of first execution?
i want to insert a row in a table as with table creation time but Hibernate 5 is create table successfully but not insert my given default row like below example, 我想在表创建时插入一行,但Hibernate 5是成功创建表但不插入我给定的默认行,如下例所示,
i already tried below code, 我已经尝试过以下代码,
@Entity
public class FTPDomain {
@Id
private Long id=(long)1;
private String transferCroneTime="0 0 0 29 2 ?";
private String cleanCroneTime="0 0 0 29 2 ?";
//Setters & Getters
}
and i also tried , 我也尝试过,
@PrePersist
public void prePersist() {
if(id == null) id = (long)1;
if(transferCroneTime == null) transferCroneTime ="0 0 0 29 2 ?";
if(cleanCroneTime == null) cleanCroneTime = "0 0 0 29 2 ?"
}
i expect when FTPDomain.java compile and execute first time create table with a ROW where id=1, transferCroneTime=0 0 0 29 2 ?
我希望当FTPDomain.java编译并执行第一次创建表时,其中id为1,transferCroneTime = 0 0 0 29 2? and cleanCroneTime=0 0 0 29 2 ?
和cleanCroneTime = 0 0 0 29 2? in the table.
在表中。
Thanks... 谢谢...
@PrePersist and @PreUpdate are JPA event listener, which are meant to modify entity states before the entity gets persisted or updated, as already mentioned in the comments. @PrePersist和@PreUpdate是JPA事件监听器,用于在实体被持久化或更新之前修改实体状态,如评论中已经提到的那样。 See the following examples for a proper usage of these event listeners.
有关这些事件侦听器的正确用法,请参阅以下示例。
@PrePersist
public void prePersist() {
creationDate = Localdatetime.now();
updatedDate = Localdatetime.now();
}
@PreUpdate
public void preUpdate() {
updatedDate = Localdatetime.now();
}
If you want to pre-populate data to a freshly created schema, then you need to use the mechanisms described in this article https://thoughts-on-java.org/standardized-schema-generation-data-loading-jpa-2-1/ , whereby you configure it in your persistence.xml via the parameter javax.persistence.sql-load-script-source , which points to a sql script which loads the data via insert statements into your database. 如果要将数据预填充到新创建的模式,则需要使用本文中描述的机制https://thoughts-on-java.org/standardized-schema-generation-data-loading-jpa-2 -1 / ,您可以通过参数javax.persistence.sql-load-script-source在persistence.xml中对其进行配置,该参数指向一个sql脚本,该脚本通过insert语句将数据加载到您的数据库中。
There are also hibernate specific properties which provide the same functionality as describe in the docs https://docs.jboss.org/hibernate/orm/5.1/userguide/html_single/Hibernate_User_Guide.html#_importing_script_files , but I would recommend to use the JPA specific ones. 还有一些特定于hibernate的属性提供与文档https://docs.jboss.org/hibernate/orm/5.1/userguide/html_single/Hibernate_User_Guide.html#_importing_script_files中描述的相同的功能,但我建议使用JPA特定的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.