2012-06-04 297 views
1

我正在做一對多的映射的概念證明沒有成功。我的架構如下:用戶-----> UseRole休眠多對一映射

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

} 

,我得到這個錯誤 初始SessionFactory的創建failed.org的一部分。 hibernate.AnnotationException:上User.userRole @OneToOne或@ManyToOne引用了未知的實體:UserRole的

屁股,你可以看到我加了註解@Entity在用戶角色類

回答

5

你可能只是忘了申報在Hibernate的配置文件中的類:

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

另外,還要注意的是targetEntity=UserRole.class不必要的,因爲你的getter返回UserRole。 Hibernate會從getter的返回類型中推導出目標實體。

+0

的確我只是通過我無法提交答案才發現它自己,但很多很多謝謝 –