Accessing Hibernate facet model

Does the OpenAPI include the persistence model used by the Hibernate facet?

In short, this is the information I'm after:

  • entities making up the model

  • entity relations

  • persistent attributes


I don't need to modify the model in any way, just navigate it.
I assume/hope the model abstracts the difference between XML-based and annotations-based metadata.

A minimal code example to get access to the model and (for example) find an entity from an FQN class name would be appreciated.

6 comments
Comment actions Permalink

Hello Taras,

JPA and Hibernate models are exposed via persistence openapi which is
packaged as PersistenceSupport plugin. There's persistence-openapi.jar
with public interfaces/utility classes.

JPA & Hibernate facets inherit
com.intellij.persistence.facet.PersistenceFacetBase class which defines
methods to access persistence model from top (project) to bottom
(entities/attributes).

com.intellij.persistence.util.PersistenceCommonUtil has methods to
discover those facets in a module or a project.

To quickly access persistence model from bottom to top there is a notion
of persistence class roles (PersistenceClassRole).

The method PersistenceCommonUtil.getPersistenceRoles(PsiClass) returns
all the roles associated with the class specified.

for (PersistenceClassRole role: PersistenceCommonUtil.getPersistenceRoles(
JavaPsiFacade.getInstance().findClass(FQN, scope), ENTITY)) {
if (role.getType() != PersistenceClassRoleEnum.ENTITY) continue;
PersistenceObject entity = role.getPersistenceObject();
for (PersistenceAttribute attribute :
entity.getObjectModelHelper().getAttributes()) {
// here we have PersistenceAttribute
}
}

There is also a way to browse Persistence Model with "model browser"
com.intellij.persistence.util.PersistenceModelBrowser.

A browser instance can be created by
com.intellij.persistence.PersistenceHelper service and there are a
couple of utility "create" methods in PersistenceCommonUtil.







Taras Tielkes wrote:

Does the OpenAPI include the persistence model used by the Hibernate facet?

In short, this is the information I'm after:

  • entities making up the model

  • entity relations

  • persistent attributes


I don't need to modify the model in any way, just navigate it.
I assume/hope the model abstracts the difference between XML-based and annotations-based metadata.

A minimal code example to get access to the model and (for example) find an entity from an FQN class name would be appreciated.

0
Comment actions Permalink

Thanks Gregory.

Two additional questions for now:
1) How can I distinguish between JPA and Hibernate?
2) Is the API backwards-compatible to 7.0?

Cheers,

Taras

0
Comment actions Permalink

Taras Tielkes wrote:

Thanks Gregory.

Two additional questions for now:
1) How can I distinguish between JPA and Hibernate?


On the facet level you can use getFacetType.

2) Is the API backwards-compatible to 7.0?


Partially.


Cheers,

Taras

0
Comment actions Permalink

Thanks Gregory

I'm using a ReferenceProvider to inject persistent attribute names. How do I get the relevant PsiReference to use?

Should I use PersistentAttribute#getIdentifyingPsiElement()? I want a refactor action on the attribute name reference to work for both .hbm.xml as well as @Entity style attributes.

0

Please sign in to leave a comment.