- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有两个类:
学生和工资
在数据库中,表 Etudiant
具有表 Pays
的外键。
在我的代码中我有这样的东西:
List<Etudiant> listEtudiants = (List<Etudiant>) etudiantService.getAll();
for(Etudiant etudiant : listEtudiants) {
if(((JTextField)arg0.getSource()).getText().equals(etudiant.getNom())){
System.out.println(etudiant.getPays().getNom());
}
}
但是当我运行此代码时,它失败并出现异常:
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
在行中:
System.out.println(etudiant.getPays().getNom());
学生映射:
<hibernate-mapping>
<class name="tp.ihm.domain.Etudiant" table="etudiant" schema="public" optimistic-lock="version">
<id name="numInsc" type="java.lang.Long">
<column name="num_insc" />
<generator class="assigned" />
</id>
<many-to-one name="pays" class="tp.ihm.domain.Pays" fetch="select">
<column name="pays" not-null="true" />
</many-to-one>
<property name="nom" type="string">
<column name="nom" length="50" not-null="true" />
</property>
<property name="prenom" type="string">
<column name="prenom" length="50" not-null="true" />
</property>
</class>
</hibernate-mapping>
付款映射:
<hibernate-mapping>
<class name="tp.ihm.domain.Pays" table="pays" schema="public" optimistic-lock="version">
<id name="id" type="java.lang.Long">
<column name="id" />
<generator class="assigned" />
</id>
<property name="nom" type="string">
<column name="nom" length="45" not-null="true" />
</property>
<set name="etudiants" table="etudiant" inverse="true" lazy="true" fetch="select">
<key>
<column name="pays" not-null="true" />
</key>
<one-to-many class="tp.ihm.domain.Etudiant" />
</set>
</class>
</hibernate-mapping>
我尝试删除 Pays
映射中的 fetch 属性,然后将其值更改为 eager,但没有任何效果。
有人可以帮我解决这个问题吗?
这是 getAll
方法的代码:
public List getAll() throws EntityNotFoundException {
// Get the current session
Session s = getSession();
List list = null;
// If the BLL layer started a transaction
// In this case it is the BLL layer that manages the session and transaction
if (anActiveTransactionExist(s)) {
list = s.createCriteria(Etudiant).list();
} else {
LOGGER.debug("DAO initialize its own transaction");
Transaction tx = null;
try {
// Starts a transaction locally
tx = s.beginTransaction();
list = s.createCriteria(boClass).list();
tx.commit();
} catch (RuntimeException ex) {
// Cancel the transaction if there is a problem
handleDaoOpError(tx, ex);
} finally {
closeSession(s);
}
}
if (list == null || list.size() == 0)
throw new EntityNotFoundException();
return list;
}
最佳答案
您需要将 Etudiant
的映射从 fetch=select
更改为 fetch=join
fetch-“join” = Disable the lazy loading, always load all the collections and entities.
fetch-“select” (default) = Lazy load all the collections and entities.
<many-to-one name="pays" class="tp.ihm.domain.Pays" fetch="join">
<column name="pays" not-null="true" />
</many-to-one>
关于java - Hibernate org.hibernate.LazyInitializationException : could not initialize,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27377993/
我开始学习使用 Hibernate 来开发 Java 中的 Web 系统,但我在类的映射和使用方面遇到了一些问题。 我有两个表:tblusuario和tblperfilusuario,tblusuar
我正在开发一个部署在 Tomcat 上的 J2EE 服务器应用程序。我使用 Spring 源作为 MVC 框架,使用 Hibernate 作为 ORM 提供者。我的对象模型有很多惰性关系(根据请求获取
我在尝试访问 permissions 时遇到 LazyInitializationException我的 Collection User目的。异常信息: org.hibernate.LazyIniti
我在我们的生产网络服务器上收到以下错误: NHibernate.LazyInitializationException : Initializing[Domain.Entities.AudienceT
这是一个让我困惑的问题。我正在尝试实现基本的 Hibernate DAO 结构,但遇到了问题。 这是基本代码: int startingCount = sfdao.count(); sfdao.cre
我看到类似的问题已经发布过,但是标准建议的解决方案似乎不适用于我,我想了解原因。 我有一个非常活跃的Grails 2.2.5应用程序,而且我一直在调查涉及大量数据集的报告问题。所讨论的域类是Polic
嗨,我有使用 jhipster 4.2.0 的微服务应用程序这是我的资源类 import org.springframework.web.bind.annotation.*; import javax
我有一个 Patient 类,其中包含 Id 集合和文件夹集合。当我尝试单独保存文件夹时,由于患者的 id 集合,它会抛出 LazyInitializationException。 患者类别如下所示:
我正在使用 Hibernate 3.6.0 制作一个应用程序,但出现了 LazyInitializationException。我没能解决这个问题,所以我来这里寻求你的帮助。 这是堆栈跟踪: Exce
在将域对象从数据库转换为客户端的资源对象期间,我遇到延迟加载字段的问题。 Customer :使用惰性字段从数据库加载实体 FullCustomer :将发送给客户端的实体。 服务层: @Transa
我有一个问题。我在 grails 中有事务性服务: class MyService { static transactional = true Set getFurniture(Long
我有一些具有两种关系的模型: @Entity @Table(name = "data_model") public class DataModel { @Id @GeneratedVa
传统上,我们会尝试避免 LazyInitializationException。但是,我需要暂时允许它们被抛出。这是我想要做的伪代码: Session session = ...; Customer
我正在将 Hibernate 4 用于 Java 项目。 我有下一个包含两个实体的方案: Units *has many* Users 因此,单位拥有用户的外键(请参阅 table definitio
JPA session 在静态方法中的行为如何?我使用 Hibernate 从数据库中检索对象列表,然后使用 Guava 的静态 Lists.transform 方法对其进行转换,这会导致: org.
我有这样的 OmQcActivity 类: @Entity @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) @Table(name="OM_QC
我的代码运行良好,但出现异常。将调用一个操作方法,但是在方法调用完成后我得到了这个异常。 我尝试了网络上所有可用的选项,但似乎没有什么能真正帮到我。 12/14 23:30:17 INFO er.S
目前我有一个子实体有一个 @ManyToOne关联到它的父实体。之前的开发者已经将这个字段设置为 lazy="false"在 session 关闭时也可以随时获取父级,但我决定它应该是 lazy="t
此代码的目的是允许用户检查 ID;在这种情况下,它将是检查特定运动比赛的 ID,以便查看相关比赛及其详细信息。这样做的全部目的是通知用户,如果他们输入了错误的 ID,我只是想在 SOAP 请求中生成一
尝试从数据库获取某些对象时,我不断收到错误: org.hibernate.LazyInitializationException (LazyInitializationException.java:1
我是一名优秀的程序员,十分优秀!