How can I get all @Entity classes from a Persistence Unit?

試著忘記壹切 提交于 2019-12-03 10:44:27

If Your jar is well-formed (persistence.xml at the right place - in the META-INF folder), then all looks fine.

It is not necessary to run your utility inside a container, JPA is not a part of JavaEE specs.

In case anyone's interested, Solution 1 worked. Here's essentially what I had to do:

public MySQLSchemaGenerator() throws ClassNotFoundException {
    Properties mySQLDialectProps = new Properties();
    mySQLDialectProps.setProperty("javax.persistence.transactionType", "RESOURCE_LOCAL");
    mySQLDialectProps.setProperty("javax.persistence.jtaDataSource", "");

    final EntityManagerFactory emf = Persistence.createEntityManagerFactory("<persistence_unit_name>", mySQLDialectProps);
    final Metamodel mm = emf.getMetamodel();
    for (final ManagedType<?> managedType : mm.getManagedTypes()) {
      managedType.getJavaType(); // this returns the java class of the @Entity object
    }
  }

The key was to override my transaction type and blank out the jtaDataSource which had been defined in my persistence.xml. Turns out everything else was unnecessary.

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