Access JPA <persistence-unit-metadata> programmatically

泪湿孤枕 提交于 2019-12-04 12:01:29

问题


is it possible to access the information in <persistence-unit-metadata> through Java API?

<persistence-unit-metadata>
    <persistence-unit-defaults>
        <schema>MySchema</schema>
    </persistence-unit-defaults>
</persistence-unit-metadata>

I would like to read the schema "MySchema" via JPA API or EclipseLink API, which is the implementation I use.

Something like: entityManager.getDefaults().getSchema(); It's OK to cast or use any EclipseLink class, that's fine for this.

Thank you


回答1:


After debugging for a while I found a solution to access the schema of an entity.

EntityType<MyEntity> entity = emf.getMetamodel().entity(MyEntity.class);

EntityTypeImpl entityTypeImpl = (EntityTypeImpl) entity;        
ClassDescriptor descriptor =  entityTypeImpl.getDescriptor();

String schema = descriptor.getDefaultTable().getTableQualifier();

Looking for an easier and better way to access the information! Thank you so much.




回答2:


I know this is an old question, but here is a simpler way to get the table name:

MyEntity.class.getAnnotation(javax.persistence.Entity.class).name();




回答3:


The previous replies didn't work for me. This is what I found to work:

String schema = em.unwrap(JpaEntityManager.class).getServerSession().getDescriptor(MyClass.class).getTables().get(0).getTableQualifier();

https://wiki.eclipse.org/EclipseLink/FAQ/JPA




回答4:


I know is an old post, but worked for me with this

javax.persistence.Table table = MyEntity.class.getAnnotation(javax.persistence.Table.class)

from there you can get:

table.catalog()
table.indexes()
table.name()
table.schema()
table.uniqueConstraints()


来源:https://stackoverflow.com/questions/8298058/access-jpa-persistence-unit-metadata-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!