Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
195 additions
and
0 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
hibernate-core/src/test/java/org/hibernate/test/subselect/Author.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
hibernate-core/src/test/java/org/hibernate/test/subselect/Book.hbm.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
40 changes: 40 additions & 0 deletions
40
hibernate-core/src/test/java/org/hibernate/test/subselect/Book.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
hibernate-core/src/test/java/org/hibernate/test/subselect/SetSubselectTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} | ||
|
||
} |