Access JPA <persistence-unit-metadata> programmatically

梦想与她 提交于 2019-12-03 07:43:27

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.

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();

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

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