我@CreatedDate在实体属性上使用,我看到它在db中插入日期.我不明白@CreatedBySpring Data JPA 中注释的目的是什么.
在参考文档中,我读到:
我们提供
@CreatedBy,@LastModifiedBy捕捉谁创建或修改该实体的用户
但是如何创建和使用这样的用户呢?
我试图通过以下方式获得它:
private static ApplicationContext applicationContext;
@Autowired
public static void setApplicationContext(ApplicationContext applicationContext) {
AuditorTest.applicationContext = applicationContext;
}
Run Code Online (Sandbox Code Playgroud)
但这并不能像其他所有尝试一样起作用。
如何自动连接静电ApplicationContext?
我使用h2 db和hibernate 4.
我想从实体自动生成db模式并从import.sql文件填充db .这是相关的hibernate.cfg.xml:
<!-- automatically generate database tables from hibernate entities -->
<property name="hibernate.hbm2ddl.auto">create-drop</property>
<!-- initialize db on startup -->
<property name="hibernate.hbm2ddl.import_files">/import.sql</property>
Run Code Online (Sandbox Code Playgroud)
因此,在生成数据库表时会import.sql调用它.这是第一个sql语句:
insert into Borrower values (1,"Greece, Aphines, Square street",5,"Antonio");
Run Code Online (Sandbox Code Playgroud)
当hibernate运行这个sql语句时,它会给出一个错误:
???? 23, 2015 8:56:43 PM org.hibernate.tool.hbm2ddl.SchemaExport importScript
ERROR: HHH000388: Unsuccessful: insert into Borrower values (1,"Greece, Aphines, Square street",5,"Antonio")
???? 23, 2015 8:56:43 PM org.hibernate.tool.hbm2ddl.SchemaExport importScript
Column "Greece, Aphines, Square street" not found; SQL statement:
insert into Borrower values (1,"Greece, Aphines, …Run Code Online (Sandbox Code Playgroud)