Skip to content

Commit

Permalink
HHH-14329 test case showing that DirtinessTracker usage for enhanced …
Browse files Browse the repository at this point in the history
…entities doesn't respect mutable types
  • Loading branch information
beikov authored and Sanne committed Nov 17, 2020
1 parent c444d5f commit 5ea0d92
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/

package org.hibernate.test.bytecode.enhancement.mutable;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
import javax.persistence.AttributeConverter;

public class MapStringConverter implements AttributeConverter<Map<String, String>, String> {

@Override
public String convertToDatabaseColumn(Map<String, String> attribute) {
if ( attribute == null ) {
return null;
}
return attribute.entrySet().stream()
.map( entry -> entry.getKey() + ";" + entry.getValue() )
.collect( Collectors.joining( ";" ) );
}

@Override
public Map<String, String> convertToEntityAttribute(String dbData) {
if ( dbData == null ) {
return null;
}
String[] strings = dbData.split( ";" );
Map<String, String> map = new HashMap<>();
for ( int i = 0; i < strings.length; i += 2 ) {
map.put( strings[i], strings[i + 1] );
}
return map;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/

package org.hibernate.test.bytecode.enhancement.mutable;

import java.util.Date;

import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(BytecodeEnhancerRunner.class)
public class MutableTypeEnhancementTestCase extends BaseCoreFunctionalTestCase {

@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[] { TestEntity.class };
}

@Test
@TestForIssue(jiraKey = "HHH-14329")
public void testMutateMutableTypeObject() throws Exception {
inTransaction( entityManager -> {
TestEntity e = new TestEntity();
e.setId( 1L );
e.setDate( new Date() );
e.getTexts().put( "a", "abc" );
entityManager.persist( e );
} );

inTransaction( entityManager -> {
TestEntity e = entityManager.find( TestEntity.class, 1L );
e.getDate().setTime( 0 );
e.getTexts().put( "a", "def" );
} );

inTransaction( entityManager -> {
TestEntity e = entityManager.find( TestEntity.class, 1L );
Assert.assertEquals( 0L, e.getDate().getTime() );
Assert.assertEquals( "def", e.getTexts().get( "a" ) );
} );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.test.bytecode.enhancement.mutable;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
public class TestEntity {
@Id
private Long id;

@Temporal(TemporalType.TIMESTAMP)
private Date date;

@Basic
@Column(name = "TEXTS")
@Convert(converter = MapStringConverter.class)
private Map<String, String> texts = new HashMap<>();

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public Date getDate() {
return date;
}

public void setDate(Date date) {
this.date = date;
}

public Map<String, String> getTexts() {
return texts;
}

public void setTexts(Map<String, String> texts) {
this.texts = texts;
}
}

0 comments on commit 5ea0d92

Please sign in to comment.