1

我正在为一对多映射进行概念验证,但没有成功。我的架构如下:用户 -----> UserRole

import static javax.persistence.GenerationType.IDENTITY;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="UserRole")
public class UserRole {

    private int userRoleId;
    private String userRoleName;

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "UserRoleId", unique = true, nullable = false)
    public int getUserRoleId() {
        return userRoleId;
    }

    public void setUserRoleId(int userRoleId) {
        this.userRoleId = userRoleId;
    }

    @Column(name = "UserRoleName", unique = true, nullable = false)
    public String getUserRoleName() {
        return userRoleName;
    }

    public void setUserRoleName(String userRoleName) {
        this.userRoleName = userRoleName;
    }

}

这里是 User 类的一部分

    @Entity
    @Table(name="Users")
    public class User {
    private int userId;
    private String firstName;
    private String lastName;
    private String userName;
    private String password;
    private String phone;
    private String email;
    private UserRole userRole;
    private List<Meeting> meetings;
    private List<MeetingInvitation> meetingInvitationsSend;
    private List<MeetingInvitation> meetingInvitationsReceived;

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "UserId", unique = true, nullable = false)
    public int getUserId() {
        return userId;
    }
         .... 

    @ManyToOne(targetEntity=UserRole.class)
    @JoinColumn (name="userRole")
    public UserRole getUserRole() {
        return userRole;
    }

    public void setUserRole(UserRole userRole) {
        this.userRole = userRole;
    }

}

我收到此错误 Initial SessionFactory creation failed.org.hibernate.AnnotationException: @OneToOne or @ManyToOne on User.userRole references an unknown entity: UserRole

你可以看到我在用户角色类中添加了@Entity 注释

4

1 回答 1

5

您可能只是忘记在您的休眠配置文件中声明该类:

<mapping class="com.foo.bar.UserRole"/>

另外,请注意,这targetEntity=UserRole.class是不必要的,因为您的 getter 返回 a UserRole。Hibernate 会从 getter 的返回类型推导出目标实体。

于 2012-06-04T10:45:22.607 回答