Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

An extra UPDATE is triggered when using records #619

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -55,6 +55,8 @@ public void test() {
"Vlad Mihalcea",
bookProperties.get("author")
);

assertEquals(Long.valueOf(0), book.getVersion());
});
}

Expand All @@ -71,6 +73,9 @@ public static class Book {
@Column(length = 15)
private String isbn;

@Version
private Long version;

@Type(type = "jsonb")
@Column(columnDefinition = "jsonb")
private Map<String, String> properties = new HashMap<>();
Expand All @@ -84,6 +89,10 @@ public Book setIsbn(String isbn) {
return this;
}

public Long getVersion() {
return version;
}

public Map<String, String> getProperties() {
return properties;
}
Expand All @@ -98,4 +107,4 @@ public Book addProperty(String key, String value) {
return this;
}
}
}
}
@@ -0,0 +1,123 @@
package io.hypersistence.utils.hibernate.type.json;

import io.hypersistence.utils.hibernate.util.AbstractPostgreSQLIntegrationTest;
import org.hibernate.Session;
import org.hibernate.annotations.NaturalId;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;
import org.junit.Test;
import org.springframework.util.Assert;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Version;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.assertEquals;

/**
* @author Vlad Mihalcea
*/
public class PostgreSQLJsonMapTest2 extends AbstractPostgreSQLIntegrationTest {

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

@Test
public void test() {
Map<String, Book.BookInventory> bookTypeMap = new HashMap<>();
bookTypeMap.put("Fiction",
new Book.BookInventory(new Book.BookInventory.Inventory(10, 1),
Collections.singletonMap(501L, new Book.BookInventory.Inventory(1,1))));

doInJPA(entityManager -> {
entityManager.persist(
new Book()
.setIsbn("978-9730228236")
.addProperty("Publisher 1", bookTypeMap)
);
});

doInJPA(entityManager -> {
Book book = entityManager.unwrap(Session.class)
.bySimpleNaturalId(Book.class)
.load("978-9730228236");

Map<String, Map<String, Book.BookInventory>> bookProperties = book.getProperties();

assertEquals(
bookTypeMap,
bookProperties.get("Publisher 1")
);

assertEquals(Long.valueOf(0), book.getVersion());
});
}

@Entity(name = "Book")
@Table(name = "book")
@TypeDef(name = "jsonb", typeClass = JsonBinaryType.class)
public static class Book {

@Id
@GeneratedValue
private Long id;

@NaturalId
@Column(length = 15)
private String isbn;

@Version
private Long version;

@Type(type = "jsonb")
@Column(columnDefinition = "jsonb")
private Map<String, Map<String, BookInventory>> properties = new HashMap<>();

public String getIsbn() {
return isbn;
}

public Book setIsbn(String isbn) {
this.isbn = isbn;
return this;
}

public Long getVersion() {
return version;
}

public Map<String, Map<String, BookInventory>> getProperties() {
return properties;
}

public Book setProperties(Map<String, Map<String, BookInventory>> properties) {
this.properties = properties;
return this;
}

public Book addProperty(String key, Map<String, BookInventory> value) {
properties.put(key, value);
return this;
}

public record BookInventory(Inventory global, Map<Long, Inventory> info) {

public BookInventory {
Assert.notNull(info, "info must be provided");
}

public record Inventory(Integer maxItems, Integer remainingItems) {
}
}
}
}