|
| 1 | +package com.fasterxml.jackson.datatype.hibernate5.failing; |
| 2 | + |
| 3 | +import javax.persistence.Entity; |
| 4 | +import javax.persistence.Id; |
| 5 | + |
| 6 | +import org.mockito.stubbing.Answer; |
| 7 | +import org.mockito.invocation.InvocationOnMock; |
| 8 | + |
| 9 | +import static org.mockito.Mockito.mock; |
| 10 | +import static org.mockito.Mockito.when; |
| 11 | + |
| 12 | +import org.hibernate.proxy.HibernateProxy; |
| 13 | +import org.hibernate.proxy.LazyInitializer; |
| 14 | + |
| 15 | +import com.fasterxml.jackson.annotation.JsonIdentityInfo; |
| 16 | +import com.fasterxml.jackson.annotation.ObjectIdGenerators; |
| 17 | + |
| 18 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 19 | +import com.fasterxml.jackson.datatype.hibernate5.BaseTest; |
| 20 | + |
| 21 | +/** |
| 22 | + * Problem with handling of Object Id, [datatype-hibernate#41] |
| 23 | + */ |
| 24 | +public class Issue41Test extends BaseTest |
| 25 | +{ |
| 26 | + @Entity |
| 27 | + @JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property = "id") |
| 28 | + public static class SomeJPAEntity { |
| 29 | + @Id |
| 30 | + public String id; |
| 31 | + |
| 32 | + // @ManyToOne not needed because we mock this |
| 33 | + public SomeJPAEntity owner; |
| 34 | + } |
| 35 | + |
| 36 | + public static class EntityHibernateProxy extends SomeJPAEntity implements HibernateProxy |
| 37 | + { |
| 38 | + private static final long serialVersionUID = 1L; |
| 39 | + |
| 40 | + private LazyInitializer lazyInitializer; |
| 41 | + |
| 42 | + public EntityHibernateProxy() { |
| 43 | + this(true); |
| 44 | + } |
| 45 | + |
| 46 | + public EntityHibernateProxy(final boolean uninitialized) { |
| 47 | + lazyInitializer = mock(LazyInitializer.class); |
| 48 | + when(lazyInitializer.isUninitialized()).thenReturn(uninitialized); |
| 49 | + final String currId = "d0024148-3040-4fee-a78a-e94fd2449ac6"; |
| 50 | + when(lazyInitializer.getIdentifier()).thenReturn(currId); |
| 51 | + when(lazyInitializer.getEntityName()).thenReturn("someJPAEntity"); |
| 52 | + |
| 53 | + when(lazyInitializer.getImplementation()).thenAnswer(new Answer<Object>() { |
| 54 | + |
| 55 | + @Override |
| 56 | + public Object answer(InvocationOnMock invocation) throws Throwable { |
| 57 | + SomeJPAEntity entity = new SomeJPAEntity(); |
| 58 | + entity.id = id; |
| 59 | + entity.owner = new EntityHibernateProxy(false); |
| 60 | + return entity; |
| 61 | + }}); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public Object writeReplace() { |
| 66 | + return this; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public LazyInitializer getHibernateLazyInitializer() { |
| 71 | + return lazyInitializer; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + public void testIssue41() throws Exception |
| 76 | + { |
| 77 | + EntityHibernateProxy entity = new EntityHibernateProxy(false); |
| 78 | + entity.id = "3cf7a573-f528-440c-83b9-873d7594b373"; |
| 79 | + entity.owner = entity; |
| 80 | + |
| 81 | + ObjectMapper mapper = mapperWithModule(true); |
| 82 | + String json = mapper.writeValueAsString(entity); |
| 83 | + |
| 84 | + // will throw an exception long before here so this should suffice: |
| 85 | + assertNotNull(json); |
| 86 | + } |
| 87 | +} |
0 commit comments