public void edit(Person person) { logger.debug("Editing existing person, which is a detached object"); // Retrieve session from Hibernate Session session = sessionFactory.getCurrentSession(); // Retrieve existing person via id, then copy everything from detached person // to attached one, and return attached one Person existingPerson = (Person) session.merge(person); }
@Override @SuppressWarnings("unchecked") public <T> T merge(final T entity) throws DataAccessException { return nonNull(executeWithNativeSession(session -> { checkWriteOperationAllowed(session); return (T) session.merge(entity); })); }
@Override @SuppressWarnings("unchecked") public <T> T merge(final String entityName, final T entity) throws DataAccessException { return nonNull(executeWithNativeSession(session -> { checkWriteOperationAllowed(session); return (T) session.merge(entityName, entity); })); }
public Long save(User user) { user = (User) getSession().merge(user); return user.getId(); }
@Override @SuppressWarnings("unchecked") public <T> T merge(final T entity) throws DataAccessException { return nonNull(executeWithNativeSession(session -> { checkWriteOperationAllowed(session); return (T) session.merge(entity); })); }
@Override @SuppressWarnings("unchecked") public <T> T merge(final String entityName, final T entity) throws DataAccessException { return nonNull(executeWithNativeSession(session -> { checkWriteOperationAllowed(session); return (T) session.merge(entityName, entity); })); }
@Test(expected = ConstraintViolationException.class) public void testOneToManyNullCollection() { Parent parent = new Parent(); parent.id = 1L; Session s = openSession(); Transaction t = s.beginTransaction(); parent = (Parent) s.merge( parent ); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); s.delete( parent ); t.commit(); s.close(); }
@Test(expected = ConstraintViolationException.class) public void testOneToManyNullCollection() { Parent parent = new Parent(); Child child = new Child(); child.setParent( parent ); Session s = openSession(); Transaction t = s.beginTransaction(); parent = (Parent) s.merge( parent ); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); s.delete( parent ); t.commit(); s.close(); }
@Test @TestForIssue(jiraKey = "HHH-3868") public void testTransientMergeComponentParent() { Session s = openSession(); Transaction tx = s.beginTransaction(); Book b = new Book(); b.setIsbn( UUID.randomUUID().toString() ); b.setSummary( new Summary() ); b = (Book) s.merge( b ); tx.commit(); s.close(); }
@Test public void testUpdateWithNullRelation() { Session session = openSession(); session.beginTransaction(); User user = new User(); user.setName( "User1" ); session.persist( user ); session.getTransaction().commit(); session.close(); session = openSession(); session.beginTransaction(); user.setName( "UserUpdate" ); session.merge( user ); session.getTransaction().commit(); session.close(); } }
@Test public void testMultiPathMergeModifiedDetached() throws Exception { // persist a simple A in the database Session s = openSession(); s.beginTransaction(); A a = new A(); a.setData( "Anna" ); s.save( a ); s.getTransaction().commit(); s.close(); // modify detached entity modifyEntity( a ); s = openSession(); s.beginTransaction(); a = (A) s.merge( a ); s.getTransaction().commit(); s.close(); verifyModifications( a.getId() ); }
@Test public void testField() { doInHibernate( this::sessionFactory, s -> { ItemField input = new ItemField(); input.name = "F"; input.parameters = new HashMap<>(); input.parameters.put( "aaa", "AAA" ); input.parameters.put( "bbb", "BBB" ); s.persist( input ); } ); doInHibernate( this::sessionFactory, s -> { // A parameters map is created with the class and is being compared to the persistent map (by the generated code) -- it shouldn't item = s.find( ItemField.class, "F" ); } ); doInHibernate( this::sessionFactory, s -> { mergedItem = (Item) s.merge( item ); } ); Assert.assertEquals( 2, mergedItem.getParameters().size() ); }
@Test public void testMerge() { Session s = openSession(); s.getTransaction().begin(); b = (B) s.merge( b ); c = b.getC(); d = b.getD(); e = d.getE(); f = e.getF(); g = f.getG(); s.getTransaction().commit(); s.close(); check(); }
@Test public void testMerge() { Session s = openSession(); s.getTransaction().begin(); b = ( EntityB ) s.merge( b ); c = b.getC(); d = b.getD(); e = d.getE(); f = e.getF(); g = f.getG(); s.getTransaction().commit(); s.close(); check(); }
@Test public void testMerge() { Session s = openSession(); s.getTransaction().begin(); b = ( B ) s.merge( b ); c = b.getC(); d = b.getD(); e = d.getE(); f = e.getF(); g = f.getG(); s.getTransaction().commit(); s.close(); check(); }
@Test public void testMergeDetached() { final Heading heading = doInHibernate( this::sessionFactory, session -> { Heading entity = new Heading(); entity.name = "new"; entity.setGrouping( new Grouping() ); Thing thing = new Thing(); entity.getGrouping().getThings().add( thing ); session.save( thing ); session.save( entity ); return entity; } ); doInHibernate( this::sessionFactory, session -> { heading.name = "updated"; Heading headingMerged = (Heading) session.merge( heading ); assertNotSame( heading, headingMerged ); assertNotSame( heading.grouping, headingMerged.grouping ); assertNotSame( heading.grouping.things, headingMerged.grouping.things ); } ); }
@Test public void testMergeDetachedOrphanRemoval() { final Root entity = doInHibernate( this::sessionFactory, session -> { Root root = new Root(); root.setName( "new" ); session.save( root ); return root; } ); doInHibernate( this::sessionFactory, session -> { entity.setName( "updated" ); Root entityMerged = (Root) session.merge( entity ); assertNotSame( entity, entityMerged ); assertNotSame( entity.getLeaves(), entityMerged.getLeaves() ); } ); }
@Test public void testMergeDetached() { final Heading heading = doInHibernate( this::sessionFactory, session -> { Heading entity = new Heading(); entity.name = "new"; entity.setGrouping( new Grouping() ); entity.getGrouping().getThings().add( new Thing() ); session.save( entity ); return entity; } ); doInHibernate( this::sessionFactory, session -> { heading.name = "updated"; Heading headingMerged = (Heading) session.merge( heading ); assertNotSame( heading, headingMerged ); assertNotSame( heading.grouping, headingMerged.grouping ); assertNotSame( heading.grouping.things, headingMerged.grouping.things ); } ); }
@Test public void testMergeDetachedNonEmptyCollection() { final Root entity = doInHibernate( this::sessionFactory, session -> { Root root = new Root(); root.setName( "new" ); Leaf leaf = new Leaf(); leaf.setRoot( root ); root.getLeaves().add( leaf ); session.save( root ); return root; } ); doInHibernate( this::sessionFactory, session -> { entity.setName( "updated" ); Root entityMerged = (Root) session.merge( entity ); assertNotSame( entity, entityMerged ); assertNotSame( entity.getLeaves(), entityMerged.getLeaves() ); } ); } }
@Test public void test() { doInHibernate( this::sessionFactory, session -> { Manager manager = new Manager(); manager.setName("John Doe"); manager.setResume(new byte[] {1, 2, 3}); Company company = new Company(); company.setName("Company"); company.setManager(manager); Company _company = (Company) session.merge( company); assertEquals( company.getName(), _company.getName() ); assertArrayEquals( company.getManager().getResume(), _company.getManager().getResume() ); } ); }