HHH-17320 Add test for issue

This commit is contained in:
Andrea Boriero 2024-02-01 11:39:07 +01:00 committed by Christian Beikov
parent b78159499e
commit 7be2362f29
2 changed files with 170 additions and 0 deletions

View File

@ -0,0 +1,87 @@
/*
* 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.orm.test.annotations.basic;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.Test;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
@DomainModel(
annotatedClasses = {
SetAsBasicTest.Post.class
}
)
@SessionFactory
public class SetAsBasicTest {
@Test
public void testPersist(SessionFactoryScope scope) {
Integer postId = 1;
scope.inTransaction(
session -> {
Set<String> tags = new HashSet<>();
tags.add( "tag1" );
Post post = new Post( postId, "post", tags );
session.persist( post );
}
);
scope.inTransaction(
session -> {
Post post = session.find( Post.class, postId );
Set<String> tags = post.getTags();
assertThat( tags ).isNotNull();
assertThat( tags.size() ).isEqualTo( 1 );
assertThat( tags.stream().findFirst().get() ).isEqualTo( "tag1" );
}
);
}
@Entity
@Table(name = "post")
public static class Post {
@Id
public Integer id;
public String name;
Set<String> tags;
public Post() {
}
public Post(Integer id, String name, Set<String> tags) {
this.id = id;
this.name = name;
this.tags = tags;
}
public Integer getId() {
return id;
}
public String getName() {
return name;
}
public Set<String> getTags() {
return tags;
}
}
}

View File

@ -0,0 +1,83 @@
package org.hibernate.orm.test.annotations.basic;
import java.util.TreeMap;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.JiraKey;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.Test;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
@JiraKey("HHH-17320")
@DomainModel(
annotatedClasses = {
TreeMapAsBasicTypeTest.Customer.class
}
)
@SessionFactory
public class TreeMapAsBasicTypeTest {
@Test
public void testPersist(SessionFactoryScope scope) {
Long cutomerId = 1l;
scope.inTransaction(
session -> {
TreeMap<String, Integer> data = new TreeMap<>();
data.put( "key", 1 );
TreeMap<String, String> data2 = new TreeMap<>();
data2.put( "key2", "2" );
Customer c = new Customer( cutomerId, data, data2 );
session.persist( c );
}
);
scope.inTransaction(
session -> {
Customer customer = session.find( Customer.class, cutomerId );
TreeMap<String, Integer> data = customer.getData();
assertThat( data ).isNotNull();
assertThat( data.get( "key" ) ).isEqualTo( 1 );
TreeMap<String, String> data2 = customer.getData2();
assertThat( data2 ).isNotNull();
assertThat( data2.get( "key2" ) ).isEqualTo( "2" );
}
);
}
@Entity(name = "Customer")
public static class Customer {
@Id
private Long id;
private TreeMap<String, Integer> data;
private TreeMap<String, String> data2;
public Customer() {
}
public Customer(Long id, TreeMap<String, Integer> data, TreeMap<String, String> data2) {
this.id = id;
this.data = data;
this.data2 = data2;
}
public Long getId() {
return id;
}
public TreeMap<String, Integer> getData() {
return data;
}
public TreeMap<String, String> getData2() {
return data2;
}
}
}