Java Code Examples for javax.persistence.EntityManager#merge()

The following examples show how to use javax.persistence.EntityManager#merge() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TicketPriceDTO.java    From monolith with Apache License 2.0 7 votes vote down vote up
public TicketPrice fromDTO(TicketPrice entity, EntityManager em)
{
   if (entity == null)
   {
      entity = new TicketPrice();
   }
   if (this.show != null)
   {
      entity.setShow(this.show.fromDTO(entity.getShow(), em));
   }
   if (this.section != null)
   {
      entity.setSection(this.section.fromDTO(entity.getSection(), em));
   }
   if (this.ticketCategory != null)
   {
      entity.setTicketCategory(this.ticketCategory.fromDTO(
            entity.getTicketCategory(), em));
   }
   entity.setPrice(this.price);
   entity = em.merge(entity);
   return entity;
}
 
Example 2
Source File: SecurityKeyTrustDAO.java    From peer-os with Apache License 2.0 7 votes vote down vote up
/******************************************
 *
 */
void update( SecurityKeyTrust SecurityKeyTrust )
{
    EntityManager em = daoManager.getEntityManagerFactory().createEntityManager();

    try
    {
        daoManager.startTransaction( em );
        em.merge( SecurityKeyTrust );
        daoManager.commitTransaction( em );
    }
    catch ( Exception ex )
    {
        daoManager.rollBackTransaction( em );
    }
    finally
    {
        daoManager.closeEntityManager( em );
    }
}
 
Example 3
Source File: InvoiceDB.java    From MusicStore with MIT License 7 votes vote down vote up
/**
 * Updates the invoice object in the database
 *
 * @param invoice The object to be updated
 */
public static void update(Invoice invoice) {
   EntityManager em = DBUtil.getEmFactory().createEntityManager();
   EntityTransaction transaction = em.getTransaction();

   try {
      transaction.begin();
      em.merge(invoice);
      transaction.commit();
   } catch (Exception e) {
      System.out.println(e);
      transaction.rollback();
   } finally {
      em.close();
   }
}
 
Example 4
Source File: EventDTO.java    From monolith with Apache License 2.0 6 votes vote down vote up
public Event fromDTO(Event entity, EntityManager em)
{
   if (entity == null)
   {
      entity = new Event();
   }
   entity.setName(this.name);
   if (this.mediaItem != null)
   {
      entity.setMediaItem(this.mediaItem.fromDTO(entity.getMediaItem(),
            em));
   }
   if (this.category != null)
   {
      entity.setCategory(this.category.fromDTO(entity.getCategory(), em));
   }
   entity.setDescription(this.description);
   entity = em.merge(entity);
   return entity;
}
 
Example 5
Source File: SubscriberDao.java    From peer-os with Apache License 2.0 6 votes vote down vote up
public void update( final String environmentId, final String subscriberId ) throws DaoException
{
    EntityManager em = emf.createEntityManager();
    try
    {
        em.getTransaction().begin();

        Subscriber subscriber = new Subscriber( environmentId, subscriberId );
        em.merge( subscriber );

        em.getTransaction().commit();
    }
    catch ( Exception e )
    {
        LOGGER.error( "Instance is not an entity or command invoked on a container-managed entity manager." );
        if ( em.getTransaction().isActive() )
        {
            em.getTransaction().rollback();
        }
        throw new DaoException( e );
    }
    finally
    {
        em.close();
    }
}
 
Example 6
Source File: SectionDTO.java    From monolith with Apache License 2.0 6 votes vote down vote up
public Section fromDTO(Section entity, EntityManager em)
{
   if (entity == null)
   {
      entity = new Section();
   }
   entity.setName(this.name);
   entity.setDescription(this.description);
   entity.setNumberOfRows(this.numberOfRows);
   entity.setRowCapacity(this.rowCapacity);
   if (this.venue != null)
   {
      entity.setVenue(this.venue.fromDTO(entity.getVenue(), em));
   }
   entity = em.merge(entity);
   return entity;
}
 
Example 7
Source File: JPAPersistenceManagerService.java    From incubator-batchee with Apache License 2.0 6 votes vote down vote up
@Override
public void markJobStarted(final long key, final Timestamp startTS) {
    final EntityManager em = emProvider.newEntityManager();
    try {
        final Object tx = txProvider.start(em);
        try {
            final JobExecutionEntity instance = em.find(JobExecutionEntity.class, key);
            instance.setBatchStatus(BatchStatus.STARTED);
            instance.setStartTime(startTS);
            instance.setUpdateTime(startTS);

            em.merge(instance);
            txProvider.commit(tx);
        } catch (final Exception e) {
            throw new BatchContainerRuntimeException(performRollback(tx, e));
        }
    } finally {
        emProvider.release(em);
    }
}
 
Example 8
Source File: RoleDAO.java    From peer-os with Apache License 2.0 6 votes vote down vote up
public void update( final Role item )
{
    EntityManager em = daoManager.getEntityManagerFromFactory();
    try
    {
        daoManager.startTransaction( em );
        em.merge( item );
        daoManager.commitTransaction( em );
    }
    catch ( Exception e )
    {
        daoManager.rollBackTransaction( em );

        LOG.error( e.getMessage() );
    }
    finally
    {
        daoManager.closeEntityManager( em );
    }
}
 
Example 9
Source File: PeerDataService.java    From peer-os with Apache License 2.0 6 votes vote down vote up
@Override
public void update( PeerData item )
{
    EntityManager em = emf.createEntityManager();
    try
    {
        em.getTransaction().begin();
        em.merge( item );
        em.getTransaction().commit();
    }
    catch ( Exception e )
    {
        LOG.error( e.toString(), e );
        if ( em.getTransaction().isActive() )
        {
            em.getTransaction().rollback();
        }
    }
    finally
    {
        em.close();
    }
}
 
Example 10
Source File: BaseDaoImpl.java    From metacat with Apache License 2.0 6 votes vote down vote up
@Override
public T save(final T entity, final boolean flush) {
    T result = null;
    final EntityManager entityManager = em.get();
    if (isNew(entity)) {
        entityManager.persist(entity);
        result = entity;
    } else {
        result = entityManager.merge(entity);
    }
    if (flush) {
        entityManager.flush();
    }

    return result;
}
 
Example 11
Source File: NestedTicketCategoryDTO.java    From monolith with Apache License 2.0 6 votes vote down vote up
public TicketCategory fromDTO(TicketCategory entity, EntityManager em)
{
   if (entity == null)
   {
      entity = new TicketCategory();
   }
   if (this.id != null)
   {
      TypedQuery<TicketCategory> findByIdQuery = em
            .createQuery(
                  "SELECT DISTINCT t FROM TicketCategory t WHERE t.id = :entityId",
                  TicketCategory.class);
      findByIdQuery.setParameter("entityId", this.id);
      try
      {
         entity = findByIdQuery.getSingleResult();
      }
      catch (javax.persistence.NoResultException nre)
      {
         entity = null;
      }
      return entity;
   }
   entity.setDescription(this.description);
   entity = em.merge(entity);
   return entity;
}
 
Example 12
Source File: AbstractJpaStorage.java    From apiman with Apache License 2.0 5 votes vote down vote up
/**
 * @param bean the bean to update
 * @throws StorageException if a storage problem occurs while storing a bean
 */
public <T> void update(T bean) throws StorageException {
    EntityManager entityManager = getActiveEntityManager();
    try {
        if (!entityManager.contains(bean)) {
            entityManager.merge(bean);
        }
    } catch (Throwable t) {
        logger.error(t.getMessage(), t);
        throw new StorageException(t);
    }
}
 
Example 13
Source File: JpaFactoryDao.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@Transactional
protected FactoryImpl doUpdate(FactoryImpl update) throws NotFoundException {
  final EntityManager manager = managerProvider.get();
  if (manager.find(FactoryImpl.class, update.getId()) == null) {
    throw new NotFoundException(
        format("Could not update factory with id %s because it doesn't exist", update.getId()));
  }
  if (update.getWorkspace() != null) {
    update.getWorkspace().getProjects().forEach(ProjectConfigImpl::prePersistAttributes);
  }
  FactoryImpl merged = manager.merge(update);
  manager.flush();
  return merged;
}
 
Example 14
Source File: NestedMediaItemDTO.java    From monolith with Apache License 2.0 5 votes vote down vote up
public MediaItem fromDTO(MediaItem entity, EntityManager em)
{
   if (entity == null)
   {
      entity = new MediaItem();
   }
   if (this.id != null)
   {
      TypedQuery<MediaItem> findByIdQuery = em
            .createQuery(
                  "SELECT DISTINCT m FROM MediaItem m WHERE m.id = :entityId",
                  MediaItem.class);
      findByIdQuery.setParameter("entityId", this.id);
      try
      {
         entity = findByIdQuery.getSingleResult();
      }
      catch (javax.persistence.NoResultException nre)
      {
         entity = null;
      }
      return entity;
   }
   entity.setMediaType(this.mediaType);
   entity.setUrl(this.url);
   entity = em.merge(entity);
   return entity;
}
 
Example 15
Source File: SectionAllocationDTO.java    From monolith with Apache License 2.0 5 votes vote down vote up
public SectionAllocation fromDTO(SectionAllocation entity, EntityManager em)
{
   if (entity == null)
   {
      entity = new SectionAllocation();
   }
   entity = em.merge(entity);
   return entity;
}
 
Example 16
Source File: NestedVenueDTO.java    From monolith with Apache License 2.0 5 votes vote down vote up
public Venue fromDTO(Venue entity, EntityManager em)
{
   if (entity == null)
   {
      entity = new Venue();
   }
   if (this.id != null)
   {
      TypedQuery<Venue> findByIdQuery = em.createQuery(
            "SELECT DISTINCT v FROM Venue v WHERE v.id = :entityId",
            Venue.class);
      findByIdQuery.setParameter("entityId", this.id);
      try
      {
         entity = findByIdQuery.getSingleResult();
      }
      catch (javax.persistence.NoResultException nre)
      {
         entity = null;
      }
      return entity;
   }
   entity.setName(this.name);
   if (this.address != null)
   {
      entity.setAddress(this.address.fromDTO(entity.getAddress(), em));
   }
   entity.setDescription(this.description);
   entity.setCapacity(this.capacity);
   entity = em.merge(entity);
   return entity;
}
 
Example 17
Source File: NestedTicketPriceDTO.java    From monolith with Apache License 2.0 5 votes vote down vote up
public TicketPrice fromDTO(TicketPrice entity, EntityManager em)
{
   if (entity == null)
   {
      entity = new TicketPrice();
   }
   if (this.id != null)
   {
      TypedQuery<TicketPrice> findByIdQuery = em
            .createQuery(
                  "SELECT DISTINCT t FROM TicketPrice t WHERE t.id = :entityId",
                  TicketPrice.class);
      findByIdQuery.setParameter("entityId", this.id);
      try
      {
         entity = findByIdQuery.getSingleResult();
      }
      catch (javax.persistence.NoResultException nre)
      {
         entity = null;
      }
      return entity;
   }
   entity.setPrice(this.price);
   entity = em.merge(entity);
   return entity;
}
 
Example 18
Source File: JpaAccountDao.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@Transactional
protected void doUpdate(AccountImpl update) throws NotFoundException {
  final EntityManager manager = managerProvider.get();
  final AccountImpl account = manager.find(AccountImpl.class, update.getId());
  if (account == null) {
    throw new NotFoundException(
        format("Couldn't update account with id '%s' because it doesn't exist", update.getId()));
  }
  manager.merge(update);
  manager.flush();
}
 
Example 19
Source File: NestedPerformanceDTO.java    From monolith with Apache License 2.0 5 votes vote down vote up
public Performance fromDTO(Performance entity, EntityManager em)
{
   if (entity == null)
   {
      entity = new Performance();
   }
   if (this.id != null)
   {
      TypedQuery<Performance> findByIdQuery = em
            .createQuery(
                  "SELECT DISTINCT p FROM Performance p WHERE p.id = :entityId",
                  Performance.class);
      findByIdQuery.setParameter("entityId", this.id);
      try
      {
         entity = findByIdQuery.getSingleResult();
      }
      catch (javax.persistence.NoResultException nre)
      {
         entity = null;
      }
      return entity;
   }
   entity.setDate(this.date);
   entity = em.merge(entity);
   return entity;
}
 
Example 20
Source File: TestConflictedNewFile.java    From desktop with GNU General Public License v3.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    EntityManager em = config.getDatabase().getEntityManager();
    Folder root = staticFunctionsTest.initConfig(config);
    
    
    //create F1
    File f1 = staticFunctionsTest.createFile(root.getLocalFile().getPath() + staticFunctionsTest.fileName1, "content1");
    CloneFile dbFile = staticFunctionsTest.indexNewRequest(root, f1, null);
    
    //create F2
    f1 = staticFunctionsTest.createFile(root.getLocalFile().getPath() + staticFunctionsTest.fileName1, "content2");        
    staticFunctionsTest.indexNewRequest(root, f1, dbFile);
                  
    Thread.sleep(5000);
    
    em.getTransaction().begin();
    dbFile = db.getFile(root, f1);                
    
    Object toBeRemoved = em.merge(dbFile);
    em.remove(toBeRemoved);
    
    em.flush();
    em.getTransaction().commit();
                    
    
    staticFunctionsTest.createFile(root.getLocalFile().getPath() + staticFunctionsTest.fileName1, "content1");
    //create a conflicted modification v2
    /*Update update = Update.parse(dbFile);
    update.setVersion(1);
    update.setStatus(Status.NEW);
    update.setServerUploadedAck(true);
    update.setServerUploadedTime(new Date());  
    
    List<Update> list = new ArrayList<Update>();
    list.add(update);
    
    Profile profile = config.getProfile();
    ChangeManager cm = profile.getRemoteWatcher().getChangeManager();
    cm.queueUpdates(list);                
    System.out.println("FINISH!!!!\n\n");        
    System.out.println("The result is: \n file1 exist with content2. \n file1 conflicted with content1. \n 2 rows in database New ACK, New conflicted.");
    while(true) { }*/
}