我正在使用 Hibernate 3.2.5 创建一个 Spring Web MVC 3.1.1 应用程序。我在 Netbeans 7.2 中开发,应用服务器是 GlassFish 3.1.2。数据库是Mysql 5.5。
我的问题是 subclass 没有预先加载@OneToMany
,而另一个具有 a@OneToMany
的类能够加载。此问题仅在 Glassfish 上出现:当我以完全相同的逻辑运行本地脚本时,没有问题。
相关的休眠配置:
<hibernate-configuration>
<session-factory>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping class="blagae.db.game.Game"/>
<mapping class="blagae.db.game.GamePhase"/>
<mapping class="blagae.db.game.Series"/>
<mapping class="blagae.db.play.Play"/>
</session-factory>
</hibernate-configuration>
其基本理念是美式橄榄球比赛。AGame
包含几个GamePhases
( Series
, Kickoffs
, Half Time
, End Of Game
);a Series
contains Plays
,它们被进一步细分。
GamePhase
& Game
(这些工作):
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
public class GamePhase implements Serializable {
@Id
@GeneratedValue
private int gamePhaseID;
@ManyToOne(fetch = FetchType.EAGER)
private Game game;
}
@Entity
public class Game implements Serializable {
@Id
@GeneratedValue
private int gameID;
@OneToMany(cascade=CascadeType.ALL, mappedBy="game", fetch=FetchType.EAGER)
private List<GamePhase> gamePhases = new ArrayList<GamePhase>();
public List<GamePhase> getGamePhases() {
return gamePhases;
}
public void setGamePhases(List<GamePhase> gamePhases) {
this.gamePhases = gamePhases;
}
public Series getFirstGamePhase() throws Exception {
if (gamePhases.isEmpty() || gamePhases == null) {
throw new Exception ("List<GamePhase> error");
}
return gamePhases.get(0);
}
}
Series
(<- GamePhase
) 和Play
:
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
public class Play implements Countable, Serializable {
@ManyToOne
private Series series;
}
@Entity
public class Series extends GamePhase implements Countable {
@OneToMany(cascade=CascadeType.ALL, mappedBy="series",fetch=FetchType.EAGER)
private List<Play> plays = new ArrayList<Play>();
public List<Play> getPlays() {
return plays;
}
public void setPlays(List<Play> plays) {
this.plays = plays;
}
public Play getFirstPlay() throws Exception {
if (plays.isEmpty() || plays == null) {
throw new Exception ("List<Play> error");
}
return plays.get(0);
}
}
当我在本地运行以下代码时,我得到了正常响应:
Session session = SessionFactoryHelper.getSession();
try {
GamePhase gp = ((Game) session.get(Game.class, 1)).getFirstPlay();
System.out.println("GamePhaseID="+gp.getGamePhaseID());
Play play = ((Series) session.get(Series.class, 1)).getFirstPlay();
System.out.println("PlayID="+play.getPlayID());
}
catch (Exception e) {
e.printStackTrace(System.out);
}
响应:GamePhaseID=1 PlayID=1
当我在 JSP 中运行它时(请原谅脚本,我仍在学习 JSTL 的基础知识)。
<% Game game = (Game) request.getAttribute("game");
List<GamePhase> series = game.getDrives(); %>
<p> <%=game.getFirstGamePhase() %> </p>
<%for (GamePhase gamePhase : series) {
if (gamePhase.isSeries()) {
Series series = (Series) gamePhase;%>
<table>
<tr>
<td> <%= series.getGamePhaseID() %>
</td>
<td> Play <%= series.getFirstPlay() %>
</td>
</tr>
</table>
该getFirstPlay()
方法失败,因为List<Play>
play 为空。如果我注释掉 <%-- series.getFirstPlay() --%>
,代码可以正常工作并呈现良好。
据我所知,唯一的区别是@OneToMany
inPlay
指的是一个子类实体,而@OneToMany
inGamePhase
不是...
有任何想法吗?