diff --git a/hibernate-core/src/test/java/org/hibernate/test/subselect/Author.java b/hibernate-core/src/test/java/org/hibernate/test/subselect/Author.java new file mode 100644 index 000000000000..b26fc1d8306d --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/subselect/Author.java @@ -0,0 +1,42 @@ +/* + * 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 . + */ +package org.hibernate.test.subselect; + +import java.util.Set; + +/** + * @author Andrea Boriero + */ +public class Author { + int id; + String name; + Set books; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Set getBooks() { + return books; + } + + public void setBooks(Set books) { + this.books = books; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/subselect/Book.hbm.xml b/hibernate-core/src/test/java/org/hibernate/test/subselect/Book.hbm.xml new file mode 100644 index 000000000000..01aee9e6c315 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/subselect/Book.hbm.xml @@ -0,0 +1,38 @@ + + + + + + + + + + + + + + SELECT b.author_id, b.title FROM book b join author a on b.author_id = a.id + + + + + + + + + + + + + + diff --git a/hibernate-core/src/test/java/org/hibernate/test/subselect/Book.java b/hibernate-core/src/test/java/org/hibernate/test/subselect/Book.java new file mode 100644 index 000000000000..b71f3e19e033 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/subselect/Book.java @@ -0,0 +1,40 @@ +/* + * 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 . + */ +package org.hibernate.test.subselect; + +/** + * @author Andrea Boriero + */ +public class Book { + int id; + String title; + int authorId; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public int getAuthorId() { + return authorId; + } + + public void setAuthorId(int authorId) { + this.authorId = authorId; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/subselect/SetSubselectTest.java b/hibernate-core/src/test/java/org/hibernate/test/subselect/SetSubselectTest.java new file mode 100644 index 000000000000..bbae6b013552 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/subselect/SetSubselectTest.java @@ -0,0 +1,75 @@ +/* + * 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 . + */ +package org.hibernate.test.subselect; + +import org.hibernate.Session; +import org.hibernate.resource.transaction.spi.TransactionStatus; + +import org.junit.Test; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; + +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertThat; + +/** + * @author Andrea Boriero + */ +@TestForIssue(jiraKey = "") +public class SetSubselectTest extends BaseCoreFunctionalTestCase { + public String[] getMappings() { + return new String[] {"subselect/Book.hbm.xml"}; + } + + @Test + public void testSubselect() { + Session s = openSession(); + + s.getTransaction().begin(); + try { + Author b = new Author(); + b.setName( "Camilleri" ); + b.setId( 1 ); + + s.save( b ); + + Book book = new Book(); + book.setId( 2 ); + book.setAuthorId( 1 ); + book.setTitle( "Il sognaglio" ); + s.save( book ); + + Book book2 = new Book(); + book2.setId( 3 ); + book2.setAuthorId( 1 ); + book2.setTitle( "Il casellante" ); + + s.save( book2 ); + + s.getTransaction().commit(); + } + catch (Exception e) { + if ( s.getTransaction().getStatus() == TransactionStatus.ACTIVE ) { + s.getTransaction().rollback(); + } + } + finally { + s.close(); + } + + s = openSession(); + try { + Author author = s.get( Author.class, 1 ); + assertThat( author.getBooks().size(), is( 2 ) ); + } + finally { + s.close(); + } + } + +}