-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
GenericBasicValuedPathTest.java
106 lines (92 loc) · 3.55 KB
/
GenericBasicValuedPathTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
* 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.generics;
import org.hibernate.query.criteria.JpaPath;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.Jira;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.MappedSuperclass;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Path;
import jakarta.persistence.criteria.Root;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Marco Belladelli
*/
@SessionFactory
@DomainModel( annotatedClasses = {
GenericBasicValuedPathTest.MySuper.class,
GenericBasicValuedPathTest.MyEntity.class
} )
@Jira( "https://hibernate.atlassian.net/browse/HHH-16378" )
public class GenericBasicValuedPathTest {
@BeforeAll
public void setUp(SessionFactoryScope scope) {
scope.inTransaction( session -> session.persist( new MyEntity( 1, "my_entity" ) ) );
}
@AfterAll
public void tearDown(SessionFactoryScope scope) {
scope.inTransaction( session -> session.createMutationQuery( "delete from MyEntity" ).executeUpdate() );
}
@Test
public void testId(SessionFactoryScope scope) {
scope.inTransaction( session -> {
final CriteriaBuilder cb = session.getCriteriaBuilder();
final CriteriaQuery<Object> query = cb.createQuery();
final Root<MyEntity> root = query.from( MyEntity.class );
final Path<String> idPath = root.get( "id" );
// generic attributes are always reported as Object java type
assertThat( idPath.getJavaType() ).isEqualTo( Object.class );
assertThat( idPath.getModel() ).isSameAs( root.getModel().getAttribute( "id" ) );
assertThat( ( (JpaPath<?>) idPath ).getResolvedModel().getBindableJavaType() ).isEqualTo( Integer.class );
final Object result = session.createQuery( query.select( idPath ) ).getSingleResult();
assertThat( result ).isEqualTo( 1 );
} );
}
@Test
public void testProperty(SessionFactoryScope scope) {
scope.inTransaction( session -> {
final CriteriaBuilder cb = session.getCriteriaBuilder();
final CriteriaQuery<Object> query = cb.createQuery();
final Root<MyEntity> root = query.from( MyEntity.class );
final Path<String> dataPath = root.get( "data" );
// generic attributes are always reported as Object java type
assertThat( dataPath.getJavaType() ).isEqualTo( Object.class );
assertThat( dataPath.getModel() ).isSameAs( root.getModel().getAttribute( "data" ) );
assertThat( ( (JpaPath<?>) dataPath ).getResolvedModel().getBindableJavaType() ).isEqualTo( String.class );
final Object result = session.createQuery( query.select( dataPath ) ).getSingleResult();
assertThat( result ).isEqualTo( "my_entity" );
} );
}
@MappedSuperclass
public abstract static class MySuper<T, S> {
@Id
private T id;
private S data;
public MySuper() {
}
public MySuper(T id, S data) {
this.id = id;
this.data = data;
}
}
@Entity( name = "MyEntity" )
public static class MyEntity extends MySuper<Integer, String> {
public MyEntity() {
}
public MyEntity(Integer id, String data) {
super( id, data );
}
}
}