gpt4 book ai didi

Java Spring 急切获取 Hibernate 子类失败

转载 作者:行者123 更新时间:2023-12-01 14:51:09 25 4
gpt4 key购买 nike

我正在使用 Hibernate 3.2.5 创建 Spring Web MVC 3.1.1 应用程序。我正在Netbeans 7.2中开发,应用程序服务器是GlassFish 3.1.2。数据库是Mysql 5.5。

我的问题是子类没有预先加载@OneToMany ,而另一个类的 @OneToMany能够加载。这个问题仅在 Glassfish 上出现:当我运行具有完全相同逻辑的本地脚本时,没有问题。

相关 Hibernate 配置:

<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>

基本思想是美式橄榄球比赛。一个Game包含几个GamePhases (SeriesKickoffsHalf TimeEnd Of Game);一个Series包含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);
}

response: GamePhaseID=1 PlayID=1

当我在 JSP 中运行它时(请原谅这些 scriptlet,我仍在学习 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>播放为空。如果我注释掉<%-- series.getFirstPlay() --%> ,代码可以正常运行并呈现良好。

据我所知,唯一的区别是 @OneToManyPlay指的是子类实体,而@OneToManyGamePhase不...

有什么想法吗?

最佳答案

问题在于选角。

在工作示例中:

Play play = ((Series) session.get(Series.class, 1)).getFirstPlay();

在上面的场景中,您告诉 Hibernate 显式加载 ID=1 的 Series 对象 - 因此它可以正确处理急切加载,并且加载播放不会出现任何问题。

但是,在第二个示例中:

for (GamePhase gamePhase : series) {
if (gamePhase.isSeries()) {
Series series = (Series) gamePhase;

这次 Hibernate 已经为您加载了 GamePhase 对象 - 当您将其转换为 Series 对象时,该方法可用,但显然 Hibernate 此时并不急于在转换中加载其他 Series 内容。

关于Java Spring 急切获取 Hibernate 子类失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14836354/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com