-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HHH-14329 test case showing that DirtinessTracker usage for enhanced …
…entities doesn't respect mutable types
- Loading branch information
Showing
3 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
...ore/src/test/java/org/hibernate/test/bytecode/enhancement/mutable/MapStringConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
.../java/org/hibernate/test/bytecode/enhancement/mutable/MutableTypeEnhancementTestCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" ) ); | ||
} ); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/mutable/TestEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |