Finding the current persistence unit for the current EntityManagerFactory

后端 未结 2 964
长情又很酷
长情又很酷 2021-01-19 07:11

I noticed that calling createEntityManagerFactory(null) will use the default Persistence Unit (PU) in the configuration file. Sometimes the classpaths get real

2条回答
  •  隐瞒了意图╮
    2021-01-19 07:25

    I found a way to do it if the provider is hibernate:

    
    Session s=(Session)em.getDelegate();
    
    //Get the URL.
    String info=null;
    try {
     //The wonderful Hibernate team deprecated connection() before giving an alternative.
     //Feel free to share the love at http://opensource.atlassian.com/projects/hibernate/browse/HHH-2603
     //cf http://opensource.atlassian.com/projects/hibernate/browse/HHH-2603?focusedCommentId=29531&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#action_29531
     info=s.connection().getMetaData().getURL();
    } catch (Exception e) {
     throw new RuntimeException(e);
    }
    
    //Finish
    em.close();
    
    return info;
    

    Note that em is an opened EntityManager created from an EntityManagerFactory. Also, like the comments? That's right, the Hibernate team is full of surprises again. Even though users gave feedback begging not to deprecate the only method that can help me, they are going full steam ahead. Note this link.

    I'll upvote any good suggestions for a more stable JPA provider. Hibernate is just getting too unstable. This is definitely not the first issue of this kind that I had to workaround.

提交回复
热议问题