java 如何避免在 Hibernate 中获取 javassist 惰性实体代理实例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14023969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to avoid getting javassist lazy Entity proxy instances in Hibernate
提问by SkyWalker
What do I have to change to avoid Hibernate giving me lazy javassist instance proxies rather than the true entity?
我必须改变什么才能避免 Hibernate 给我懒惰的 javassist 实例代理而不是真正的实体?
UPDATE: I am using Spring 3.x and Hibernate 4.x
更新:我使用的是 Spring 3.x 和 Hibernate 4.x
The API I am using to load the entity is org.hibernate.internal.SessionImpl#load(Person.class, Id)
and the mapping simply:
我用来加载实体的 APIorg.hibernate.internal.SessionImpl#load(Person.class, Id)
和映射很简单:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.perfectjpattern.example.model">
<class name="Person" table="PERSON_" >
<id name="id">
<generator class="native"></generator>
</id>
<property name="name" update="false" />
<property name="age" update="true" />
</class>
<query name="Person.findByName">
<![CDATA[select p from Person p where p.name = ? ]]>
</query>
<query name="Person.findByAge">
<![CDATA[select p from Person p where p.age = :Age ]]>
</query>
</hibernate-mapping>
采纳答案by SkyWalker
Actually solved it by simply changing the mapping to (see the default-lazy="false"
):
实际上通过简单地将映射更改为(参见default-lazy="false"
)来解决它:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.perfectjpattern.example.model" default-lazy="false">
回答by ivivi
You can use Hibernate.initialize(obj)
after session.load(id)
.
Hibernate.initialize(obj)
之后可以使用session.load(id)
。
This method can instantly initialize your obj.
此方法可以立即初始化您的 obj。