标准加入不在复合表上工作

时间:2011-03-02 22:48:54

标签: hibernate spring composite-key hibernate-criteria

我无法在一次调用中使用复合可嵌入键表上的select来检索这两个属性。我首先抓住计数,然后分页。整个站点的其他地方都使用相同的逻辑。我在查询中得到了以下错误。真的迷失在这里。

java.sql.SQLSyntaxErrorException:ORA-00904:“U3 _”。“ID”:标识符无效

  select
            count(*) as y0_ 
        from
            TEST.USERBADGES this_ 
        where
            u3_.ID=?
    Hibernate: 
        select
            count(*) as y0_ 
        from
            TEST.USERBADGES this_ 
        where
            u3_.ID=?   


Criteria criteria = userBadgeRepository.getCritieria();

            criteria.createCriteria("userBadgePK", "ub", 1);
            criteria.createCriteria("ub.badge", "b", 1);
            criteria.createCriteria("ub.user", "u", 1);

UserBadge.java

@Entity
@Table(name = "USERBADGES", schema = "TEST")
@SuppressWarnings("serial")
@AssociationOverrides({
@AssociationOverride(name = "userBadgesPK.user", joinColumns = @JoinColumn(name = "userid")),
@AssociationOverride(name = "userBadgesPK.badge", joinColumns = @JoinColumn(name = "badgeid"))
})
public class UserBadge implements Serializable {

    /**
     * Primary key
     */

    private UserBadgePK userBadgesPK = new UserBadgePK();

    /**
     * Attribute visible.
     */
    private Long visible;

    /**
     * Attribute datecreated.
     */
    private Date dateCreated;


    /**
     * Get the primary key
     */
    @Basic
    @EmbeddedId
    public UserBadgePK getUserBadgePK() {
        return this.userBadgesPK;
    }

    /**
     * set the primary key
     */
    public void setUserBadgePK(UserBadgePK userBadgesPK) {
        this.userBadgesPK = userBadgesPK;
    }

    /**
     * get users
     */
    @Transient
    public User getUser() {
        return this.getUserBadgePK().user;
    }

    /**
     * set users
     */
    public void setUser(User user) {
        getUserBadgePK().setUser(user);
    }       
    /**
     * get badges
     */
    @Transient
    public Badge getBadge() {
        return getUserBadgePK().badge;
    }

    /**
     * set badges
     */
    public void setBadge(Badge badge) {
        getUserBadgePK().setBadge(badge);
    }   

    /**
     * @return visible
     */
    @Basic
    @Column(name = "VISIBLE")
        public Long getVisible() {
        return visible;
    }

    /**
     * @param visible new value for visible 
     */
    public void setVisible(Long visible) {
        this.visible = visible;
    }

    /**
     * @return datecreated
     */
    @Basic
    @Column(name = "DATECREATED")
        public Date getDateCreated() {
        return dateCreated;
    }

    /**
     * @param datecreated new value for datecreated 
     */
    public void setDateCreated(Date dateCreated) {
        this.dateCreated = dateCreated;
    }


     public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        UserBadge that = (UserBadge) o;

        if (getUserBadgePK() != null ? !getUserBadgePK().equals(that.getUserBadgePK()) : that.getUserBadgePK() != null) return false;

        return true;
    }

    public int hashCode() {
        return (getUserBadgePK() != null ? getUserBadgePK().hashCode() : 0);
    }


    @SuppressWarnings("serial")
    @Embeddable
    public static class UserBadgePK implements Serializable {

        /**
         * Attribute users
         */
         private User user; 


        /**
         * Attribute badges
         */
         private Badge badge;


        /**
         * get users
         */
        @ManyToOne(fetch=FetchType.EAGER)
        public User getUser() {
            return this.user;
        }

        /**
         * set users
         */
        public void setUser(User user) {
            this.user = user;
        }       
        /**
         * get badges
         */
        @ManyToOne(fetch=FetchType.EAGER)
        public Badge getBadge() {
            return this.badge;
        }

        /**
         * set badges
         */
        public void setBadge(Badge badge) {
            this.badge = badge;
        }       

        @Override
         public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;

            UserBadgePK that = (UserBadgePK) o;

            if (badge != null ? !badge.equals(that.badge) : that.badge != null) return false;
            if (user != null ? !user.equals(that.user) : that.user != null)
                return false;

            return true;
        }

        @Override
        public int hashCode() {
            int result;
            result = (badge != null ? badge.hashCode() : 0);
            result = 31 * result + (user != null ? user.hashCode() : 0);
            return result;
        }

    }
}

1 个答案:

答案 0 :(得分:1)

您无法在嵌入对象上创建子标准。创建子标准映射以将连接添加到SQL查询。您不在嵌入字段上创建连接。

另外,请为连接类型使用常量而不是魔术值:

criteria.createCriteria("ub.badge", "b", Criteria.LEFT_JOIN);

更具可读性
criteria.createCriteria("ub.badge", "b", 1);

您没有向我们展示您的实体及其关系,但他的代码可能会更好:

Criteria criteria = userBadgeRepository.getCritieria(); // sic

criteria.createCriteria("userBadgePK.badge", "b", Criteria.INNER_JOIN); 
// you don't have foreign keys pointing to nowhere, have you? 
// So an INNER join is preferred here
criteria.createCriteria("userBadgePK.user", "u", Criteria.INNER_JOIN);