Hibernate复合表和关键问题

时间:2011-02-09 22:13:01

标签: oracle hibernate spring-mvc annotations composite-key

我试图让hibernate使用复合表。基本上有一个用户表,角色表和一个名为userroles的组合表,它们连接两者。我从获得角色的用户获得的查询急切地输出以下内容。我不断收到stackoverflow错误,或null异常。我的问题是为什么这些列在Oracle中产生两个userid和roleid输出?

 select
    this_.ID as ID0_1_,
    this_.DATECREATED as DATECREA2_0_1_,
    this_.EMAIL as EMAIL0_1_,
    this_.ENABLED as ENABLED0_1_,
    this_.FIRSTNAME as FIRSTNAME0_1_,
    this_.LASTNAME as LASTNAME0_1_,
    this_.PASSWORD as PASSWORD0_1_,
    this_.SALT as SALT0_1_,
    this_.USERNAME as USERNAME0_1_,
    userroles2_.USERID as USERID3_,
    userroles2_.ROLEID as ROLEID3_,
    userroles2_.ROLEID as ROLEID3_0_,
    userroles2_.USERID as USERID3_0_ 
from
    CISCO.USERS this_ 
inner  join
    CISCO.USERROLES userroles2_ 
        on this_.ID=userroles2_.USERID 
where
    this_.USERNAME='mike'

我的课程如下。

User.java

@Entity
@Table(name = "USERS", schema = "CISCO")
@SuppressWarnings("serial")
public class User implements Serializable {

    /**
     * Attribute id.
     */
    private long id;

    /**
     * Attribute username.
     */
    private String username;

    /**
     * Attribute password.
     */
    private String password;

    /**
     * Attribute enabled.
     */
    private Long enabled;

    /**
     * Attribute salt.
     */
    private String salt;

    /**
     * Attribute first name.
     */
    private String firstName;

    /**
     * Attribute last name.
     */
    private String lastName;

    /**
     * Attribute email.
     */
    private String email;

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

    /**
     * @return id
     */
    @Basic
    @Id
    @Column(name = "ID")
        public long getId() {
        return id;
    }

    /**
     * @param username new value for id 
     */
    public void setId(long id) {
        this.id = id;
    }

    /**
     * @return username
     */
    @Basic
    @Column(name = "USERNAME", length = 50)
        public String getUsername() {
        return username;
    }

    /**
     * @param username new value for username 
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * @return password
     */
    @Basic
    @Column(name = "PASSWORD", length = 50)
        public String getPassword() {
        return password;
    }

    /**
     * @param password new value for password 
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @return enabled
     */
    @Basic
    @Column(name = "ENABLED")
        public Long getEnabled() {
        return enabled;
    }

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

    /**
     * @return salt
     */
    @Basic
    @Column(name = "SALT", length = 25)
        public String getSalt() {
        return salt;
    }

    /**
     * @param salt new value for salt 
     */
    public void setSalt(String salt) {
        this.salt = salt;
    }

    /**
     * @return first name
     */
    @Basic
    @Column(name = "FIRSTNAME", length = 100)
        public String getFirstName() {
        return firstName;
    }

    /**
     * @param first name new value for first name 
     */
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    /**
     * @return last name
     */
    @Basic
    @Column(name = "LASTNAME", length = 100)
        public String getLastName() {
        return lastName;
    }

    /**
     * @param last name new value for last name 
     */
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    /**
     * @return email
     */
    @Basic
    @Column(name = "EMAIL", length = 50)
        public String getEmail() {
        return email;
    }

    /**
     * @param email new value for email
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * @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;
    }

    private List<UserRole> userRoles;

    /**
     * Get the list of User Roles
     */
     @OneToMany(fetch=FetchType.EAGER, mappedBy="userRolePK.user")
     public List<UserRole> getUserRoles() {
         return this.userRoles;
     }

    /**
     * Set the list of User Roles
     */
     public void setUserRoles(List<UserRole> userRoles) {
         this.userRoles = userRoles;
     }
}

Role.java

@Entity
@Table(name = "ROLES", schema = "CISCO")
@SuppressWarnings("serial")
public class Role implements Serializable {

    /**
     * Attribute id.
     */
    private Long id;

    /**
     * Attribute name.
     */
    private String name;

    /**
     * List of Userroles
     */
    private List<UserRole> userRoles = null;


    /**
     * @return id
     */
    @Basic
    @Id
    @Column(name = "ID")
        public Long getId() {
        return id;
    }

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

    /**
     * @return name
     */
    @Basic
    @Column(name = "NAME", length = 20)
        public String getName() {
        return name;
    }

    /**
     * @param name new value for name 
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * Get the list of Userroles
     */
     @OneToMany(fetch=FetchType.LAZY, mappedBy="userRolePK.role")
     public List<UserRole> getUserRoles() {
        return this.userRoles;
     }

    /**
     * Set the list of Userroles
     */
     public void setUserRoles(List<UserRole> userRoles) {
        this.userRoles = userRoles;
     }


}

UserRole.java

@Entity
@Table(name = "USERROLES", schema = "CISCO")
@SuppressWarnings("serial")
public class UserRole implements Serializable {

    /**
     * Primary key
     */
    private UserRolePK userRolePK;


    /**
     * Get the primary key
     */
    @Basic
    @Id
    public UserRolePK getUserRolePK() {
        return this.userRolePK;
    }

    /**
     * set the primary key
     */
    public void setUserRolePK(UserRolePK userRolePK) {
        this.userRolePK = userRolePK;
    }

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

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


        /**
         * Attribute roles
         */
         private Role role; 


    /**
     * get users
     */
            @ManyToOne(fetch=FetchType.LAZY)
            @JoinColumn(name = "USERID")
            public User getUser() {
                return this.user;
            }

            /**
             * set users
             */
            public void setUser(User user) {
                this.user = user;
            }       
    /**
     * get roles
     */
            @ManyToOne(fetch=FetchType.LAZY)
            @JoinColumn(name = "ROLEID")
            public Role getRole() {
                return this.role;
            }

            /**
             * set roles
             */
            public void setRole(Role roles) {
                this.role = role;
            }       

        /**
         * calculate hashcode
         */
        @Override
        public int hashCode()
        {
            //TODO : implement this method
            return super.hashCode();
        }

        /**
         * equals method
         */
        @Override
        public boolean equals(Object object)
        {
            //TODO : implement this method
            return super.equals(object);
        }

    }

}

获取数据的调用是

 public List<T> findByCriteria(Criteria criteria, List<Criterion> criterions, IList list) {

        if(criterions != null)
        {
            for(Criterion c : criterions)
            {
                criteria.add(c);
            }
        }

        criteria.setProjection(Projections.rowCount());

        list.setTotal(((Integer)criteria.uniqueResult()).intValue());

        logger.debug("Count:" + list.getTotal());

        if(list.getTotal() > 0)
        {
            criteria.setProjection(null);
            criteria.setResultTransformer(Criteria.ROOT_ENTITY);
            criteria.setFirstResult((list.getPage() - 1) * list.getPageSize()).setMaxResults(list.getPageSize());

            if(list.getSortBy() != null && list.getSortBy().length() > 0) {

                Order order = null;

                if(list.getOrderBy().equalsIgnoreCase("asc"))
                    order = Order.asc(list.getSortBy());
                else
                    order = Order.desc(list.getSortBy());

                criteria.addOrder(order);
            }

            return criteria.list();
        }

        return new ArrayList<T>();

}

2 个答案:

答案 0 :(得分:0)

我不确定这个奇怪查询的原因,但我想知道为什么你将连接表映射为实体而不是@ManyToMany

@Entity 
@Table(name = "USERS", schema = "CISCO") 
@SuppressWarnings("serial") 
public class User implements Serializable {
    ...
    @ManyToMany
    @JoinTable(name = "USERROLES", 
        joinColumns = @JoinColumn(name = "USERID"),
        inverseJoinColumns = @JoinColumn(name = "ROLEID")
    )
    public List<Role> getRoles() { ... }
    public void setRoles(List<Role> roles) { ... }
} 

@Entity  
@Table(name = "ROLES", schema = "CISCO")
@SuppressWarnings("serial")
public class Role implements Serializable { 
    ...

    // I'm not sure if you need this relationship to be bidirectional, 
    // but if you actually need here is another side
    @ManyToMany(mappedBy = "roles")
    public List<User> getUsers() { ... }
    public void setUsers(List<User> users) { ... }
}

另见:

答案 1 :(得分:0)

我基本上走了那条路。我使用默认模式Spring表示用于默认设置。我继续创建了一个如上所述的Users / Roles / UserRoles表。下面是我在User.cs类中的内容,它正确地加入了它。

 @OneToMany(fetch=FetchType.EAGER)
 @JoinTable(
        name="UserRoles",
        joinColumns = @JoinColumn( name="USERID"),
        inverseJoinColumns = @JoinColumn( name="ROLEID")
)
 public List<Role> getRoles() {
    return this.roles;
 }