|
| 1 | +package org.hibernate.orm.test.query.sql; |
| 2 | + |
| 3 | +import java.io.Serializable; |
| 4 | +import java.util.List; |
| 5 | +import java.util.Objects; |
| 6 | + |
| 7 | +import org.hibernate.query.NativeQuery; |
| 8 | + |
| 9 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 10 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 11 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 12 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 13 | +import org.junit.jupiter.api.BeforeAll; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | + |
| 16 | +import jakarta.persistence.Column; |
| 17 | +import jakarta.persistence.Entity; |
| 18 | +import jakarta.persistence.Id; |
| 19 | +import jakarta.persistence.IdClass; |
| 20 | +import jakarta.persistence.Table; |
| 21 | + |
| 22 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 23 | + |
| 24 | +@DomainModel( |
| 25 | + annotatedClasses = { |
| 26 | + ClassIdNativeQueryTest.Book.class, |
| 27 | + ClassIdNativeQueryTest.Publisher.class, |
| 28 | + } |
| 29 | +) |
| 30 | +@SessionFactory |
| 31 | +@JiraKey("HHH-17108") |
| 32 | +public class ClassIdNativeQueryTest { |
| 33 | + private static final String FILE_ID = "file1"; |
| 34 | + private static final String VERSION_ID = "version1"; |
| 35 | + |
| 36 | + @BeforeAll |
| 37 | + public void setUp(SessionFactoryScope scope) { |
| 38 | + scope.inTransaction( |
| 39 | + session -> { |
| 40 | + Publisher publisher = new Publisher( FILE_ID ); |
| 41 | + publisher.setVersionid( VERSION_ID ); |
| 42 | + publisher.setDescription( "Dodo Books" ); |
| 43 | + session.persist( publisher ); |
| 44 | + assertEquals( FILE_ID, publisher.getFileId() ); |
| 45 | + assertEquals( VERSION_ID, publisher.getVersionid() ); |
| 46 | + |
| 47 | + Book book = new Book( FILE_ID, VERSION_ID ); |
| 48 | + book.setTitle( "Birdwatchers Guide to Dodos" ); |
| 49 | + book.setDescription( "A complete guide" ); |
| 50 | + session.persist( book ); |
| 51 | + } |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void testNativeQueryWithPlaceholders(SessionFactoryScope scope) { |
| 57 | + scope.inTransaction( |
| 58 | + session -> { |
| 59 | + NativeQuery query = session |
| 60 | + .createNativeQuery( |
| 61 | + "select {book.*}, {publisher.*} from BOOK_T book, PUBLISHER_T publisher where book.fileid = publisher.fileid" ); // and book.versionid = publisher.versionid |
| 62 | + query.addEntity( "book", Book.class ); |
| 63 | + query.addEntity( "publisher", Publisher.class ); |
| 64 | + List<Object[]> results = query.list(); |
| 65 | + |
| 66 | + assertEquals( 1, results.size() ); |
| 67 | + Object[] row = results.get( 0 ); |
| 68 | + assertEquals( 2, row.length ); |
| 69 | + Book retrievedBook = (Book) row[0]; |
| 70 | + Publisher retrievedPublisher = (Publisher) row[1]; |
| 71 | + |
| 72 | + assertEquals( "A complete guide", retrievedBook.getDescription() ); |
| 73 | + assertEquals( "Dodo Books", retrievedPublisher.getDescription() ); |
| 74 | + } |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + @Entity(name = "Book") |
| 79 | + @IdClass(BookPK.class) |
| 80 | + @Table(name = "BOOK_T") |
| 81 | + public static class Book { |
| 82 | + @Id |
| 83 | + private String fileid; |
| 84 | + |
| 85 | + @Id |
| 86 | + private String versionid; |
| 87 | + |
| 88 | + @Column(name = "description") |
| 89 | + private String description; |
| 90 | + |
| 91 | + @Column(name = "title") |
| 92 | + private String title; |
| 93 | + |
| 94 | + public Book() { |
| 95 | + } |
| 96 | + |
| 97 | + public Book(final String fileid, final String versionid) { |
| 98 | + this.fileid = fileid; |
| 99 | + this.versionid = versionid; |
| 100 | + } |
| 101 | + |
| 102 | + public String getFileId() { |
| 103 | + return fileid; |
| 104 | + } |
| 105 | + |
| 106 | + public void setFileId(final String fileid) { |
| 107 | + this.fileid = fileid; |
| 108 | + } |
| 109 | + |
| 110 | + public String getVersionid() { |
| 111 | + return versionid; |
| 112 | + } |
| 113 | + |
| 114 | + public void setVersionid(final String versionid) { |
| 115 | + this.versionid = versionid; |
| 116 | + } |
| 117 | + |
| 118 | + public String getDescription() { |
| 119 | + return description; |
| 120 | + } |
| 121 | + |
| 122 | + public void setDescription(final String description) { |
| 123 | + this.description = description; |
| 124 | + } |
| 125 | + |
| 126 | + public String getTitle() { |
| 127 | + return title; |
| 128 | + } |
| 129 | + |
| 130 | + public void setTitle(final String title) { |
| 131 | + this.title = title; |
| 132 | + } |
| 133 | + |
| 134 | + } |
| 135 | + |
| 136 | + @Entity(name = "Publisher") |
| 137 | + @Table(name = "PUBLISHER_T") |
| 138 | + public static class Publisher { |
| 139 | + @Id |
| 140 | + private String fileid; |
| 141 | + |
| 142 | + private String versionid; |
| 143 | + |
| 144 | + @Column(name = "description") |
| 145 | + private String description; |
| 146 | + |
| 147 | + public Publisher() { |
| 148 | + } |
| 149 | + |
| 150 | + public Publisher(final String fileid) { |
| 151 | + this.fileid = fileid; |
| 152 | + } |
| 153 | + |
| 154 | + public String getFileId() { |
| 155 | + return fileid; |
| 156 | + } |
| 157 | + |
| 158 | + public void setFileId(final String pk) { |
| 159 | + this.fileid = pk; |
| 160 | + } |
| 161 | + |
| 162 | + public String getFileid() { |
| 163 | + return fileid; |
| 164 | + } |
| 165 | + |
| 166 | + public void setFileid(final String fileid) { |
| 167 | + this.fileid = fileid; |
| 168 | + } |
| 169 | + |
| 170 | + public String getVersionid() { |
| 171 | + return versionid; |
| 172 | + } |
| 173 | + |
| 174 | + public void setVersionid(final String versionid) { |
| 175 | + this.versionid = versionid; |
| 176 | + } |
| 177 | + |
| 178 | + public String getDescription() { |
| 179 | + return description; |
| 180 | + } |
| 181 | + |
| 182 | + public void setDescription(final String description) { |
| 183 | + this.description = description; |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + public static class BookPK implements Serializable { |
| 188 | + |
| 189 | + protected String fileid; |
| 190 | + |
| 191 | + protected String versionid; |
| 192 | + |
| 193 | + public BookPK() { |
| 194 | + |
| 195 | + } |
| 196 | + |
| 197 | + public BookPK(final String fileid) { |
| 198 | + this( fileid, "" ); |
| 199 | + } |
| 200 | + |
| 201 | + public BookPK(final String fileid, final String versionid) { |
| 202 | + this.fileid = fileid; |
| 203 | + this.versionid = versionid; |
| 204 | + } |
| 205 | + |
| 206 | + @Override |
| 207 | + public boolean equals(Object o) { |
| 208 | + if ( this == o ) { |
| 209 | + return true; |
| 210 | + } |
| 211 | + if ( o == null || getClass() != o.getClass() ) { |
| 212 | + return false; |
| 213 | + } |
| 214 | + BookPK bookPK = (BookPK) o; |
| 215 | + return Objects.equals( fileid, bookPK.fileid ) && Objects.equals( versionid, bookPK.versionid ); |
| 216 | + } |
| 217 | + |
| 218 | + @Override |
| 219 | + public int hashCode() { |
| 220 | + return Objects.hash( fileid, versionid ); |
| 221 | + } |
| 222 | + } |
| 223 | + |
| 224 | +} |
0 commit comments