|
Hibernate example source code file (MergeTest.java)
The Hibernate MergeTest.java source code/* * Hibernate, Relational Persistence for Idiomatic Java * * Copyright (c) 2011, Red Hat Inc. or third-party contributors as * indicated by the @author tags or express copyright attribution * statements applied by the authors. All third-party contributions are * distributed under license by Red Hat Inc. * * This copyrighted material is made available to anyone wishing to use, modify, * copy, or redistribute it subject to the terms and conditions of the GNU * Lesser General Public License, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this distribution; if not, write to: * Free Software Foundation, Inc. * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ package org.hibernate.ejb.test.emops; import javax.persistence.EntityManager; import java.util.ArrayList; import java.util.List; import org.hibernate.ejb.test.BaseEntityManagerFunctionalTestCase; import org.junit.Test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; /** * @author Emmanuel Bernard */ public class MergeTest extends BaseEntityManagerFunctionalTestCase { @Test public void testMergeWithIndexColumn() { Race race = new Race(); race.competitors.add( new Competitor( "Name" ) ); race.competitors.add( new Competitor() ); race.competitors.add( new Competitor() ); EntityManager em = getOrCreateEntityManager(); em.getTransaction().begin(); em.persist( race ); em.flush(); em.clear(); race.competitors.add( new Competitor() ); race.competitors.remove( 2 ); race.competitors.remove( 1 ); race.competitors.get( 0 ).setName( "Name2" ); race = em.merge( race ); em.flush(); em.clear(); race = em.find( Race.class, race.id ); assertEquals( 2, race.competitors.size() ); assertEquals( "Name2", race.competitors.get( 0 ).getName() ); em.getTransaction().rollback(); em.close(); } @Test public void testMergeManyToMany() { Competition competition = new Competition(); competition.getCompetitors().add( new Competitor( "Name" ) ); competition.getCompetitors().add( new Competitor() ); competition.getCompetitors().add( new Competitor() ); EntityManager em = getOrCreateEntityManager(); em.getTransaction().begin(); em.persist( competition ); em.flush(); em.clear(); competition.getCompetitors().add( new Competitor() ); competition.getCompetitors().remove( 2 ); competition.getCompetitors().remove( 1 ); competition.getCompetitors().get( 0 ).setName( "Name2" ); competition = em.merge( competition ); em.flush(); em.clear(); competition = em.find( Competition.class, competition.getId() ); assertEquals( 2, competition.getCompetitors().size() ); // we cannot assume that the order in the list is maintained - HHH-4516 String changedCompetitorName; if ( competition.getCompetitors().get( 0 ).getName() != null ) { changedCompetitorName = competition.getCompetitors().get( 0 ).getName(); } else { changedCompetitorName = competition.getCompetitors().get( 1 ).getName(); } assertEquals( "Name2", changedCompetitorName ); em.getTransaction().rollback(); em.close(); } @Test public void testMergeManyToManyWithDeference() { Competition competition = new Competition(); competition.getCompetitors().add( new Competitor( "Name" ) ); competition.getCompetitors().add( new Competitor() ); competition.getCompetitors().add( new Competitor() ); EntityManager em = getOrCreateEntityManager(); em.getTransaction().begin(); em.persist( competition ); em.flush(); em.clear(); List<Competitor> newComp = new ArrayList Other Hibernate examples (source code examples)Here is a short list of links related to this Hibernate MergeTest.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.