Skip to content

Commit 92103ff

Browse files
committed
HHH-18494 Add test for issue
1 parent 013bb46 commit 92103ff

File tree

1 file changed

+205
-0
lines changed

1 file changed

+205
-0
lines changed
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
/*
2+
* SPDX-License-Identifier: LGPL-2.1-or-later
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.query.sql;
6+
7+
import java.util.HashSet;
8+
import java.util.Set;
9+
10+
import org.hibernate.query.NativeQuery;
11+
12+
import org.hibernate.testing.orm.junit.DomainModel;
13+
import org.hibernate.testing.orm.junit.Jira;
14+
import org.hibernate.testing.orm.junit.SessionFactory;
15+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
16+
import org.junit.jupiter.api.AfterAll;
17+
import org.junit.jupiter.api.BeforeAll;
18+
import org.junit.jupiter.api.Test;
19+
20+
import jakarta.persistence.Entity;
21+
import jakarta.persistence.FetchType;
22+
import jakarta.persistence.Id;
23+
import jakarta.persistence.JoinColumn;
24+
import jakarta.persistence.JoinTable;
25+
import jakarta.persistence.ManyToOne;
26+
import jakarta.persistence.OneToMany;
27+
import jakarta.persistence.Table;
28+
29+
import static org.junit.jupiter.api.Assertions.assertEquals;
30+
31+
/**
32+
* @author Marco Belladelli
33+
*/
34+
@DomainModel( annotatedClasses = {
35+
NativeQueryJoinTableTest.Shelf.class, NativeQueryJoinTableTest.Book.class,
36+
} )
37+
@SessionFactory
38+
@Jira( "https://hibernate.atlassian.net/browse/HHH-18494" )
39+
public class NativeQueryJoinTableTest {
40+
private static final String SHELF_ID = "shelf1";
41+
private static final String FILE_ID = "file1";
42+
43+
@Test
44+
public void testTypedQuery(SessionFactoryScope scope) {
45+
scope.inTransaction( session -> {
46+
final NativeQuery<Book> query = session.createNativeQuery(
47+
"select book.*, book_1_.shelfid from BOOK_T book, SHELF_BOOK book_1_ where book.fileid = book_1_.fileid",
48+
Book.class
49+
);
50+
final Book retrievedBook = query.getSingleResult();
51+
assertEquals( FILE_ID, retrievedBook.getFileId() );
52+
assertEquals( "Birdwatchers Guide to Dodos", retrievedBook.getTitle() );
53+
assertEquals( "nonfiction", retrievedBook.getShelf().getArea() );
54+
assertEquals( 3, retrievedBook.getShelf().getShelfNumber() );
55+
assertEquals( SHELF_ID, retrievedBook.getShelf().getShelfid() );
56+
assertEquals( 5, retrievedBook.getShelf().getPosition() );
57+
} );
58+
}
59+
60+
@Test
61+
public void testAddEntity(SessionFactoryScope scope) {
62+
scope.inTransaction( session -> {
63+
final NativeQuery query = session.createNativeQuery(
64+
"select {book.*}, book_1_.shelfid from BOOK_T book, SHELF_BOOK book_1_ where book.fileid = book_1_.fileid"
65+
);
66+
query.addEntity( "book", Book.class );
67+
final Book retrievedBook = (Book) query.getSingleResult();
68+
assertEquals( FILE_ID, retrievedBook.getFileId() );
69+
assertEquals( "Birdwatchers Guide to Dodos", retrievedBook.getTitle() );
70+
assertEquals( "nonfiction", retrievedBook.getShelf().getArea() );
71+
assertEquals( 3, retrievedBook.getShelf().getShelfNumber() );
72+
assertEquals( SHELF_ID, retrievedBook.getShelf().getShelfid() );
73+
assertEquals( 5, retrievedBook.getShelf().getPosition() );
74+
} );
75+
}
76+
77+
@BeforeAll
78+
public void setUp(SessionFactoryScope scope) {
79+
scope.inTransaction( session -> {
80+
final Shelf shelf = new Shelf();
81+
shelf.setShelfid( SHELF_ID );
82+
shelf.setArea( "nonfiction" );
83+
shelf.setPosition( Integer.valueOf( 5 ) );
84+
shelf.setShelfNumber( Integer.valueOf( 3 ) );
85+
shelf.setBooks( new HashSet<>() );
86+
session.persist( shelf );
87+
final Book book = new Book( FILE_ID );
88+
book.setTitle( "Birdwatchers Guide to Dodos" );
89+
book.setShelf( shelf );
90+
session.persist( book );
91+
} );
92+
}
93+
94+
@AfterAll
95+
public void tearDown(SessionFactoryScope scope) {
96+
scope.getSessionFactory().getSchemaManager().truncateMappedObjects();
97+
}
98+
99+
@Entity( name = "Shelf" )
100+
@Table( name = "SHELF" )
101+
public static class Shelf {
102+
@Id
103+
private String shelfid;
104+
105+
private String area;
106+
107+
private Integer shelfNumber;
108+
109+
private Integer position;
110+
111+
@OneToMany
112+
@JoinTable( name = "SHELF_BOOK", joinColumns = @JoinColumn( name = "shelfid" ), inverseJoinColumns = @JoinColumn( name = "fileid" ) )
113+
private Set<Book> books;
114+
115+
public Shelf() {
116+
}
117+
118+
public String getShelfid() {
119+
return shelfid;
120+
}
121+
122+
public void setShelfid(String shelfid) {
123+
this.shelfid = shelfid;
124+
}
125+
126+
public String getArea() {
127+
return area;
128+
}
129+
130+
public void setArea(String area) {
131+
this.area = area;
132+
}
133+
134+
public Integer getShelfNumber() {
135+
return shelfNumber;
136+
}
137+
138+
public void setShelfNumber(Integer shelfNumber) {
139+
this.shelfNumber = shelfNumber;
140+
}
141+
142+
public Integer getPosition() {
143+
return position;
144+
}
145+
146+
public void setPosition(Integer position) {
147+
this.position = position;
148+
}
149+
150+
public Set<Book> getBooks() {
151+
return books;
152+
}
153+
154+
public void setBooks(Set<Book> books) {
155+
this.books = books;
156+
}
157+
}
158+
159+
;
160+
161+
@Entity( name = "Book" )
162+
@Table( name = "BOOK_T" )
163+
public static class Book {
164+
@Id
165+
private String fileid;
166+
167+
private String title;
168+
169+
@ManyToOne( optional = false, fetch = FetchType.EAGER )
170+
@JoinTable( name = "SHELF_BOOK" )
171+
@JoinColumn( name = "shelfid" )
172+
private Shelf shelf;
173+
174+
public Book() {
175+
}
176+
177+
public Book(final String fileid) {
178+
this.fileid = fileid;
179+
}
180+
181+
public String getFileId() {
182+
return fileid;
183+
}
184+
185+
public void setFileId(final String fileid) {
186+
this.fileid = fileid;
187+
}
188+
189+
public String getTitle() {
190+
return title;
191+
}
192+
193+
public void setTitle(final String title) {
194+
this.title = title;
195+
}
196+
197+
public Shelf getShelf() {
198+
return shelf;
199+
}
200+
201+
public void setShelf(Shelf shelf) {
202+
this.shelf = shelf;
203+
}
204+
}
205+
}

0 commit comments

Comments
 (0)