Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access JPA <persistence-unit-metadata> programmatically

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

like image 802
chris1069603 Avatar asked Nov 28 '11 15:11

chris1069603


1 Answers

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.

like image 79
chris1069603 Avatar answered Sep 21 '22 12:09

chris1069603