Skip to content

Commit

Permalink
Return original object if target and original are the same when merging
Browse files Browse the repository at this point in the history
  • Loading branch information
SimoneGiusso authored and vladmihalcea committed Dec 18, 2023
1 parent 4968e53 commit 64f5f47
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,11 @@ public T assemble(Serializable cached, Object owner) {

@Override
public T replace(T detached, T managed, Object owner) {
return deepCopy(detached);
if (!isMutable() || (managed != null && equals(detached, managed))) {
return detached;
}

return deepCopy(detached);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package io.hypersistence.utils.hibernate.type.json;

import io.hypersistence.utils.hibernate.type.model.Event;
import io.hypersistence.utils.hibernate.type.model.Location;
import io.hypersistence.utils.hibernate.type.model.Participant;
import io.hypersistence.utils.hibernate.util.AbstractPostgreSQLIntegrationTest;
import org.junit.Test;

import static java.lang.System.identityHashCode;
import static org.junit.Assert.assertEquals;

/**
* @author Simone Giusso
*/
public class PostgreSQLJsonTypeMergeTest extends AbstractPostgreSQLIntegrationTest {

@Override
protected Class<?>[] entities() {
return new Class<?>[]{
Event.class,
Participant.class
};
}

@Test
public void test() {
doInJPA(entityManager -> {
Location location = new Location();
location.setCountry("Romania");
location.setCity("Cluj-Napoca");

Event event = new Event();
event.setId(1L);
event.setLocation(location);
entityManager.persist(event);

event = entityManager.merge(event);

assertThatMergeReturnsTheOriginalJsonTypeObject(event.getLocation(), location);
});
}

private void assertThatMergeReturnsTheOriginalJsonTypeObject(Location target, Location original) {
assertEquals(identityHashCode(original), identityHashCode(target));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ public T assemble(Serializable cached, Object owner) {

@Override
public T replace(T detached, T managed, Object owner) {
if (!isMutable() || (managed != null && equals(detached, managed))) {
return detached;
}

return deepCopy(detached);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package io.hypersistence.utils.hibernate.type.json;

import io.hypersistence.utils.hibernate.type.model.Event;
import io.hypersistence.utils.hibernate.type.model.Location;
import io.hypersistence.utils.hibernate.type.model.Participant;
import io.hypersistence.utils.hibernate.util.AbstractPostgreSQLIntegrationTest;
import org.junit.Test;

import static java.lang.System.identityHashCode;
import static org.junit.Assert.assertEquals;

/**
* @author Simone Giusso
*/
public class PostgreSQLJsonTypeMergeTest extends AbstractPostgreSQLIntegrationTest {

@Override
protected Class<?>[] entities() {
return new Class<?>[]{
Event.class,
Participant.class
};
}

@Test
public void test() {
doInJPA(entityManager -> {
Location location = new Location();
location.setCountry("Romania");
location.setCity("Cluj-Napoca");

Event event = new Event();
event.setId(1L);
event.setLocation(location);
entityManager.persist(event);

event = entityManager.merge(event);

assertThatMergeReturnsTheOriginalJsonTypeObject(event.getLocation(), location);
});
}

private void assertThatMergeReturnsTheOriginalJsonTypeObject(Location target, Location original) {
assertEquals(identityHashCode(original), identityHashCode(target));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ public T assemble(Serializable cached, Object owner) {

@Override
public T replace(T detached, T managed, Object owner) {
if (!isMutable() || (managed != null && equals(detached, managed))) {
return detached;
}

return deepCopy(detached);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package io.hypersistence.utils.hibernate.type.json;

import io.hypersistence.utils.hibernate.type.model.Event;
import io.hypersistence.utils.hibernate.type.model.Location;
import io.hypersistence.utils.hibernate.type.model.Participant;
import io.hypersistence.utils.hibernate.util.AbstractPostgreSQLIntegrationTest;
import org.junit.Test;

import static java.lang.System.identityHashCode;
import static org.junit.Assert.assertEquals;

/**
* @author Simone Giusso
*/
public class PostgreSQLJsonTypeMergeTest extends AbstractPostgreSQLIntegrationTest {

@Override
protected Class<?>[] entities() {
return new Class<?>[]{
Event.class,
Participant.class
};
}

@Test
public void test() {
doInJPA(entityManager -> {
Location location = new Location();
location.setCountry("Romania");
location.setCity("Cluj-Napoca");

Event event = new Event();
event.setId(1L);
event.setLocation(location);
entityManager.persist(event);

event = entityManager.merge(event);

assertThatMergeReturnsTheOriginalJsonTypeObject(event.getLocation(), location);
});
}

private void assertThatMergeReturnsTheOriginalJsonTypeObject(Location target, Location original) {
assertEquals(identityHashCode(original), identityHashCode(target));
}

}

0 comments on commit 64f5f47

Please sign in to comment.