-
Notifications
You must be signed in to change notification settings - Fork 368
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid the unnecessary UPDATE for JsonNode entity mappings #348
- Loading branch information
1 parent
89cc477
commit bfab823
Showing
19 changed files
with
615 additions
and
80 deletions.
There are no files selected for viewing
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
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
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
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
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
123 changes: 123 additions & 0 deletions
123
...ypes-43/src/test/java/com/vladmihalcea/hibernate/type/json/MySQLJsonNodePropertyTest.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,123 @@ | ||
package com.vladmihalcea.hibernate.type.json; | ||
|
||
import com.fasterxml.jackson.databind.DeserializationFeature; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.vladmihalcea.hibernate.type.util.AbstractMySQLIntegrationTest; | ||
import com.vladmihalcea.hibernate.type.util.transaction.JPATransactionFunction; | ||
import net.ttddyy.dsproxy.QueryCount; | ||
import net.ttddyy.dsproxy.QueryCountHolder; | ||
import org.hibernate.Session; | ||
import org.hibernate.annotations.NaturalId; | ||
import org.hibernate.annotations.Type; | ||
import org.hibernate.annotations.TypeDef; | ||
import org.junit.Test; | ||
|
||
import javax.persistence.*; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
/** | ||
* @author Victor Noël | ||
*/ | ||
public class MySQLJsonNodePropertyTest extends AbstractMySQLIntegrationTest { | ||
|
||
private final ObjectMapper mapper = newMapper(); | ||
|
||
@Override | ||
protected Class<?>[] entities() { | ||
return new Class<?>[]{ | ||
Book.class | ||
}; | ||
} | ||
|
||
|
||
@Override | ||
protected void afterInit() { | ||
doInJPA(new JPATransactionFunction<Void>() { | ||
|
||
@Override | ||
public Void apply(EntityManager entityManager) { | ||
try { | ||
entityManager.persist( | ||
new Book() | ||
.setIsbn("978-9730228236") | ||
.setProperties(mapper.readTree("{\"field\": 0.05}")) | ||
); | ||
} catch (Exception e) { | ||
throw new IllegalStateException(e); | ||
} | ||
|
||
return null; | ||
} | ||
}); | ||
} | ||
|
||
@Test | ||
public void test() { | ||
QueryCountHolder.clear(); | ||
|
||
doInJPA(new JPATransactionFunction<Void>() { | ||
|
||
@Override | ||
public Void apply(EntityManager entityManager) { | ||
Book book = (Book) entityManager.unwrap(Session.class) | ||
.bySimpleNaturalId(Book.class) | ||
.load("978-9730228236"); | ||
assertEquals(0.05, book.getProperties().get("field").asDouble(), 0.0); | ||
|
||
return null; | ||
} | ||
}); | ||
|
||
QueryCount queryCount = QueryCountHolder.getGrandTotal(); | ||
assertEquals(0, queryCount.getUpdate()); | ||
} | ||
|
||
public static class MyJsonType extends JsonType { | ||
public MyJsonType() { | ||
super(newMapper()); | ||
} | ||
} | ||
|
||
@Entity(name = "Book") | ||
@Table(name = "book") | ||
@TypeDef(name = "json", typeClass = MyJsonType.class) | ||
public static class Book { | ||
|
||
@Id | ||
@GeneratedValue | ||
private Long id; | ||
|
||
@NaturalId | ||
private String isbn; | ||
|
||
@Type(type = "json") | ||
@Column(columnDefinition = "json") | ||
private JsonNode properties; | ||
|
||
public String getIsbn() { | ||
return isbn; | ||
} | ||
|
||
public Book setIsbn(String isbn) { | ||
this.isbn = isbn; | ||
return this; | ||
} | ||
|
||
public JsonNode getProperties() { | ||
return properties; | ||
} | ||
|
||
public Book setProperties(JsonNode properties) { | ||
this.properties = properties; | ||
return this; | ||
} | ||
} | ||
|
||
private static ObjectMapper newMapper() { | ||
ObjectMapper mapper = new ObjectMapper(); | ||
mapper.configure(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS, true); | ||
return mapper; | ||
} | ||
} |
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
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
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
Oops, something went wrong.