2016-07-21 88 views
0

我試着做一個標準的搜索休眠錯誤拋出:IllegalArgumentException

ERROR org.hibernate.property.access.spi.GetterMethodImpl時正從休眠這個錯誤 - HHH000122: 拋出:IllegalArgumentException類:packagename.domain.User ,吸氣劑 屬性的方法,包括: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

嘗試用'criteria.add(Restrictions.eq(「author.id」,userId));'。如果這不起作用,也許你必須使用'別名'。 – Apostolos

+0

謝謝你的工作。你能否讓這個答案成爲答案,並將其標記爲這樣 –

回答

0

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

author屬性是User類的,所以你需要引用其id財產平等嘗試。