本文整理汇总了Java中javax.persistence.EntityManager.close方法的典型用法代码示例。如果您正苦于以下问题:Java EntityManager.close方法的具体用法?Java EntityManager.close怎么用?Java EntityManager.close使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.persistence.EntityManager
的用法示例。
在下文中一共展示了EntityManager.close方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getCandidateByEmail
import javax.persistence.EntityManager; //导入方法依赖的package包/类
public static Candidates getCandidateByEmail(String email) {
if (email != null && !email.isEmpty() && CustomUtilities.isValidEmail(email)) {
EntityManager em = EMFUtil.getEMFactory().createEntityManager();
String query = "SELECT candidate FROM Candidates candidate WHERE candidate.email =:email";
try {
TypedQuery<Candidates> q = em.createQuery(query, Candidates.class);
q.setParameter("email", email);
return q.getSingleResult();
} catch (Exception e) {
} finally {
em.close();
}
}
return null;
}
示例2: findApplyAndMerge
import javax.persistence.EntityManager; //导入方法依赖的package包/类
/**
* So you want to load an entity, set some data, and save it again, without detaching it from the persistence
* context and without bothering with the EntityManager?
* Look no further! Functional programming to the rescue, just pass a function that does the required transformation
* on the entity.
* <p>
* NOTE that this will create a new instance of the entity if it does not exist yet.
*
*/
@Nonnull
public <E extends SaucedEntity<I, E>, I extends Serializable> E findApplyAndMerge(@Nonnull final Transfiguration<I, E> transfiguration)
throws DatabaseException {
final EntityManager em = this.databaseConnection.getEntityManager();
try {
return this.lockedWrappedTransformFunc(transfiguration).apply(em);
} catch (final PersistenceException e) {
final String message = String.format("Failed to find, apply and merge entity id %s of class %s on DB %s",
transfiguration.key.id.toString(), transfiguration.key.clazz.getName(),
this.databaseConnection.getName());
throw new DatabaseException(message, e);
} finally {
em.close();
}
}
示例3: createEntity
import javax.persistence.EntityManager; //导入方法依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
public long createEntity(Product entity) {
PersistenceProduct product = new PersistenceProduct();
product.setName(entity.getName());
product.setDescription(entity.getDescription());
product.setListPriceInCents(entity.getListPriceInCents());
EntityManager em = getEM();
try {
em.getTransaction().begin();
PersistenceCategory cat = em.find(PersistenceCategory.class, entity.getCategoryId());
if (cat != null) {
product.setCategory(cat);
em.persist(product);
} else {
product.setId(-1L);
}
em.getTransaction().commit();
} finally {
em.close();
}
return product.getId();
}
示例4: main
import javax.persistence.EntityManager; //导入方法依赖的package包/类
public static void main(String[] args) {
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("pl.edu.bogdan.training.db.entity");
EntityManager em = entityManagerFactory.createEntityManager();
// begining of transaction
em.getTransaction().begin();
// Creating query in the way very similar to SQL
Query query = em.createQuery("Select r from Role r where r.name = :name");
query.setParameter("name", "ordinary");
// We obtaining result as a list of entities
List<Role> roles = query.getResultList();
for (Role role : roles) {
System.out.println(role.getName());
for (User u : role.getUsers()) {
System.out.println(" " + u.getLastName());
}
}
em.getTransaction().commit();
em.close();
entityManagerFactory.close();
}
示例5: jpa_with_parent_and_active_span_only
import javax.persistence.EntityManager; //导入方法依赖的package包/类
@Test
public void jpa_with_parent_and_active_span_only() {
try (Scope ignored = mockTracer.buildSpan("parent").startActive(true)) {
EntityManagerFactory entityManagerFactory = Persistence
.createEntityManagerFactory("jpa_active_span_only");
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
entityManager.persist(new Employee());
entityManager.persist(new Employee());
entityManager.getTransaction().commit();
entityManager.close();
entityManagerFactory.close();
}
List<MockSpan> spans = mockTracer.finishedSpans();
assertEquals(11, spans.size());
checkSameTrace(spans);
assertNull(mockTracer.activeSpan());
}
示例6: persist
import javax.persistence.EntityManager; //导入方法依赖的package包/类
public static Conversations persist (Conversations conversation) {
if (conversation != null) {
EntityManager em = EMFUtil.getEMFactory().createEntityManager();
EntityTransaction trans = em.getTransaction();
try {
trans.begin();
em.persist(conversation);
trans.commit();
return conversation;
} catch (Exception e) {
e.printStackTrace();
trans.rollback();
} finally {
em.close();
}
}
return null;
}
示例7: getByClientAndCandidateID
import javax.persistence.EntityManager; //导入方法依赖的package包/类
public static ClientPipelines getByClientAndCandidateID (int clientID, int candidateID) {
if (clientID > 0 && candidateID > 0) {
EntityManager em = EMFUtil.getEMFactory().createEntityManager();
try {
String query = "SELECT elt FROM ClientPipelines elt WHERE elt.clientID = :clientID AND elt.candidateID = :candidateID";
TypedQuery<ClientPipelines> q = em.createQuery(query, ClientPipelines.class);
q.setParameter("clientID", clientID);
q.setParameter("candidateID", candidateID);
return q.getSingleResult();
} catch (Exception e) {
e.printStackTrace();
} finally {
em.close();
}
}
return null;
}
示例8: getCar
import javax.persistence.EntityManager; //导入方法依赖的package包/类
@Override
public Car getCar(String id) {
EntityManager em = emf.createEntityManager();
try {
return em.find(Car.class, id);
} finally {
em.close();
}
}
示例9: getDailyRegistry
import javax.persistence.EntityManager; //导入方法依赖的package包/类
/**
* Returns the list of bookings that should be displayed in the daily registry for today's
* date.
*/
public List<Booking> getDailyRegistry() {
List<Booking> results;
EntityManager entityManager = HibernateUtil.getEntityManager();
String queryString = "FROM Booking WHERE isCheckedIn = true "
+ "OR trunc(sysdate) BETWEEN checkInDate and checkOutDate";
Query q = entityManager.createQuery(queryString);
results = q.getResultList();
entityManager.close();
return results;
}
示例10: registrate
import javax.persistence.EntityManager; //导入方法依赖的package包/类
private void registrate(User user, String plainTextPassword) {
generatePassword(user, plainTextPassword);
LOGGER.info("persisting user " + user.getUsername() + " with password " + user.getPassword() + " and salt " + user.getSalt());
EntityManagerFactory emf = Persistence.createEntityManagerFactory("userData");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
em.persist(user);
LOGGER.info("user persisted");
tx.commit();
em.close();
}
示例11: carLifecycleRL
import javax.persistence.EntityManager; //导入方法依赖的package包/类
/**
* Create, find and delete car using resource local transactions
* @param emf
* @throws BundleException
*/
protected void carLifecycleRL(EntityManager em) throws BundleException {
em.getTransaction().begin();
Car car = createBlueCar();
em.persist(car);
em.getTransaction().commit();
Car car2 = em.find(Car.class, BLUE_PLATE);
assertBlueCar(car2);
em.getTransaction().begin();
em.remove(car2);
em.getTransaction().commit();
em.close();
}
示例12: addCar
import javax.persistence.EntityManager; //导入方法依赖的package包/类
@Override
public void addCar(Car car) {
EntityManager localEm = emf.createEntityManager();
localEm.persist(car);
localEm.flush();
localEm.close();
}
示例13: getCurrentlyCheckedInGuests
import javax.persistence.EntityManager; //导入方法依赖的package包/类
public List<Guest> getCurrentlyCheckedInGuests() {
EntityManager entityManager = HibernateUtil.getEntityManager();
String query = "SELECT b.guest FROM Booking AS b WHERE b.isCheckedIn = true";
Query q = entityManager.createQuery(query);
List<Guest> results = q.getResultList();
entityManager.close();
return results;
}
示例14: getByID
import javax.persistence.EntityManager; //导入方法依赖的package包/类
public static Certifications getByID(int certificationID) {
if (certificationID > 0) {
EntityManager em = EMFUtil.getEMFactory().createEntityManager();
Certifications certification = em.find(Certifications.class, certificationID);
em.close();
return certification;
}
return null;
}
示例15: jButton1ActionPerformed
import javax.persistence.EntityManager; //导入方法依赖的package包/类
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
Object[] selected = inactiveList.getSelectedValues();
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
for (Object plugin : selected) {
Plugins p = (Plugins) plugin;
p.setActive(true);
inactivemodel.removeElement(p);
activemodel.addElement(p);
p = em.merge(p);
em.persist(p);
}
em.getTransaction().commit();
em.close();
}