简体   繁体   English

使用Hibernate Transient Set会引发null异常

[英]Using Hibernate Transient Set throws null exception

I am trying to set up a menu with drop down children. 我正在尝试为下拉菜单的孩子设置菜单。 I have created a regular table for menu items, and a set / array / list (tried each) into which child items will get moved to make it easier to render via the JSP. 我创建了一个用于菜单项的常规表,以及一个将子项移入其中的set / array / list(分别尝试),以使其更易于通过JSP呈现。 However, when i try to use .add() to add items into the list of children, I receive a java.lang.NullPointerException and it goes no further. 但是,当我尝试使用.add()将项添加到子项列表中时,我收到了java.lang.NullPointerException,并且不再进行。

My system is set up using Spring / Hibernate, and everything works except this. 我的系统是使用Spring / Hibernate设置的,除此之外一切正常。

Model (excluding irrelevant fields and getters / setters): 模型(不相关的字段和获取器/设置器):

public class Menu {
  @Transient
  private Set<Menu> pageChildren;

  @Column(name = "pageParent")
  private Integer pageParent;

  @Column(name = "pageId")
  private Integer pageId;

  ...

And the necessary snippet from the controller: 以及来自控制器的必要代码段:

List<Menu> menuItems = new ArrayList<Menu>();
menuItems = menuService.getAll();
for (Menu item: menuItems) {
  if (item.getPageType() == 1 && item.getPageParent() > 0) {
    menuItems.get(item.getPageParent() - 1).getPageChildren().add(item);
    menuItems.remove(item);
  }
}

The transient variable needed initializing, and the for loop was configured wrong. 临时变量需要初始化,并且for循环配置错误。

Model (excluding irrelevant fields and getters / setters): 模型(不相关的字段和获取器/设置器):

public class Menu {
  @Transient
  private Set<Menu> pageChildren = new HashSet<Menu>();       //CHANGED

  @Column(name = "pageParent")
  private Integer pageParent;

  @Column(name = "pageId")
  private Integer pageId;

  ...

And the necessary snippet from the controller: 以及来自控制器的必要代码段:

List<Menu> menuItems = new ArrayList<Menu>();
menuItems = menuService.getAll();
for(int i=0; i<menuItems.size(); i++){                        //CHANGED
  Menu item = menuItems.get(i);                               //CHANGED
  if (item.getPageType() == 1 && item.getPageParent() > 0) {
    menuItems.get(item.getPageParent() - 1).getPageChildren().add(item);
    menuItems.remove(item);
  }
}

暂无
暂无

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

相关问题 Hibernate异常org.hibernate.PropertyValueException:not-null属性引用了null或瞬态值 - Hibernate exception org.hibernate.PropertyValueException: not-null property references a null or transient value Hibernate持久化@Embeddable对象集引发异常 - Hibernate persisting Set of @Embeddable objects throws exception 线程“ main” org.hibernate.PropertyValueException中的异常:not-null属性引用了null或瞬态值 - Exception in thread “main” org.hibernate.PropertyValueException: not-null property references a null or transient value org.hibernate.PropertyValueException:非空属性在使用 Spring 引导时引用 null 或瞬态值 - org.hibernate.PropertyValueException: not-null property references a null or transient value while using Spring Boot 在Hibernate中获取@Transient属性的非法状态异常 - Getting Illegal state exception for @Transient property in Hibernate 非空属性引用了一个瞬态值 Hibernate - Not-null property references a transient value Hibernate 休眠异常“ PropertyValueException:非null属性引用&gt; null或瞬时值”。 我被困住了,不知道该怎么办 - Hibernate exception “PropertyValueException: not-null property references a > null or transient value”. I am stuck and don't know what to do 使用休眠时空指针异常 - Null pointer exception while using hibernate Hibernate / Spring:Not-null属性引用null或transient值 - Hibernate/Spring: Not-null property references a null or transient value 使用Tapestry和Hibernate的Web应用程序在调用persist()时引发异常 - Web application using Tapestry and Hibernate throws Exception when calling persist()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM