8
現在我創造EntityManagerFactory
這樣創建EntityManagerFactory的編程方式(不persistence.xml文件):有註解的類
Map<String, String> properties = ImmutableMap.<String, String>builder()
.put(DRIVER, "com.mysql.jdbc.Driver")
.put(DIALECT, "org.hibernate.dialect.MySQL5Dialect");
.put(USER, dbUsername)
.put(PASS, dbPassword)
.put(URL, dbConnectionUrl)
//Some more properties
.build();
Ejb3Configuration cfg = new Ejb3Configuration();
cfg.configure(properties);
cfg.addAnnotatedClass(AuditEntry.class);
cfg.addAnnotatedClass(LastWrittenEventId.class);
//Some more annotated classes
return cfg.createEntityManagerFactory();
不過,我可以看到的Javadoc,Ejb3Configuration
被棄用,我不應該使用它。根據JPA spec第7.3節,我應該使用Persistence.createEntityManagerFactory()
。但是我只能傳遞一些屬性,但是我可以添加註釋類嗎?
我認爲這篇文章可以幫助你http://stackoverflow.com/questions/8448950/adding-entity-classes-dynamically-at-runtime –