Tips for Managing Sustainability Context Manually

I am studying using JPA for my data access code. I am trying to write a business layer and data access layer so that it works in a web application and Java SE application. Therefore, I cannot use the container persistence context. Most of my JPA services show examples in a container-managed environment.

Currently, I get an EntityManagerFactory every time I create a new instance of the service class. For each operation (for example, add, get, etc.) I open the EntityManager, start the transaction, perform the operations, commit and close the EntityManager and EntityManagerFactory. I would like you to have problems maintaining context outside the Java EE environment.

Are there any recommendations when you are not using a container driven context? Are there any independent Java EE persistence context managers? Are there any recommended patterns?

Thank,

Al

Update

Thank you eveyone for the info. Everything was very helpful.

+3
source share
4 answers

I'm not sure about the best practices related to this, but I spent a lot of time doing something like this work.

Basically you need to build something EntityManagerwith. I have always used Spring for this. Their documentation documentation ):

<bean id="myEmf" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
  <property name="persistenceUnitName" value="myPersistenceUnit"/>
</bean>

, , , , , . , , - .

hibernate (persistence.xml META-INF/):

<property name="hibernate.connection.driver_class" value="com.company.driver" />
<property name="hibernate.connection.url" value="some:jdbc@string" />
<property name="hibernate.connection.username" value="myuser" />
<property name="hibernate.connection.password" value="mypassword" />

, Spring, EntityManagerFactory (.. context.getBean("myEmf")).

LocalContainerEntityManagerFactoryBean , . , , , , Hibernate. persistence.xml, (, , , oracle 10g):

<bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="myDatasource" />
    <property name="persistenceProvider">
        <bean class="org.hibernate.ejb.HibernatePersistence" />
    </property>
</bean>

Spring BasicDataSource Apache dbcp, .

, JPA . , , Hibernate JPA, , , JPA. , API- Hibernate.

+5

EntityManagerFactory , . (, add, get ..) EntityManager, , , EntityManager EntityManagerFactory.

EntityManagerFactory . EntityManagerFactory . , , , :

public class JpaUtil {
    private JpaUtil() {}

    private static EntityManagerFactory emf = null;

    static {
        try {
            emf = Persistence.createEntityManagerFactory("MyPu");
        } catch (Throwable ex) {
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static EntityManager getEntityManager() {
        return emf.createEntityManager();
    }
}

( ThreadLocal, . ), EntityManager .

, Java EE.

- , Spring EntityManager Spring beans, Java SE.

JPA Spring Spring , , :

  • LocalEntityManagerFactoryBean, EntityManagerFactory
  • JpaTransactionManager JPA
  • <tx:annotation-driven />, Spring @Transactional
  • bean!

Spring, .

.

+4

JPA Java EE. :

Java EE.

, Java EE "open a EntityManager, , EntityManager, PITA .

EE, JPA Spring. . , JpaDaoSupport Transactional .

EE:

    import java.util.List;
    import org.springframework.orm.jpa.support.JpaDaoSupport;

    public class SpeakerDAOImpl extends JpaDaoSupport implements SpeakerDAO {

    public Speaker findSpeaker(long id) {
       return getJpaTemplate().find(Speaker.class,id);
    }

    @Transactional
    public void saveSpeaker(Speaker s) {
       getJpaTemplate().persist(s);
    }
  }

, ..:

+1

, Ebean ORM JPA- (@Entity ..), " " "- , EntityManager.

Ebean API JPA ( "" API - EntityManager, / beans ..), EntityManager, .

0

Source: https://habr.com/ru/post/1761320/


All Articles