问题
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