以编程方式向OpenJPA注册实体类型
看起來很簡單,如果有點(diǎn)難看。 OpenJPA允許以編程方式定義某些關(guān)鍵組件的實(shí)現(xiàn); 這些在屬性映射中指定,然后傳遞給javax.persistence.Persistence.createEntityManager(null,props) 。 但它也支持可用于通過setter注入初始化那些組件的語法。
在我的情況下,感興趣的組件是openjpa.MetaDataFactory 。 有一次我以為自己會(huì)寫我自己的實(shí)現(xiàn)。 但事實(shí)證明,標(biāo)準(zhǔn)實(shí)現(xiàn)滿足了我的需要,因?yàn)樗试S通過其setTypes(List <String>)增幅器注入類型。 字符串列表以;分隔列表的形式傳遞到該屬性中。
所以,這就是我最終得到的結(jié)果:
final Map<String, String> props = Maps.newHashMap();final String typeList = entityTypeList(); props.put("openjpa.MetaDataFactory","org.apache.openjpa.persistence.jdbc.PersistenceMappingFactory(types=" + typeList + ")");// ... then add in regular properties such as // openjpa.ConnectionURL, openjpa.ConnectionDriverName etc...entityManagerFactory = Persistence.createEntityManagerFactory(null, props);在我的情況下, EntityTypeList()看起來像這樣:
private String entityTypeList() {final StringBuilder buf = new StringBuilder();// loop thru Isis' metamodel looking for types that have been annotated using @Entityfinal Collection<ObjectSpecification> allSpecifications = getSpecificationLoader().allSpecifications();for(ObjectSpecification objSpec: allSpecifications) {if(objSpec.containsFacet(JpaEntityFacet.class)) {final String fqcn = objSpec.getFullIdentifier();buf.append(fqcn).append(";");}}final String typeList = buf.toString();return typeList; }歡迎評(píng)論,一如既往
參考: Dan Haywood博客博客中的JCG合作伙伴 Dan Haywood以編程方式在OpenJPA中注冊(cè)實(shí)體類型 。
翻譯自: https://www.javacodegeeks.com/2012/06/registering-entity-types-with-openjpa.html
總結(jié)
以上是生活随笔為你收集整理的以编程方式向OpenJPA注册实体类型的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java EE 6测试第二部分– Arq
- 下一篇: 令人印象深刻的第一个Apache Cam