Skip to content

Commit

Permalink
HHH-10437 - Add test for issue
Browse files Browse the repository at this point in the history
  • Loading branch information
dreab8 committed Jan 12, 2016
1 parent c53a308 commit 6443089
Show file tree
Hide file tree
Showing 4 changed files with 195 additions and 0 deletions.
@@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.subselect;

import java.util.Set;

/**
* @author Andrea Boriero
*/
public class Author {
int id;
String name;
Set<String> 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<String> getBooks() {
return books;
}

public void setBooks(Set<String> books) {
this.books = books;
}
}
@@ -0,0 +1,38 @@
<?xml version="1.0"?>
<!--
~ 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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
-->
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping
package="org.hibernate.test.subselect"
default-access="field">

<class name="Author" table="author">
<id name="id"/>

<property name="name"
not-null="true"/>

<set name="books">
<subselect>
SELECT b.author_id, b.title FROM book b join author a on b.author_id = a.id
</subselect>
<key column="author_id"/>

<element type="string" column="title"/>
</set>
</class>

<class name="Book" table="book">
<id name="id"/>
<property name="title"/>
<property name="authorId" column="author_id"/>
</class>

</hibernate-mapping>
@@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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;
}
}
@@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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();
}
}

}

0 comments on commit 6443089

Please sign in to comment.