Skip to content

Commit 938c637

Browse files
committed
[#1641] tests for @struct annotations
1 parent 6ab0530 commit 938c637

File tree

2 files changed

+315
-0
lines changed

2 files changed

+315
-0
lines changed
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/* Hibernate, Relational Persistence for Idiomatic Java
2+
*
3+
* SPDX-License-Identifier: Apache-2.0
4+
* Copyright: Red Hat Inc. and Hibernate Authors
5+
*/
6+
package org.hibernate.reactive;
7+
8+
import java.lang.invoke.MethodHandles;
9+
import java.util.Collection;
10+
import java.util.List;
11+
12+
import org.hibernate.annotations.Struct;
13+
import org.hibernate.reactive.annotations.DisabledFor;
14+
import org.hibernate.reactive.logging.impl.Log;
15+
import org.hibernate.reactive.logging.impl.LoggerFactory;
16+
17+
import org.junit.jupiter.api.BeforeEach;
18+
import org.junit.jupiter.api.Test;
19+
20+
import io.vertx.junit5.Timeout;
21+
import io.vertx.junit5.VertxTestContext;
22+
import jakarta.persistence.Column;
23+
import jakarta.persistence.Embeddable;
24+
import jakarta.persistence.Entity;
25+
import jakarta.persistence.GeneratedValue;
26+
import jakarta.persistence.Id;
27+
28+
import static java.util.concurrent.TimeUnit.MINUTES;
29+
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.COCKROACHDB;
30+
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.MARIA;
31+
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.MYSQL;
32+
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.ORACLE;
33+
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.SQLSERVER;
34+
import static org.junit.jupiter.api.Assertions.assertEquals;
35+
import static org.junit.jupiter.api.Assertions.assertNotNull;
36+
37+
@Timeout(value = 10, timeUnit = MINUTES)
38+
@DisabledFor(value = ORACLE, reason = "java.sql.SQLException: Not using JDBC\n" +
39+
"at org.hibernate.reactive.provider.service.NoJdbcConnectionProvider.getConnection(NoJdbcConnectionProvider.java:25)")
40+
@DisabledFor(value = {SQLSERVER, MYSQL, MARIA, COCKROACHDB}, reason = "" +
41+
"+ `Dialect does not support aggregateComponentAssignmentExpression:")
42+
public class StructComponentTest extends BaseReactiveTest {
43+
44+
private static final Log LOG = LoggerFactory.make( Log.class, MethodHandles.lookup() );
45+
46+
static Book book = createBook();
47+
static Publisher ePublisher;
48+
static Publisher pPublisher;
49+
50+
@Override
51+
protected Collection<Class<?>> annotatedEntities() {
52+
return List.of( Book.class );
53+
}
54+
55+
private static Book createBook() {
56+
ePublisher = new Publisher();
57+
ePublisher.setName( "ebooks" );
58+
ePublisher.setPubId( 5 );
59+
60+
pPublisher = new Publisher();
61+
pPublisher.setName( "paperbooks" );
62+
pPublisher.setPubId( 25 );
63+
64+
Book book = new Book();
65+
book.title = "Hibernate";
66+
book.author = "Steve";
67+
book.ebookPublisher = ePublisher;
68+
book.paperBackPublisher = pPublisher;
69+
return book;
70+
}
71+
72+
@BeforeEach
73+
public void populateDB(VertxTestContext context) {
74+
test( context, getSessionFactory()
75+
.withTransaction( session -> session.persist( book )
76+
.thenCompose( v -> session.flush() ) )
77+
);
78+
}
79+
80+
@Test
81+
public void testStructComponent(VertxTestContext context) {
82+
test( context, openSession()
83+
.thenCompose( s2 -> s2.find( Book.class, book.id ) )
84+
.thenAccept( resultBook -> {
85+
assertNotNull( resultBook );
86+
assertEquals( book.title, resultBook.title );
87+
assertEquals( book.ebookPublisher.pubId, resultBook.ebookPublisher.pubId );
88+
assertEquals( book.paperBackPublisher.pubId, resultBook.paperBackPublisher.pubId );
89+
} )
90+
);
91+
}
92+
93+
@Entity(name = "Book")
94+
public static class Book {
95+
96+
@Id
97+
@GeneratedValue
98+
private Long id;
99+
100+
private String title;
101+
102+
private String author;
103+
104+
@Column(name = "ebook_publisher")
105+
private Publisher ebookPublisher;
106+
private Publisher paperBackPublisher;
107+
}
108+
109+
@Embeddable
110+
@Struct( name = "publisher_type")
111+
public static class Publisher {
112+
113+
private String name;
114+
115+
public String getName() {
116+
return name;
117+
}
118+
119+
public void setName(String name) {
120+
this.name = name;
121+
}
122+
123+
private Integer pubId;
124+
125+
public Integer getPubId() {
126+
return pubId;
127+
}
128+
129+
public void setPubId(Integer pubId) {
130+
this.pubId = pubId;
131+
}
132+
}
133+
}
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
/* Hibernate, Relational Persistence for Idiomatic Java
2+
*
3+
* SPDX-License-Identifier: Apache-2.0
4+
* Copyright: Red Hat Inc. and Hibernate Authors
5+
*/
6+
package org.hibernate.reactive;
7+
8+
import java.awt.Point;
9+
import java.util.Collection;
10+
import java.util.List;
11+
12+
import org.hibernate.annotations.Struct;
13+
import org.hibernate.reactive.annotations.DisabledFor;
14+
15+
import org.junit.jupiter.api.BeforeEach;
16+
import org.junit.jupiter.api.Test;
17+
18+
import io.vertx.junit5.Timeout;
19+
import io.vertx.junit5.VertxTestContext;
20+
import jakarta.persistence.Column;
21+
import jakarta.persistence.Embeddable;
22+
import jakarta.persistence.Entity;
23+
import jakarta.persistence.Id;
24+
25+
import static java.util.concurrent.TimeUnit.MINUTES;
26+
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.COCKROACHDB;
27+
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.MARIA;
28+
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.MYSQL;
29+
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.ORACLE;
30+
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.SQLSERVER;
31+
import static org.junit.jupiter.api.Assertions.assertEquals;
32+
import static org.junit.jupiter.api.Assertions.assertNotNull;
33+
34+
@Timeout(value = 10, timeUnit = MINUTES)
35+
@DisabledFor(value = ORACLE, reason = "java.sql.SQLException: Not using JDBC " +
36+
"at org.hibernate.reactive.provider.service.NoJdbcConnectionProvider.getConnection(NoJdbcConnectionProvider.java:25)")
37+
@DisabledFor(value = {SQLSERVER, MYSQL, MARIA, COCKROACHDB},
38+
reason = "ORM Dialect does not support aggregateComponentAssignmentExpression:")
39+
public class StructEmbeddableTest extends BaseReactiveTest {
40+
static RecordStructHolder holder1;
41+
static RecordStructHolder holder2;
42+
43+
@Override
44+
protected Collection<Class<?>> annotatedEntities() {
45+
return List.of( RecordStructHolder.class );
46+
}
47+
48+
@BeforeEach
49+
public void populateDB(VertxTestContext context) {
50+
holder1 = new RecordStructHolder( 1L, new NamedPoint( "first", 1, 1 ) );
51+
holder2 = new RecordStructHolder( 2L, new NamedPoint( "second", 2, 2 ) );
52+
holder1.simpleStringHolder = new SimpleStringHolder( "column a","column b","column c" );
53+
54+
test( context, getSessionFactory()
55+
.withTransaction( session -> session.persist( holder1, holder2 )
56+
.thenCompose( v -> session.flush() ) )
57+
);
58+
}
59+
60+
@Test
61+
public void testFindAndUpdate(VertxTestContext context) {
62+
test( context, openSession()
63+
.thenCompose( s2 -> s2.find( RecordStructHolder.class, holder1.id )
64+
.thenAccept( resultHolder -> {
65+
assertNotNull( resultHolder );
66+
assertEquals( holder1.getThePoint().getPoint(), resultHolder.getThePoint().getPoint() );
67+
resultHolder.setThePoint( new NamedPoint( "third", 3, 3 ) );
68+
assertEquals( "third", resultHolder.getThePoint().name );
69+
} )
70+
.thenCompose( vv -> s2.flush() )
71+
.thenCompose( vv -> s2.find( RecordStructHolder.class, holder1.id )
72+
.thenAccept( found -> assertEquals( "third", found.getThePoint().getName() ) ) )
73+
)
74+
);
75+
}
76+
77+
@Test
78+
public void testSelectionItems(VertxTestContext context) {
79+
test( context, openSession()
80+
.thenCompose( s -> s.createSelectionQuery( "from RecordStructHolder where id = ?1", RecordStructHolder.class )
81+
.setParameter( 1, holder1.getId() )
82+
.getResultList() )
83+
.thenAccept( holders -> {
84+
assertNotNull( holders );
85+
final RecordStructHolder holder = holders.get( 0 );
86+
assertEquals( holder1.getThePoint().getPoint(), holder.getThePoint().getPoint() );
87+
} )
88+
);
89+
}
90+
91+
@Test
92+
public void testEmbeddedColumnOrder(VertxTestContext context) {
93+
test( context, openSession()
94+
.thenCompose( s2 -> s2.find( RecordStructHolder.class, holder1.id )
95+
.thenAccept( resultHolder -> {
96+
assertNotNull( resultHolder );
97+
assertEquals( holder1.getThePoint().getPoint(), resultHolder.getThePoint().getPoint() );
98+
assertEquals( "column a", holder1.simpleStringHolder.aColumn );
99+
assertEquals( "column b", holder1.simpleStringHolder.bColumn );
100+
assertEquals( "column c", holder1.simpleStringHolder.cColumn );
101+
} )
102+
)
103+
);
104+
}
105+
106+
@Entity(name = "RecordStructHolder")
107+
public static class RecordStructHolder {
108+
@Id
109+
private Long id;
110+
@Struct(name = "my_point_type")
111+
private NamedPoint thePoint;
112+
113+
private SimpleStringHolder simpleStringHolder;
114+
115+
public RecordStructHolder() {
116+
}
117+
118+
public RecordStructHolder(Long id, NamedPoint thePoint) {
119+
this.id = id;
120+
this.thePoint = thePoint;
121+
}
122+
123+
public Long getId() {
124+
return id;
125+
}
126+
127+
public void setId(Long id) {
128+
this.id = id;
129+
}
130+
131+
public NamedPoint getThePoint() {
132+
return thePoint;
133+
}
134+
135+
public void setThePoint(NamedPoint point) {
136+
this.thePoint = point;
137+
}
138+
}
139+
140+
@Embeddable
141+
static class NamedPoint {
142+
public String name;
143+
public Point point;
144+
145+
public NamedPoint() {
146+
}
147+
148+
public NamedPoint(String name, Integer x, Integer y) {
149+
this.point = new Point( x, y );
150+
this.name = name;
151+
}
152+
153+
public String getName() {
154+
return name;
155+
}
156+
157+
public Point getPoint() {
158+
return point;
159+
}
160+
}
161+
162+
// By default, the order of columns is based on the alphabetical ordering of the embeddable type attribute names.
163+
// This class has column names re-defined using @Column annotation "name" attribute and will reverse the column order
164+
@Embeddable
165+
@Struct(name = "simple_string_holder")
166+
static class SimpleStringHolder {
167+
@Column(name = "c")
168+
public String aColumn;
169+
@Column(name = "b")
170+
public String bColumn;
171+
@Column(name = "a")
172+
public String cColumn;
173+
174+
public SimpleStringHolder() {}
175+
176+
public SimpleStringHolder(String aColumn, String bColumn, String cColumn) {
177+
this.aColumn = aColumn;
178+
this.bColumn = bColumn;
179+
this.cColumn = cColumn;
180+
}
181+
}
182+
}

0 commit comments

Comments
 (0)