@Test public void testCascadeFromDetachedToNonDirtyRepresentations() { Item item1 = new Item(); item1.setName("item1"); Hoarder hoarder = new Hoarder(); hoarder.setName("joe"); Session s = openSession(); Transaction tx = session.beginTransaction(); s.persist(item1); s.persist(hoarder); tx.commit(); s.close(); // Get another representation of the same Item from a different session. s = openSession(); Item item1_1 = (Item) s.get(Item.class, item1.getId()); s.close(); // item1_1 and item1_2 are unmodified representations of the same persistent entity. assertFalse(item1 == item1_1); assertTrue(item1.equals(item1_1)); // Update hoarder (detached) to references both representations. hoarder.getItems().add(item1); hoarder.setFavoriteItem(item1_1); s = openSession(); tx = s.beginTransaction(); hoarder = (Hoarder) s.merge(hoarder); assertEquals(1, hoarder.getItems().size()); assertSame(hoarder.getFavoriteItem(), hoarder.getItems().iterator().next()); assertEquals(item1.getId(), hoarder.getFavoriteItem().getId()); assertEquals(item1.getCategory(), hoarder.getFavoriteItem().getCategory()); tx.commit(); s.close(); s = openSession(); tx = s.beginTransaction(); hoarder = (Hoarder) s.merge(hoarder); assertEquals(1, hoarder.getItems().size()); assertSame(hoarder.getFavoriteItem(), hoarder.getItems().iterator().next()); assertEquals(item1.getId(), hoarder.getFavoriteItem().getId()); assertEquals(item1.getCategory(), hoarder.getFavoriteItem().getCategory()); tx.commit(); s.close(); cleanup(); }
@Transactional(value = "transactionManager") public DatosCancion guardar(DatosCancion datos) { Session session = sessionFactory.getCurrentSession(); datos = (DatosCancion) session.merge(datos); return datos; }
@Override public Almacen actualiza(Almacen almacen, Usuario usuario) { Session session = currentSession(); if (usuario != null) { almacen.setEmpresa(usuario.getEmpresa()); } try { // Actualiza la almacen log.debug("Actualizando almacen"); session.update(almacen); session.flush(); } catch (NonUniqueObjectException e) { try { session.merge(almacen); } catch (Exception ex) { log.error("No se pudo actualizar la almacen", ex); throw new RuntimeException("No se pudo actualizar la almacen", ex); } } if (usuario != null) { usuario.setAlmacen(almacen); } session.flush(); return almacen; }
public void update(Dataset dataset, org.hibernate.Session hibernateSession) { hibernateSession.merge(dataset); sessionResource.publish( sessionId.toString(), new SessionEvent(sessionId, ResourceType.DATASET, dataset.getDatasetId(), EventType.UPDATE), hibernateSession); }
/** * Merges the given {@link com.yammer.maestro.models.Log}. * * @param entity the entity to merge * @return the persistent entity */ public Log merge(Log entity) throws HibernateException { Log log = null; boolean ownsSession = false; Session session = null; Transaction txn = null; try { session = currentSession(); } catch (HibernateException ex) { session = sessionFactory.openSession(); txn = session.beginTransaction(); ownsSession = true; } try { log = (Log) session.merge(entity); } catch (Exception ex) { if (ownsSession) { txn.rollback(); } } finally { if (ownsSession) { txn.commit(); session.close(); } } return log; }
public Asociacion actualiza(Asociacion asociacion, Usuario usuario) { Session session = currentSession(); if (usuario != null) { asociacion.setUnion(usuario.getAsociacion().getUnion()); } try { // Actualiza la asociacion log.debug("Actualizando asociacion"); session.update(asociacion); session.flush(); } catch (NonUniqueObjectException e) { try { session.merge(asociacion); } catch (Exception ex) { log.error("No se pudo actualizar la asociacion", ex); throw new RuntimeException("No se pudo actualizar la asociacion", ex); } } if (usuario != null) { usuario.setAsociacion(asociacion); } session.flush(); return asociacion; }
public SelfBilledCreditNoteType merge(SelfBilledCreditNoteType value) { SelfBilledCreditNoteType newValue = null; Session session = getSession(); newValue = (SelfBilledCreditNoteType) session.merge(value); if (log.isDebugEnabled()) log.debug("merging document: " + value); return newValue; }
public LanguageCodeContentType merge(LanguageCodeContentType value) { LanguageCodeContentType newValue = null; Session session = getSession(); newValue = (LanguageCodeContentType) session.merge(value); if (log.isDebugEnabled()) log.debug("merging document: " + value); return newValue; }
public WarrantyInformationType merge(WarrantyInformationType value) { WarrantyInformationType newValue = null; Session session = getSession(); newValue = (WarrantyInformationType) session.merge(value); if (log.isDebugEnabled()) log.debug("merging document: " + value); return newValue; }
public DeliveryUnitType merge(DeliveryUnitType value) { DeliveryUnitType newValue = null; Session session = getSession(); newValue = (DeliveryUnitType) session.merge(value); if (log.isDebugEnabled()) log.debug("merging document: " + value); return newValue; }
public CorporateRegistrationSchemeType merge(CorporateRegistrationSchemeType value) { CorporateRegistrationSchemeType newValue = null; Session session = getSession(); newValue = (CorporateRegistrationSchemeType) session.merge(value); if (log.isDebugEnabled()) log.debug("merging document: " + value); return newValue; }
@Test public void testBackRefToProxiedEntityOnMerge() { Session s = openSession(); s.beginTransaction(); Parent me = new Parent("Steve"); me.getChildren().add(new Child("Joe")); s.persist(me); s.getTransaction().commit(); s.close(); // while detached, add a new element me.getChildren().add(new Child("Cece")); me.getChildren().add(new Child("Austin")); s = openSession(); s.beginTransaction(); // load 'me' to associate it with the new session as a proxy (this may have occurred as 'prior // work' // to the reattachment below)... Object meProxy = s.load(Parent.class, me.getName()); assertFalse(Hibernate.isInitialized(meProxy)); // now, do the reattchment... s.merge(me); s.getTransaction().commit(); s.close(); s = openSession(); s.beginTransaction(); s.createQuery("delete from Child").executeUpdate(); s.createQuery("delete from Parent").executeUpdate(); s.getTransaction().commit(); s.close(); }
public TransportEquipmentSealType merge(TransportEquipmentSealType value) { TransportEquipmentSealType newValue = null; Session session = getSession(); newValue = (TransportEquipmentSealType) session.merge(value); if (log.isDebugEnabled()) log.debug("merging document: " + value); return newValue; }
public TargetCurrencyCodeType merge(TargetCurrencyCodeType value) { TargetCurrencyCodeType newValue = null; Session session = getSession(); newValue = (TargetCurrencyCodeType) session.merge(value); if (log.isDebugEnabled()) log.debug("merging document: " + value); return newValue; }
/** * Fetch a synonym from the local cache. Failing that, create a new one. * * @param synonymType the synonym type (should be a term in the <code>genedb_synonym_type</code> * CV). * @param synonymString the actual synonym * @return the cached or newly-created synonym object */ public synchronized Synonym getSynonym(String synonymType, String synonymString) { logger.debug(String.format("Looking for synonym '%s:%s'", synonymType, synonymString)); if (session == null) { throw new IllegalStateException("getSynonym called with no session"); } if (synonymsByType.containsKey(synonymType, synonymString)) { logger.debug("Synonym found in cache"); return synonymsByType.get(synonymType, synonymString); } if (detachedSynonymsByType.containsKey(synonymType, synonymString)) { logger.debug("Synonym found in detached cache. Merging"); Synonym mergedSynonym = (Synonym) session.merge(detachedSynonymsByType.get(synonymType, synonymString)); detachedSynonymsByType.remove(synonymType, synonymString); synonymsByType.put(synonymType, synonymString, mergedSynonym); return mergedSynonym; } logger.debug(String.format("Synonym '%s:%s' not found in cache", synonymType, synonymString)); CvTerm synonymTypeCvTerm = (CvTerm) session.load( CvTerm.class, objectManager.getIdOfExistingCvTerm("genedb_synonym_type", synonymType)); Synonym synonym = new Synonym(synonymTypeCvTerm, synonymString, synonymString); synonymsByType.put(synonymType, synonymString, synonym); return synonym; }
@Test public void testTopLevelManyToOneManagedNestedIsDetached() { Item item1 = new Item(); item1.setName("item1 name"); Category category = new Category(); category.setName("category"); item1.setCategory(category); category.setExampleItem(item1); Session s = openSession(); Transaction tx = s.beginTransaction(); s.persist(item1); tx.commit(); s.close(); // get another representation of item1 s = openSession(); tx = s.beginTransaction(); Item item1_1 = (Item) s.get(Item.class, item1.getId()); tx.commit(); s.close(); s = openSession(); tx = s.beginTransaction(); Item item1Merged = (Item) s.merge(item1); item1Merged.setCategory(category); category.setExampleItem(item1_1); // now item1Merged is managed and it has a nested detached item s.merge(item1Merged); assertEquals(category.getName(), item1Merged.getCategory().getName()); assertSame(item1Merged, item1Merged.getCategory().getExampleItem()); tx.commit(); s.close(); s = openSession(); tx = s.beginTransaction(); item1 = (Item) s.get(Item.class, item1.getId()); assertEquals(category.getName(), item1.getCategory().getName()); assertSame(item1, item1.getCategory().getExampleItem()); tx.commit(); s.close(); cleanup(); }
@Test(expected = HibernateException.class) public void testCreateDataKO() { // Setup Person user = new Person(); when(session.merge(user)).thenThrow(new HibernateException("fail")); // Action dao.savePerson(user); }
@Override @CacheEvict(value = "objectPermanent", allEntries = true) public GeoObject mergeUpdate(GeoObject object, boolean... forceFlush) { Session s = getCurrentSession(); s.merge(object); flushSession(s, forceFlush); return object; }
public void delete(final T t) { final Session session = this.sessionFactory.getCurrentSession(); t.setDeleted(true); session.merge(t); }
@Test public void testTopLevelManyToOneChangedToNewEntity() { Item item1 = new Item(); item1.setName("item1 name"); Category category = new Category(); category.setName("category"); item1.setCategory(category); category.setExampleItem(item1); Session s = openSession(); Transaction tx = s.beginTransaction(); s.persist(item1); tx.commit(); s.close(); // get another representation of item1 s = openSession(); tx = s.beginTransaction(); Item item1_1 = (Item) s.get(Item.class, item1.getId()); tx.commit(); s.close(); // change many-to-one in top level to be a new (transient) Category categoryNewer = new Category(); categoryNewer.setName("newer category"); item1.setCategory(categoryNewer); // put the other representation in categoryNewer categoryNewer.setExampleItem(item1_1); s = openSession(); tx = s.beginTransaction(); Item item1Merged = (Item) s.merge(item1); // the many-to-one from the top level item will win. assertEquals(categoryNewer.getName(), item1Merged.getCategory().getName()); assertSame(item1Merged, item1Merged.getCategory().getExampleItem()); tx.commit(); s.close(); s = openSession(); tx = s.beginTransaction(); item1 = (Item) s.get(Item.class, item1.getId()); assertEquals(categoryNewer.getName(), item1.getCategory().getName()); assertSame(item1, item1.getCategory().getExampleItem()); // make sure original category is still there Category categoryQueried = (Category) s.createQuery("from Category c where c.name='category'").uniqueResult(); assertNotNull(categoryQueried); // make sure original category has the same item. assertSame(item1, categoryQueried.getExampleItem()); // set exampleItem to null to avoid constraint violation on cleanup. categoryQueried.setExampleItem(null); tx.commit(); s.close(); cleanup(); }
@Override public void submitStockUpdate(StockItem addedItem) { Session session = HibernateUtil.currentSession(); Transaction transaction = session.beginTransaction(); session.merge(addedItem); transaction.commit(); log.debug("Stock updated in db - adding stock"); }
public void updateUser(User user) { try { session.beginTransaction(); session.merge(user); session.getTransaction().commit(); } catch (Exception e) { Logger.getLogger(UserManager.class.getName()).log(Level.INFO, "Couldn't update user", e); } }
public void execute() { Session s = getFactory().openSession(); s.beginTransaction(); Entity entity = s.get(Entity.class, entityId); Assert.assertFalse(Hibernate.isPropertyInitialized(entity, "description")); EnhancerTestUtils.checkDirtyTracking(entity); Assert.assertEquals("desc", entity.getDescription()); Assert.assertTrue(Hibernate.isPropertyInitialized(entity, "description")); s.getTransaction().commit(); s.close(); s = getFactory().openSession(); s.beginTransaction(); entity.setDescription("desc1"); s.update(entity); // Assert.assertFalse( Hibernate.isPropertyInitialized( entity, "description" ) ); EnhancerTestUtils.checkDirtyTracking(entity, "description"); Assert.assertEquals("desc1", entity.getDescription()); Assert.assertTrue(Hibernate.isPropertyInitialized(entity, "description")); s.getTransaction().commit(); s.close(); s = getFactory().openSession(); s.beginTransaction(); entity = s.get(Entity.class, entityId); Assert.assertEquals("desc1", entity.getDescription()); s.getTransaction().commit(); s.close(); s = getFactory().openSession(); s.beginTransaction(); entity.setDescription("desc2"); entity = (Entity) s.merge(entity); // Assert.assertFalse( Hibernate.isPropertyInitialized( entity, "description" ) ); EnhancerTestUtils.checkDirtyTracking(entity, "description"); Assert.assertEquals("desc2", entity.getDescription()); Assert.assertTrue(Hibernate.isPropertyInitialized(entity, "description")); s.getTransaction().commit(); s.close(); s = getFactory().openSession(); s.beginTransaction(); entity = s.get(Entity.class, entityId); Assert.assertEquals("desc2", entity.getDescription()); s.getTransaction().commit(); s.close(); }
public void update(GOI goi) { Session session = HibernateUtil.getSession(); System.out.println("goi.id do aktualizacji: " + goi.getId()); System.out.println("goi.usluga.nazwa do aktualizacji: " + goi.getUsluga().getNazwa()); session.merge(goi); session.flush(); }
/* * updates customer * * @param customer to update */ public void update(Student customer) { SessionFactory sessionFactory = HibernateSession.getSessionFactory(); Session session = sessionFactory.openSession(); session.beginTransaction(); session.merge(customer); session.getTransaction().commit(); session.close(); }
@Transactional(value = "transactionManager") public void guardar(List<DatosCancion> datos) { Session session = sessionFactory.getCurrentSession(); for (DatosCancion dato : datos) { session.merge(dato); } }
@Test public void testBackRef() { Session s = openSession(); Transaction t = s.beginTransaction(); Parent p = new Parent("Marc"); Parent p2 = new Parent("Nathalie"); Child c = new Child("Elvira"); Child c2 = new Child("Blase"); p.getChildren().add(c); p.getChildren().add(c2); s.persist(p); s.persist(p2); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); c = (Child) s.get(Child.class, "Elvira"); c.setAge(2); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); p = (Parent) s.get(Parent.class, "Marc"); c = (Child) s.get(Child.class, "Elvira"); c.setAge(18); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); p = (Parent) s.get(Parent.class, "Marc"); p2 = (Parent) s.get(Parent.class, "Nathalie"); c = (Child) s.get(Child.class, "Elvira"); assertEquals(p.getChildren().indexOf(c), 0); p.getChildren().remove(c); p2.getChildren().add(c); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); Parent p3 = new Parent("Marion"); p3.getChildren().add(new Child("Gavin")); s.merge(p3); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); s.createQuery("delete from Child").executeUpdate(); s.createQuery("delete from Parent").executeUpdate(); t.commit(); s.close(); }
@Test @FailureExpected(jiraKey = "HHH-9239") public void testNestedUnidirOneToManyBackrefWithRemovedElement() { Item item1 = new Item(); item1.setName("item1 name"); SubItem subItem1 = new SubItem(); subItem1.setName("subItem1 name"); item1.getSubItemsBackref().add(subItem1); SubItem subItem2 = new SubItem(); subItem2.setName("subItem2 name"); item1.getSubItemsBackref().add(subItem2); Session s = openSession(); Transaction tx = s.beginTransaction(); s.persist(item1); tx.commit(); s.close(); // get another representation of item1 s = openSession(); tx = s.beginTransaction(); Item item1_1 = (Item) s.get(Item.class, item1.getId()); Hibernate.initialize(item1_1.getSubItemsBackref()); tx.commit(); s.close(); // remove subItem1 from the nested Item item1_1.getSubItemsBackref().remove(subItem1); Category category = new Category(); category.setName("category"); item1.setCategory(category); category.setExampleItem(item1_1); s = openSession(); tx = s.beginTransaction(); Item item1Merged = (Item) s.merge(item1); // the element should have been removed assertEquals(1, item1Merged.getSubItemsBackref().size()); assertTrue(item1Merged.getSubItemsBackref().contains(subItem2)); tx.commit(); s.close(); s = openSession(); tx = s.beginTransaction(); item1 = (Item) s.get(Item.class, item1.getId()); assertEquals(1, item1.getSubItemsBackref().size()); assertTrue(item1.getSubItemsBackref().contains(subItem2)); // because cascade includes "delete-orphan" the removed SubItem should have been deleted. subItem1 = (SubItem) s.get(SubItem.class, subItem1.getId()); assertNull(subItem1); tx.commit(); s.close(); cleanup(); }
public void update(Sample sample, Date transactionDate, Session session) throws DAOException { try { if (sample.getModifiedDate() == null) { sample.setModifiedDate(transactionDate); } session.merge(sample); } catch (Exception ex) { throw new DAOException(ex); } }
@Override public int update(EmployeeEntity employeeEntity) { Session session = sessionFactory.openSession(); AuditEntity auditEntity = new AuditEntity(); auditEntity.setUpdatedBy("System"); auditEntity.setUpdatedDate(new Date()); employeeEntity.setAuditEntity(auditEntity); session.merge(employeeEntity); return employeeEntity.getId(); }