Example usage for javax.persistence EntityManager contains

List of usage examples for javax.persistence EntityManager contains

Introduction

In this page you can find the example usage for javax.persistence EntityManager contains.

Prototype

public boolean contains(Object entity);

Source Link

Document

Check if the instance is a managed entity instance belonging to the current persistence context.

Usage

From source file:com.autentia.common.util.ejb.EntityManagerUtils.java

/**
 * Saves an entity in the persistence layer. If the entity doesn't exist, this method inserts the entity. If the
 * entity already exists in the persistence layer, this method updates the entity.
 * <p>/*from w w w.j  a va2 s.  c o  m*/
 * Usage example: <code>save(em, company, company.getId())</code>
 * 
 * @param em the {@link EntityManager} where the inserto or update will be executed.
 * @param entity the entity to save.
 * @param id the primary key of the entity.
 */
public static void save(EntityManager em, Object entity, Object id) {
    if (id == null) {
        em.persist(entity);
    } else if (!em.contains(entity)) {
        em.merge(entity);
    }
}

From source file:nl.b3p.kaartenbalie.service.servlet.GeneralServlet.java

private static void setDetachedUserLastLoginStatus(User user, String status, EntityManager em) {

    if (!updateUserLastLoginStatus) {
        return;//from   w  w  w.j  a va2  s  .  co m
    }

    // Alleen indien user detached is updaten via aparte query

    // Komt alleen voor indien ongeldig IP of time expired nadat al
    // succesvol was ingelogd!
    if (!em.contains(user)) {
        em.createQuery("update User set lastLoginStatus = :status where id = :id")
                .setParameter("status", status).setParameter("id", user.getId()).executeUpdate();
    } else {
        // User is persistent
        user.setLastLoginStatus(status);
    }
}

From source file:org.apereo.portal.groups.pags.dao.jpa.JpaPersonAttributesGroupTestDefinitionDao.java

@PortalTransactional
@Override//from   www . j a  va  2  s .c o  m
public void deletePersonAttributesGroupTestDefinition(IPersonAttributesGroupTestDefinition definition) {
    Validate.notNull(definition, "definition can not be null");

    final IPersonAttributesGroupTestDefinition persistentDefinition;
    final EntityManager entityManager = this.getEntityManager();
    if (entityManager.contains(definition)) {
        persistentDefinition = definition;
    } else {
        persistentDefinition = entityManager.merge(definition);
    }
    entityManager.remove(persistentDefinition);
}

From source file:org.apereo.portal.groups.pags.dao.jpa.JpaPersonAttributesGroupTestGroupDefinitionDao.java

@PortalTransactional
@Override/*ww w. jav a2  s .com*/
public void deletePersonAttributesGroupTestGroupDefinition(
        IPersonAttributesGroupTestGroupDefinition definition) {
    Validate.notNull(definition, "definition can not be null");

    final IPersonAttributesGroupTestGroupDefinition persistentDefinition;
    final EntityManager entityManager = this.getEntityManager();
    if (entityManager.contains(definition)) {
        persistentDefinition = definition;
    } else {
        persistentDefinition = entityManager.merge(definition);
    }
    entityManager.remove(persistentDefinition);
}

From source file:org.apereo.portal.groups.pags.dao.jpa.JpaPersonAttributesGroupTestDefinitionDao.java

@PortalTransactional
@Override/*from  w  ww.  j  a  v a 2s . co m*/
public IPersonAttributesGroupTestDefinition updatePersonAttributesGroupTestDefinition(
        IPersonAttributesGroupTestDefinition personAttributesGroupTestDefinition) {
    Validate.notNull(personAttributesGroupTestDefinition,
            "personAttributesGroupTestDefinition can not be null");

    final IPersonAttributesGroupTestDefinition persistentDefinition;
    final EntityManager entityManager = this.getEntityManager();
    if (entityManager.contains(personAttributesGroupTestDefinition)) {
        persistentDefinition = personAttributesGroupTestDefinition;
    } else {
        persistentDefinition = entityManager.merge(personAttributesGroupTestDefinition);
    }

    this.getEntityManager().persist(persistentDefinition);
    return persistentDefinition;
}

From source file:org.apereo.portal.groups.pags.dao.jpa.JpaPersonAttributesGroupTestGroupDefinitionDao.java

@PortalTransactional
@Override//from w  w  w .j a  v  a 2s .co  m
public IPersonAttributesGroupTestGroupDefinition updatePersonAttributesGroupTestGroupDefinition(
        IPersonAttributesGroupTestGroupDefinition personAttributesGroupTestGroupDefinition) {
    Validate.notNull(personAttributesGroupTestGroupDefinition,
            "personAttributesGroupTestGroupDefinition can not be null");

    final IPersonAttributesGroupTestGroupDefinition persistentDefinition;
    final EntityManager entityManager = this.getEntityManager();
    if (entityManager.contains(personAttributesGroupTestGroupDefinition)) {
        persistentDefinition = personAttributesGroupTestGroupDefinition;
    } else {
        persistentDefinition = entityManager.merge(personAttributesGroupTestGroupDefinition);
    }

    this.getEntityManager().persist(persistentDefinition);
    return persistentDefinition;
}

From source file:org.apereo.portal.portlet.dao.jpa.JpaPortletTypeDao.java

@Override
@PortalTransactional/*from   w  ww  .j av  a  2  s .  c  o  m*/
public void deletePortletType(IPortletType type) {
    Validate.notNull(type, "definition can not be null");

    final IPortletType persistentChanneltype;
    final EntityManager entityManager = this.getEntityManager();
    if (entityManager.contains(type)) {
        persistentChanneltype = type;
    } else {
        persistentChanneltype = entityManager.merge(type);
    }
    entityManager.remove(persistentChanneltype);
}

From source file:org.apereo.portal.layout.dao.jpa.JpaStylesheetDescriptorDao.java

@PortalTransactional
@Override//ww  w  .ja v a 2s . c o m
public void deleteStylesheetDescriptor(IStylesheetDescriptor stylesheetDescriptor) {
    Validate.notNull(stylesheetDescriptor, "definition can not be null");

    final IStylesheetDescriptor persistentStylesheetDescriptor;
    final EntityManager entityManager = this.getEntityManager();
    if (entityManager.contains(stylesheetDescriptor)) {
        persistentStylesheetDescriptor = stylesheetDescriptor;
    } else {
        persistentStylesheetDescriptor = entityManager.merge(stylesheetDescriptor);
    }

    entityManager.remove(persistentStylesheetDescriptor);
}

From source file:org.jasig.portal.groups.pags.dao.jpa.JpaPersonAttributesGroupDefinitionDao.java

@PortalTransactional
@Override//from  w w  w.j  ava 2  s. co  m
public IPersonAttributesGroupDefinition updatePersonAttributesGroupDefinition(
        IPersonAttributesGroupDefinition personAttributesGroupDefinition) {
    Validate.notNull(personAttributesGroupDefinition, "personAttributesGroupDefinition can not be null");

    final IPersonAttributesGroupDefinition persistentDefinition;
    final EntityManager entityManager = this.getEntityManager();
    if (entityManager.contains(personAttributesGroupDefinition)) {
        persistentDefinition = personAttributesGroupDefinition;
    } else {
        persistentDefinition = entityManager.merge(personAttributesGroupDefinition);
    }
    this.getEntityManager().persist(persistentDefinition);
    return persistentDefinition;
}

From source file:org.apereo.portal.layout.dlm.FragmentDefinitionDao.java

@Override
@PortalTransactional/*from  w w w .j a v  a  2s .  co  m*/
public void removeFragmentDefinition(FragmentDefinition fd) {

    Validate.notNull(fd, "FragmentDefinition can not be null");
    final FragmentDefinition persistentFragmentDefinition;
    final EntityManager entityManager = this.getEntityManager();
    if (entityManager.contains(fd)) {
        persistentFragmentDefinition = fd;
    } else {
        persistentFragmentDefinition = entityManager.merge(fd);
    }

    entityManager.remove(persistentFragmentDefinition);
}