Switch to full style
Java persistent API
Post a reply

beginning and finishing the JPA transaction

Tue Apr 20, 2010 4:31 pm

He is an small example to show you how to control the transaction (like starting and committing actions ) .


Code:

package com
.codemiles.jpa;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public abstract class 
JPAService {
    private   
EntityManager  entityManager;
    
/**
     * @param args
     */
    
public void insertTopic(Topic topic){
     
EntityManagerFactory factory Persistence
         
.createEntityManagerFactory("persistenceUnitName");
         
entityManager factory.createEntityManager();
         
entityManager.getTransaction().begin();
         
entityManager.persist(topic);
         
entityManager.getTransaction().commit();
         
entityManager.close();
         
factory.close();
         
    }
 

}

 




Post a reply

Topic Tags

Java JPA