gpt4 book ai didi

java - 控制台应用程序中出现延迟加载错误

转载 作者:行者123 更新时间:2023-12-01 05:44:13 24 4
gpt4 key购买 nike

我有以下类(class):

@Entity
@Table(name="Player")
public class Player {

private String id;
private Set<String> nicknames = new HashSet<String>();

@ElementCollection(fetch=FetchType.LAZY)
@CollectionTable(name="Nicknames", joinColumns=@JoinColumn(name="user_id"))
@Column(name="nickname")
public Set<String> getNicknames() {
return nicknames;
}
}

我的 DaoServiceImpl 看起来像这样:

@Repository("daoService")
public class DaoServiceImpl implements DaoService {

@Autowired
SessionFactory sessionFactory;

@Override
public String addPlayer(Player player){
Session session = sessionFactory.getCurrentSession();
return (String) session.save(player);
}

@Override
public Player getPlayer(String id){
Session session = sessionFactory.getCurrentSession();
return (Player)session.load(Player.class, id);
}

@Override
public void updatePlayer(Player player){
Session session = sessionFactory.getCurrentSession();
session.update(player);
}
}

我的事务管理器看起来像这样:

<aop:config>
<aop:pointcut id="daoServicePoint"
expression="execution(* com.vanilla.daoService.DaoService.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="daoServicePoint"/>
</aop:config>

<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>

在我的主要功能中,我正在执行以下操作:

public void run(){
Player player = new Player();
player.addNickName("Nick1");
player.addNickName("Nick2");
player.setFirstName("Danny");
player.setLastName("Lesnik");
String playerId = daoService.addPlayer(player);
player = daoService.getPlayer(playerId);
player.addNickName("Nick4");
player.removeNickName("Nick2");
daoService.updatePlayer(player);
}

我在 player.addNickName 上遇到以下异常

Exception in thread "main" org.hibernate.LazyInitializationException: could not initialize proxy - no Session
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:167)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:215)
at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:190)
at com.vanilla.objects.Player_$$_javassist_0.addNickName(Player_$$_javassist_0.java)
at com.vanilla.main.Main.run(Main.java:27)
at com.vanilla.main.Main.main(Main.java:36)

如果是Web应用程序,我会使用OpenSessionIviewFilter。我应该如何解决 Spring Hibernate Java 控制台应用程序?

我知道我可以使用 join HQL 查询或使其成为 Eager,但我想出于 self 学习的目的而让它变得懒惰:)

最佳答案

字符串playerId = daoService.addPlayer(player);在上面的代码中,您保存实体并在当前 session 关闭。之后,您尝试使用同一 session 加载玩家。但是没有打开的 session 。您必须在 getPlayer 方法中打开新 session ,

您的代码应更改如下

公共(public)玩家 getPlayer(String id){ session session = sessionFactory.openSession(); return (Player)session.load(Player.class, id); }

关于java - 控制台应用程序中出现延迟加载错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6325153/

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