Skip to content

Commit c061826

Browse files
committed
[#1641] tests for @struct annotations
1 parent c950801 commit c061826

File tree

2 files changed

+311
-0
lines changed

2 files changed

+311
-0
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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 = "see issue https://github.com/hibernate/hibernate-reactive/issues/1855")
39+
@DisabledFor(value = {SQLSERVER, MYSQL, MARIA, COCKROACHDB}, reason = "ORM does not support @Struct for these databases")
40+
public class StructComponentTest extends BaseReactiveTest {
41+
42+
private static final Log LOG = LoggerFactory.make( Log.class, MethodHandles.lookup() );
43+
44+
static Book book = createBook();
45+
static Publisher ePublisher;
46+
static Publisher pPublisher;
47+
48+
@Override
49+
protected Collection<Class<?>> annotatedEntities() {
50+
return List.of( Book.class );
51+
}
52+
53+
private static Book createBook() {
54+
ePublisher = new Publisher();
55+
ePublisher.setName( "ebooks" );
56+
ePublisher.setPubId( 5 );
57+
58+
pPublisher = new Publisher();
59+
pPublisher.setName( "paperbooks" );
60+
pPublisher.setPubId( 25 );
61+
62+
Book book = new Book();
63+
book.title = "Hibernate";
64+
book.author = "Steve";
65+
book.ebookPublisher = ePublisher;
66+
book.paperBackPublisher = pPublisher;
67+
return book;
68+
}
69+
70+
@BeforeEach
71+
public void populateDB(VertxTestContext context) {
72+
test( context, getSessionFactory()
73+
.withTransaction( session -> session.persist( book )
74+
.thenCompose( v -> session.flush() ) )
75+
);
76+
}
77+
78+
@Test
79+
public void testStructComponent(VertxTestContext context) {
80+
test( context, openSession()
81+
.thenCompose( s2 -> s2.find( Book.class, book.id ) )
82+
.thenAccept( resultBook -> {
83+
assertNotNull( resultBook );
84+
assertEquals( book.title, resultBook.title );
85+
assertEquals( book.ebookPublisher.pubId, resultBook.ebookPublisher.pubId );
86+
assertEquals( book.paperBackPublisher.pubId, resultBook.paperBackPublisher.pubId );
87+
} )
88+
);
89+
}
90+
91+
@Entity(name = "Book")
92+
public static class Book {
93+
94+
@Id
95+
@GeneratedValue
96+
private Long id;
97+
98+
private String title;
99+
100+
private String author;
101+
102+
@Column(name = "ebook_publisher")
103+
private Publisher ebookPublisher;
104+
private Publisher paperBackPublisher;
105+
}
106+
107+
@Embeddable
108+
@Struct( name = "publisher_type")
109+
public static class Publisher {
110+
111+
private String name;
112+
113+
public String getName() {
114+
return name;
115+
}
116+
117+
public void setName(String name) {
118+
this.name = name;
119+
}
120+
121+
private Integer pubId;
122+
123+
public Integer getPubId() {
124+
return pubId;
125+
}
126+
127+
public void setPubId(Integer pubId) {
128+
this.pubId = pubId;
129+
}
130+
}
131+
}
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
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 = "see issue https://github.com/hibernate/hibernate-reactive/issues/1855")
36+
@DisabledFor(value = {SQLSERVER, MYSQL, MARIA, COCKROACHDB}, reason = "ORM does not support @Struct for these databases")
37+
public class StructEmbeddableTest extends BaseReactiveTest {
38+
static RecordStructHolder holder1;
39+
static RecordStructHolder holder2;
40+
41+
@Override
42+
protected Collection<Class<?>> annotatedEntities() {
43+
return List.of( RecordStructHolder.class );
44+
}
45+
46+
@BeforeEach
47+
public void populateDB(VertxTestContext context) {
48+
holder1 = new RecordStructHolder( 1L, new NamedPoint( "first", 1, 1 ) );
49+
holder2 = new RecordStructHolder( 2L, new NamedPoint( "second", 2, 2 ) );
50+
holder1.simpleStringHolder = new SimpleStringHolder( "column a","column b","column c" );
51+
52+
test( context, getSessionFactory()
53+
.withTransaction( session -> session.persist( holder1, holder2 )
54+
.thenCompose( v -> session.flush() ) )
55+
);
56+
}
57+
58+
@Test
59+
public void testFindAndUpdate(VertxTestContext context) {
60+
test( context, openSession()
61+
.thenCompose( s2 -> s2.find( RecordStructHolder.class, holder1.id )
62+
.thenAccept( resultHolder -> {
63+
assertNotNull( resultHolder );
64+
assertEquals( holder1.getThePoint().getPoint(), resultHolder.getThePoint().getPoint() );
65+
resultHolder.setThePoint( new NamedPoint( "third", 3, 3 ) );
66+
assertEquals( "third", resultHolder.getThePoint().name );
67+
} )
68+
.thenCompose( vv -> s2.flush() )
69+
.thenCompose( vv -> s2.find( RecordStructHolder.class, holder1.id )
70+
.thenAccept( found -> assertEquals( "third", found.getThePoint().getName() ) ) )
71+
)
72+
);
73+
}
74+
75+
@Test
76+
public void testSelectionItems(VertxTestContext context) {
77+
test( context, openSession()
78+
.thenCompose( s -> s.createSelectionQuery( "from RecordStructHolder where id = ?1", RecordStructHolder.class )
79+
.setParameter( 1, holder1.getId() )
80+
.getResultList() )
81+
.thenAccept( holders -> {
82+
assertNotNull( holders );
83+
final RecordStructHolder holder = holders.get( 0 );
84+
assertEquals( holder1.getThePoint().getPoint(), holder.getThePoint().getPoint() );
85+
} )
86+
);
87+
}
88+
89+
@Test
90+
public void testEmbeddedColumnOrder(VertxTestContext context) {
91+
test( context, openSession()
92+
.thenCompose( s2 -> s2.find( RecordStructHolder.class, holder1.id )
93+
.thenAccept( resultHolder -> {
94+
assertNotNull( resultHolder );
95+
assertEquals( holder1.getThePoint().getPoint(), resultHolder.getThePoint().getPoint() );
96+
assertEquals( "column a", holder1.simpleStringHolder.aColumn );
97+
assertEquals( "column b", holder1.simpleStringHolder.bColumn );
98+
assertEquals( "column c", holder1.simpleStringHolder.cColumn );
99+
} )
100+
)
101+
);
102+
}
103+
104+
@Entity(name = "RecordStructHolder")
105+
public static class RecordStructHolder {
106+
@Id
107+
private Long id;
108+
@Struct(name = "my_point_type")
109+
private NamedPoint thePoint;
110+
111+
private SimpleStringHolder simpleStringHolder;
112+
113+
public RecordStructHolder() {
114+
}
115+
116+
public RecordStructHolder(Long id, NamedPoint thePoint) {
117+
this.id = id;
118+
this.thePoint = thePoint;
119+
}
120+
121+
public Long getId() {
122+
return id;
123+
}
124+
125+
public void setId(Long id) {
126+
this.id = id;
127+
}
128+
129+
public NamedPoint getThePoint() {
130+
return thePoint;
131+
}
132+
133+
public void setThePoint(NamedPoint point) {
134+
this.thePoint = point;
135+
}
136+
}
137+
138+
@Embeddable
139+
static class NamedPoint {
140+
public String name;
141+
public Point point;
142+
143+
public NamedPoint() {
144+
}
145+
146+
public NamedPoint(String name, Integer x, Integer y) {
147+
this.point = new Point( x, y );
148+
this.name = name;
149+
}
150+
151+
public String getName() {
152+
return name;
153+
}
154+
155+
public Point getPoint() {
156+
return point;
157+
}
158+
}
159+
160+
// By default, the order of columns is based on the alphabetical ordering of the embeddable type attribute names.
161+
// This class has column names re-defined using @Column annotation "name" attribute and will reverse the column order
162+
@Embeddable
163+
@Struct(name = "simple_string_holder")
164+
static class SimpleStringHolder {
165+
@Column(name = "c")
166+
public String aColumn;
167+
@Column(name = "b")
168+
public String bColumn;
169+
@Column(name = "a")
170+
public String cColumn;
171+
172+
public SimpleStringHolder() {}
173+
174+
public SimpleStringHolder(String aColumn, String bColumn, String cColumn) {
175+
this.aColumn = aColumn;
176+
this.bColumn = bColumn;
177+
this.cColumn = cColumn;
178+
}
179+
}
180+
}

0 commit comments

Comments
 (0)