|
| 1 | +/* |
| 2 | + * Copyright (c) 2023 IBM Corporation. All rights reserved. |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Eclipse Public License v. 2.0 which is available at |
| 6 | + * http://www.eclipse.org/legal/epl-2.0, |
| 7 | + * or the Eclipse Distribution License v. 1.0 which is available at |
| 8 | + * http://www.eclipse.org/org/documents/edl-v10.php. |
| 9 | + * |
| 10 | + * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause |
| 11 | + */ |
| 12 | + |
| 13 | +// Contributors: |
| 14 | +package org.eclipse.persistence.jpa.test.collection; |
| 15 | + |
| 16 | +import jakarta.persistence.EntityManager; |
| 17 | +import jakarta.persistence.EntityManagerFactory; |
| 18 | +import jakarta.persistence.Query; |
| 19 | + |
| 20 | +import java.util.Set; |
| 21 | + |
| 22 | +import org.eclipse.persistence.jpa.test.framework.DDLGen; |
| 23 | +import org.eclipse.persistence.jpa.test.framework.Emf; |
| 24 | +import org.eclipse.persistence.jpa.test.framework.EmfRunner; |
| 25 | +import org.eclipse.persistence.jpa.test.framework.Property; |
| 26 | +import org.eclipse.persistence.jpa.test.collection.model.CityEntity; |
| 27 | +import org.junit.Test; |
| 28 | +import org.junit.runner.RunWith; |
| 29 | + |
| 30 | +@RunWith(EmfRunner.class) |
| 31 | +public class TestBasicCollection { |
| 32 | + |
| 33 | + @Emf(createTables = DDLGen.DROP_CREATE, |
| 34 | + classes = { CityEntity.class }, |
| 35 | + properties = { @Property(name = "eclipselink.logging.level", value = "ALL"), |
| 36 | + @Property(name = "eclipselink.logging.level.sql", value = "FINE"), |
| 37 | + @Property(name = "eclipselink.logging.parameters", value = "true")}) |
| 38 | + private EntityManagerFactory emf; |
| 39 | + |
| 40 | + @Test |
| 41 | + public void testUpdateBasicCollectionWithQuery() { |
| 42 | + EntityManager em = emf.createEntityManager(); |
| 43 | + try { |
| 44 | + em.getTransaction().begin(); |
| 45 | + em.persist(new CityEntity("Minneapolis", Set.of(608))); |
| 46 | + em.getTransaction().commit(); |
| 47 | + } finally { |
| 48 | + if (em.getTransaction().isActive()) { |
| 49 | + em.getTransaction().rollback(); |
| 50 | + } |
| 51 | + if(em.isOpen()) { |
| 52 | + em.close(); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + em = emf.createEntityManager(); |
| 57 | + try { |
| 58 | + em.getTransaction().begin(); |
| 59 | + Query q = em.createQuery("UPDATE City o SET o.areaCodes=?1 WHERE o.name=?2"); |
| 60 | + q.setParameter(1, Set.of(563)); |
| 61 | + q.setParameter(2, "Minneapolis"); |
| 62 | + q.executeUpdate(); |
| 63 | + em.getTransaction().commit(); |
| 64 | + } finally { |
| 65 | + if (em.getTransaction().isActive()) { |
| 66 | + em.getTransaction().rollback(); |
| 67 | + } |
| 68 | + if(em.isOpen()) { |
| 69 | + em.close(); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + em = emf.createEntityManager(); |
| 74 | + try { |
| 75 | + em.getTransaction().begin(); |
| 76 | + CityEntity find2 = em.find(CityEntity.class, "Minneapolis"); |
| 77 | + em.getTransaction().commit(); |
| 78 | + } finally { |
| 79 | + if (em.getTransaction().isActive()) { |
| 80 | + em.getTransaction().rollback(); |
| 81 | + } |
| 82 | + if(em.isOpen()) { |
| 83 | + em.close(); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments