-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HHH-17532 Fix NPE when load-collection is used for collection within …
…embeddable
- Loading branch information
Showing
6 changed files
with
249 additions
and
13 deletions.
There are no files selected for viewing
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
76 changes: 76 additions & 0 deletions
76
...ibernate/test/annotations/loader/collectioninembedded/LoaderCollectionInEmbeddedTest.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,76 @@ | ||
/* | ||
* 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.annotations.loader.collectioninembedded; | ||
|
||
import java.util.Iterator; | ||
import java.util.Set; | ||
|
||
import org.hibernate.ObjectNotFoundException; | ||
import org.hibernate.Session; | ||
import org.hibernate.Transaction; | ||
import org.hibernate.resource.transaction.spi.TransactionStatus; | ||
|
||
import org.hibernate.testing.TestForIssue; | ||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.fail; | ||
|
||
@TestForIssue(jiraKey = "") | ||
public class LoaderCollectionInEmbeddedTest extends BaseCoreFunctionalTestCase { | ||
@Override | ||
protected String[] getXmlFiles() { | ||
return new String[] { | ||
"org/hibernate/test/annotations/loader/collectioninembedded/Loader.hbm.xml" | ||
}; | ||
} | ||
|
||
@Override | ||
protected Class[] getAnnotatedClasses() { | ||
return new Class[]{ | ||
Player.class, | ||
Team.class | ||
}; | ||
} | ||
|
||
@Test | ||
public void testBasic() throws Exception { | ||
// set up data... | ||
Session s = openSession( ); | ||
Transaction tx = s.beginTransaction(); | ||
Team t = new Team(); | ||
Player p = new Player(); | ||
p.setName( "me" ); | ||
t.getDetails().getPlayers().add( p ); | ||
p.setTeam( t ); | ||
s.persist(p); | ||
s.persist( t ); | ||
tx.commit(); | ||
s.close(); | ||
|
||
s = openSession(); | ||
tx = s.beginTransaction(); | ||
Team t2 = s.load( Team.class, t.getId() ); | ||
Set<Player> players = t2.getDetails().getPlayers(); | ||
Iterator<Player> iterator = players.iterator(); | ||
assertEquals( "me", iterator.next().getName() ); | ||
tx.commit(); | ||
s.close(); | ||
|
||
// clean up data | ||
s = openSession(); | ||
tx = s.beginTransaction(); | ||
t = s.get( Team.class, t2.getId() ); | ||
p = s.get( Player.class, p.getId() ); | ||
s.delete( p ); | ||
s.delete( t ); | ||
tx.commit(); | ||
s.close(); | ||
} | ||
} | ||
|
53 changes: 53 additions & 0 deletions
53
...core/src/test/java/org/hibernate/test/annotations/loader/collectioninembedded/Player.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,53 @@ | ||
/* | ||
* 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.annotations.loader.collectioninembedded; | ||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.Id; | ||
import javax.persistence.JoinColumn; | ||
import javax.persistence.ManyToOne; | ||
|
||
import org.hibernate.annotations.Fetch; | ||
import org.hibernate.annotations.FetchMode; | ||
|
||
@Entity | ||
public class Player { | ||
|
||
private Long id; | ||
private Team team; | ||
private String name; | ||
|
||
@Id | ||
@GeneratedValue | ||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
@ManyToOne(targetEntity = Team.class) | ||
@Fetch(FetchMode.SELECT) | ||
@JoinColumn(name = "team_id") | ||
public Team getTeam() { | ||
return team; | ||
} | ||
|
||
public void setTeam(Team team) { | ||
this.team = team; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
...e-core/src/test/java/org/hibernate/test/annotations/loader/collectioninembedded/Team.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.annotations.loader.collectioninembedded; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
import javax.persistence.Entity; | ||
import javax.persistence.FetchType; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.Id; | ||
import javax.persistence.OneToMany; | ||
|
||
import org.hibernate.annotations.Fetch; | ||
import org.hibernate.annotations.FetchMode; | ||
import org.hibernate.annotations.Loader; | ||
|
||
@Entity | ||
public class Team { | ||
private Long id; | ||
private TeamDetails details = new TeamDetails(); | ||
|
||
@Id | ||
@GeneratedValue | ||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public TeamDetails getDetails() { | ||
return details; | ||
} | ||
|
||
public void setDetails(TeamDetails details) { | ||
this.details = details; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...src/test/java/org/hibernate/test/annotations/loader/collectioninembedded/TeamDetails.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,35 @@ | ||
/* | ||
* 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.annotations.loader.collectioninembedded; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
import javax.persistence.Embeddable; | ||
import javax.persistence.FetchType; | ||
import javax.persistence.OneToMany; | ||
|
||
import org.hibernate.annotations.Fetch; | ||
import org.hibernate.annotations.FetchMode; | ||
import org.hibernate.annotations.Loader; | ||
|
||
@Embeddable | ||
public class TeamDetails { | ||
|
||
private Set<Player> players = new HashSet<Player>(); | ||
|
||
@OneToMany(targetEntity = Player.class, mappedBy = "team", fetch = FetchType.EAGER) | ||
@Fetch(FetchMode.SELECT) | ||
@Loader(namedQuery = "loadByTeam") | ||
public Set<Player> getPlayers() { | ||
return players; | ||
} | ||
|
||
public void setPlayers(Set<Player> players) { | ||
this.players = players; | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
.../test/resources/org/hibernate/test/annotations/loader/collectioninembedded/Loader.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,27 @@ | ||
<?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"> | ||
|
||
<!-- | ||
Used to demonstrate the declarative configuration | ||
of both hbm files and annotated classes | ||
See hibernate.cfg.xml and ConfigurationTest | ||
--> | ||
|
||
<hibernate-mapping package="org.hibernate.test.annotations.loader.collectioninembedded"> | ||
|
||
<sql-query name="loadByTeam"> | ||
<load-collection alias="p" role="org.hibernate.test.annotations.loader.collectioninembedded.Team.details.players"/> | ||
select {p.*} from Player p where p.team_id = ?1 | ||
</sql-query> | ||
|
||
</hibernate-mapping> |