1

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

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);

用户徽章.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;
        }

    }
}
4

1 回答 1

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);
于 2011-03-02T23:05:55.617 回答