Skip to content

Commit

Permalink
UUID not working as GeneratedValue Id with merge - fix (#2087)
Browse files Browse the repository at this point in the history
Signed-off-by: Radek Felcman <radek.felcman@oracle.com>
  • Loading branch information
rfelcman committed Mar 6, 2024
1 parent 4c3c2b4 commit 592506c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2022, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand Down Expand Up @@ -40,6 +40,9 @@ public UUIDConverter() {
*/
@Override
public Object convertObjectValueToDataValue(Object uuidValue, Session session) {
if (uuidValue == null) {
return null;
}
if (uuidValue instanceof UUID) {
return uuidValue.toString();
}
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2022, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand Down Expand Up @@ -32,7 +32,8 @@
@Ignore
public abstract class TestUuidCommon {

private static final String NAME_UUID = "Persist UUID UUID 1";
private static final String NAME_UUID1 = "Persist UUID UUID 1";
private static final String NAME_UUID2 = "Merge UUID UUID 2";

abstract EntityManagerFactory getEmf();

Expand All @@ -41,10 +42,11 @@ public abstract class TestUuidCommon {
*/
@Test
public void testUuid() {
UUID uuid = persistUUIDUUIDEntity();
UUID uuid1 = persistUUIDUUIDEntity();
mergeUUIDUUIDEntity();
queryAllUUIDUUIDEntity();
queryWithParameterUUIDUUIDEntity(uuid);
findUUIDUUIDEntityTest(uuid);
queryWithParameterUUIDUUIDEntity(uuid1);
findUUIDUUIDEntityTest(uuid1);
}

private UUID persistUUIDUUIDEntity() {
Expand All @@ -53,7 +55,7 @@ private UUID persistUUIDUUIDEntity() {
try {
em.getTransaction().begin();
UUIDUUIDEntity entity = new UUIDUUIDEntity();
entity.setName(NAME_UUID);
entity.setName(NAME_UUID1);
em.persist(entity);
uuidResult = entity.getId();
assertNotNull(uuidResult);
Expand All @@ -71,6 +73,30 @@ private UUID persistUUIDUUIDEntity() {
return uuidResult;
}

private UUID mergeUUIDUUIDEntity() {
EntityManager em = getEmf().createEntityManager();
UUID uuidResult = null;
try {
em.getTransaction().begin();
UUIDUUIDEntity entity = new UUIDUUIDEntity();
entity.setName(NAME_UUID2);
entity = em.merge(entity);
uuidResult = entity.getId();
assertNotNull(uuidResult);
em.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException();
}
finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
em.close();
}
return uuidResult;
}

public void queryAllUUIDUUIDEntity() {
EntityManager em = getEmf().createEntityManager();
try {
Expand Down Expand Up @@ -102,7 +128,7 @@ public void queryWithParameterUUIDUUIDEntity(UUID id) {
assertNotNull(entity);
assertNotNull(entity.getId());
assertEquals(id, entity.getId());
assertEquals(NAME_UUID, entity.getName());
assertEquals(NAME_UUID1, entity.getName());
}
} catch (Exception e) {
e.printStackTrace();
Expand All @@ -122,7 +148,7 @@ public void findUUIDUUIDEntityTest(UUID id) {
UUIDUUIDEntity entity = em.find(UUIDUUIDEntity.class, id);
assertNotNull(entity);
assertEquals(id, entity.getId());
assertEquals(NAME_UUID, entity.getName());
assertEquals(NAME_UUID1, entity.getName());
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException();
Expand Down

0 comments on commit 592506c

Please sign in to comment.