hibernate错误IllegalArgumentException

时间:2016-07-21 14:07:55

标签: java spring hibernate

我在尝试进行标准搜索时从休眠中获取此错误

  

错误org.hibernate.property.access.spi.GetterMethodImpl - HHH000122:   类中的IllegalArgumentException:packagename.domain.User,getter方法   property:id

对于此标准搜索

@Override
    public List<Story> findStoryByAuthor(Long userId) throws Exception {
        Criteria criteria = currentSession().createCriteria(Story.class);
        criteria.add(Restrictions.eq("author", userId));
        criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
        List<?> stories = criteria.list();
        return (List<Story>) stories;
    }

我们通过创建它的用户找到故事,所以这里是属性定义。尽量不要弄乱这个,如果你们想看到更多代码,请告诉我。

Story与用户有多对一的关系

@ManyToOne(fetch = FetchType.LAZY, targetEntity = User.class)
    @JoinColumn(name = "author_user_id", referencedColumnName = "id")
    public User getAuthor() {
        return author;
    }

Users没有引用故事,但其id属性已正确注释

@Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Long getId() {
        return id;
    }

最后是数据库结构

故事表

id bigint NOT NULL DEFAULT nextval('stories_seq'::regclass),
  parent_id bigint,
  author_user_id bigint NOT NULL,
  title character varying(100) NOT NULL,
  dt_created timestamp without time zone NOT NULL,
  dt_last_updated timestamp without time zone NOT NULL DEFAULT now_utc(),
  thumbs_up bigint DEFAULT 0,
  thumbs_down bigint DEFAULT 0,
  CONSTRAINT pk_stories PRIMARY KEY (id),
  CONSTRAINT fk_stories_author_user_id_users FOREIGN KEY (author_user_id)
      REFERENCES public.users (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION

用户表

id bigint NOT NULL DEFAULT nextval('users_seq'::regclass),
  first_name character varying(255),
  last_name character varying(255),
  email character varying(255) NOT NULL,
  user_name character varying(255),
  password character varying(255),
  phone character varying(255),
  address character varying(500),
  city character varying(255),
  state character varying(255),
  zip integer,
  preferred_language character varying(255),
  note character varying(32000),
  active boolean DEFAULT true,
  active_paid boolean DEFAULT false,
  CONSTRAINT pk_user_id PRIMARY KEY (id),
  CONSTRAINT users_unique_fields UNIQUE (email, user_name)

FYI 所有基本dao函数(添加,更新和删除)工作(完整单元测试覆盖率)。我有另一个标准搜索,几乎相同但在电子邮件字段中搜索并且工作正常。所以这个错误与用户参考

有关

1 个答案:

答案 0 :(得分:0)

请尝试

criteria.add(Restrictions.eq("author.id", userId));

author属性属于类User,因此您需要引用其id属性以获得相等。