简体   繁体   English

Hibernate session.load不会填充对象的arraylist属性

[英]Hibernate session.load doesn't fill arraylist property of the object

I've got a problem using hibernate's session.load function. 我在使用hibernate的session.load函数时遇到了问题。 I try to get an object according to an ID, and it returns a good object, but only primitive properties are set in the instance. 我尝试根据ID获取对象,并返回一个好的对象,但只在实例中设置了原始属性。 I have a property with is a set (mapped to some other object), but it's not retrieved after set, and the set's field is null. 我有一个属性是一个集合(映射到一些其他对象),但它没有在集合后检索,并且集合的字段为空。

Does anyone have a clue why the fetching doesn't work as expected ? 有没有人知道为什么提取不能按预期工作?

Thanks 谢谢

Check your fetching strategy on the ORM. 在ORM上检查您的提取策略。 Some of the properties could be set to lazy instead of eager. 一些属性可以设置为惰性而不是急切。 You might have to use Hibernate.Initialize after the load to fill all properties. 您可能必须在加载后使用Hibernate.Initialize来填充所有属性。 Otherwise, you'll have to modify your ORM to use eager loading. 否则,您将不得不修改您的ORM以使用预先加载。 Using annotaions, you would set the following property after the @entity attribute to turn off lazy by default: 使用注释,您可以在@entity属性之后设置以下属性以默认关闭延迟:

@org.hibernate.annotations.Proxy(lazy = false) @ org.hibernate.annotations.Proxy(lazy = false)

However, this will cause everything to load upon fetch. 但是,这将导致在获取时加载所有内容。

I guess the Set is lazy loaded, if you want it to be populated you can simply call the getter of that Set and it will be loaded. 我想Set是延迟加载的,如果你想要它被填充你可以简单地调用该Set的getter并且它将被加载。 If you want it to be loaded all the time you can add 如果您希望它可以随时加载

fetch = FetchType.EAGER

example: 例:

@OneToMany(mappedBy = "program", fetch=FetchType.EAGER)
private final List<Instruction> instructions = new ArrayList<Instruction>();

to the mapping of your entity 到你的实体的映射

Unless you are 100% positive you will always want the children to be loaded, setting EAGER fetching could lead to undesired effects. 除非你100%肯定,否则你总是希望装载孩子,设置EAGER抓取可能会导致不良影响。 The JBoss Tools set fetching to LAZY by default. JBoss工具集默认提取到LAZY

Instead, you need to initialize the child association, which can be done in various ways: 相反,您需要初始化子关联,这可以通过各种方式完成:

  1. Hibernate.initialize(myObject.getChildren())
  2. Call the size on the association: myObject.getChildren().size() 调用关联上的sizemyObject.getChildren().size()
  3. In a Criteria query, set the FetchMode on the association to JOIN : criteria.setFetchMode("children", FetchMode.JOIN); Criteria查询中,将FetchMode上的FetchMode设置为JOINcriteria.setFetchMode("children", FetchMode.JOIN);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM