- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
根据教程:http://jpa.ezhibernate.com/Javacode/learn.jsp?tutorial=27hibernateloadvshibernateget ,
If you initialize a JavaBean instance with a load method call, you can only access the properties of that JavaBean, for the first time, within the transactional context in which it was initialized.
If you try to access the various properties of the JavaBean after the transaction that loaded it has been committed, you'll get an exception, a LazyInitializationException, as Hibernate no longer has a valid transactional context to use to hit the database.
但是根据我的实验,使用 hibernate 3.6 和 postgres 9,它根本不会抛出任何异常。我错过了什么吗?
这是我的代码:
import org.hibernate.Session;
public class AppLoadingEntities {
/**
* @param args
*/
public static void main(String[] args) {
HibernateUtil.beginTransaction();
Session session = HibernateUtil.getSession();
EntityUser userFromGet = get(session);
EntityUser userFromLoad = load(session);
// finish the transaction
session.getTransaction().commit();
// try fetching field value from entity bean that is fetched via get outside transaction, and it'll be okay
System.out.println("userFromGet.getId() : " + userFromGet.getId());
System.out.println("userFromGet.getName() : " + userFromGet.getName());
// fetching field from entity bean that is fetched via load outside transaction, and it'll be errornous
// NOTE : but after testing, load seems to be okay, what gives ? ask forums
try {
System.out.println("userFromLoad.getId() : " + userFromLoad.getId());
System.out.println("userFromLoad.getName() : " + userFromLoad.getName());
} catch(Exception e) {
System.out.println("error while fetching entity that is fetched from load : " + e.getMessage());
}
}
private static EntityUser load(Session session) {
EntityUser user = (EntityUser) session.load(EntityUser.class, 1l);
return user;
}
private static EntityUser get(Session session) {
// safe to set it to 1, coz the table got recreated at every run of this app
EntityUser user = (EntityUser) session.get(EntityUser.class, 1l);
return user;
}
}
这是输出:
82 [main] INFO org.hibernate.annotations.common.Version - Hibernate Commons Annotations 3.2.0.Final
87 [main] INFO org.hibernate.cfg.Environment - Hibernate 3.6.0.Final
87 [main] INFO org.hibernate.cfg.Environment - hibernate.properties not found
90 [main] INFO org.hibernate.cfg.Environment - Bytecode provider name : javassist
92 [main] INFO org.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling
132 [main] INFO org.hibernate.cfg.Configuration - configuring from resource: /hibernate.cfg.xml
132 [main] INFO org.hibernate.cfg.Configuration - Configuration resource: /hibernate.cfg.xml
172 [main] WARN org.hibernate.util.DTDEntityResolver - recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
189 [main] INFO org.hibernate.cfg.Configuration - Configured SessionFactory: null
228 [main] INFO org.hibernate.cfg.AnnotationBinder - Binding entity from annotated class: EntityUser
254 [main] INFO org.hibernate.cfg.annotations.EntityBinder - Bind entity EntityUser on table MstUser
285 [main] INFO org.hibernate.cfg.Configuration - Hibernate Validator not found: ignoring
287 [main] INFO org.hibernate.cfg.search.HibernateSearchEventListenerRegister - Unable to find org.hibernate.search.event.FullTextIndexEventListener on the classpath. Hibernate Search is not enabled.
291 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - Using Hibernate built-in connection pool (not for production use!)
291 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - Hibernate connection pool size: 20
291 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - autocommit mode: false
300 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - using driver: org.postgresql.Driver at URL: jdbc:postgresql://localhost:5432/hibernate
300 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - connection properties: {user=sofco, password=****}
369 [main] INFO org.hibernate.cfg.SettingsFactory - Database ->
name : PostgreSQL
version : 9.0.1
major : 9
minor : 0
369 [main] INFO org.hibernate.cfg.SettingsFactory - Driver ->
name : PostgreSQL Native Driver
version : PostgreSQL 9.0 JDBC4 (build 801)
major : 9
minor : 0
386 [main] INFO org.hibernate.dialect.Dialect - Using dialect: org.hibernate.dialect.PostgreSQLDialect
395 [main] INFO org.hibernate.engine.jdbc.JdbcSupportLoader - Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException
396 [main] INFO org.hibernate.transaction.TransactionFactoryFactory - Transaction strategy: org.hibernate.transaction.JDBCTransactionFactory
397 [main] INFO org.hibernate.transaction.TransactionManagerLookupFactory - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
397 [main] INFO org.hibernate.cfg.SettingsFactory - Automatic flush during beforeCompletion(): disabled
397 [main] INFO org.hibernate.cfg.SettingsFactory - Automatic session close at end of transaction: disabled
397 [main] INFO org.hibernate.cfg.SettingsFactory - JDBC batch size: 15
397 [main] INFO org.hibernate.cfg.SettingsFactory - JDBC batch updates for versioned data: disabled
397 [main] INFO org.hibernate.cfg.SettingsFactory - Scrollable result sets: enabled
397 [main] INFO org.hibernate.cfg.SettingsFactory - JDBC3 getGeneratedKeys(): enabled
397 [main] INFO org.hibernate.cfg.SettingsFactory - Connection release mode: auto
398 [main] INFO org.hibernate.cfg.SettingsFactory - Default batch fetch size: 1
398 [main] INFO org.hibernate.cfg.SettingsFactory - Generate SQL with comments: disabled
398 [main] INFO org.hibernate.cfg.SettingsFactory - Order SQL updates by primary key: disabled
398 [main] INFO org.hibernate.cfg.SettingsFactory - Order SQL inserts for batching: disabled
398 [main] INFO org.hibernate.cfg.SettingsFactory - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
398 [main] INFO org.hibernate.hql.ast.ASTQueryTranslatorFactory - Using ASTQueryTranslatorFactory
399 [main] INFO org.hibernate.cfg.SettingsFactory - Query language substitutions: {}
399 [main] INFO org.hibernate.cfg.SettingsFactory - JPA-QL strict compliance: disabled
399 [main] INFO org.hibernate.cfg.SettingsFactory - Second-level cache: enabled
399 [main] INFO org.hibernate.cfg.SettingsFactory - Query cache: disabled
399 [main] INFO org.hibernate.cfg.SettingsFactory - Cache region factory : org.hibernate.cache.impl.NoCachingRegionFactory
399 [main] INFO org.hibernate.cfg.SettingsFactory - Optimize cache for minimal puts: disabled
399 [main] INFO org.hibernate.cfg.SettingsFactory - Structured second-level cache entries: disabled
401 [main] INFO org.hibernate.cfg.SettingsFactory - Echoing all SQL to stdout
402 [main] INFO org.hibernate.cfg.SettingsFactory - Statistics: disabled
402 [main] INFO org.hibernate.cfg.SettingsFactory - Deleted entity synthetic identifier rollback: disabled
402 [main] INFO org.hibernate.cfg.SettingsFactory - Default entity-mode: pojo
402 [main] INFO org.hibernate.cfg.SettingsFactory - Named query checking : enabled
402 [main] INFO org.hibernate.cfg.SettingsFactory - Check Nullability in Core (should be disabled when Bean Validation is on): enabled
424 [main] INFO org.hibernate.impl.SessionFactoryImpl - building session factory
548 [main] INFO org.hibernate.impl.SessionFactoryObjectFactory - Not binding factory to JNDI, no JNDI name configured
Hibernate: select entityuser0_.id as id0_0_, entityuser0_.name as name0_0_, entityuser0_.password as password0_0_ from MstUser entityuser0_ where entityuser0_.id=?
Hibernate: select entityuser0_.id as id0_0_, entityuser0_.name as name0_0_, entityuser0_.password as password0_0_ from MstUser entityuser0_ where entityuser0_.id=?
userFromGet.getId() : 1
userFromGet.getName() : Albert Kam xzy
userFromLoad.getId() : 1
userFromLoad.getName() : Albert Kam xzy
最佳答案
您没有看到该异常,因为在您的 load(...) 方法中,您有以下 System.out
System.out.println("user fetched with 'load' inside transaction : " + user);
正如您所看到的,您通过调用 toString() 来打印“用户”(这是自动调用的)。在日志中,我看到正在打印以下内容,这意味着您已经覆盖了 toString() 方法,该方法在内部调用每个给定属性的 getter 方法。由于这一切都是在事务仍然打开时发生的(您在调用 load(...) 方法后提交事务),因此您不会看到此异常。
user fetched with 'load' inside transaction : 1:Albert Kam xzy:abc
关于java - hibernate : load and LazyInitializationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4568594/
我开始学习使用 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
我是一名优秀的程序员,十分优秀!