下面列出了javax.persistence.EntityManager#merge ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
/******************************************
*
*/
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 );
}
}
/**
* 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();
}
}
@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;
}
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;
}
@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();
}
}
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 );
}
}
@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);
}
}
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;
}
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();
}
}
@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;
}
@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();
}
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;
}
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;
}
/**
* @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);
}
}
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;
}
public SectionAllocation fromDTO(SectionAllocation entity, EntityManager em)
{
if (entity == null)
{
entity = new SectionAllocation();
}
entity = em.merge(entity);
return entity;
}
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;
}
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;
}
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) { }*/
}