【问题标题】:One-to-many relationship Hibernate error [duplicate]一对多关系休眠错误[重复]
【发布时间】:2012-07-02 13:42:08
【问题描述】:

可能重复:
Hibernate problem - “Use of @OneToMany or @ManyToMany targeting an unmapped class”

我正在让 Spring 集成 Hibernate 项目。但是我在项目中添加了一对多但出现错误

用户.java

    @Entity
    @Table(name = "UserLogin")
  public class User implements Serializable {

private static final long serialVersionUID = -3652559447682574722L;

private int id;
private String username;

private String password;

private Set<EmployeeLeave> leaveInfo = new HashSet<EmployeeLeave>();

public User() {
}

public User(String username, String password, int id) {
this.username = username;
this.password = password;
this.id = id;
}

@Column(name = "username", nullable = false)
public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

@Column(name = "password", nullable = false)
public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO)
public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

@Override
public String toString() {
return "User(" + username + ")";
}

public String UserAuthentification() {

return "Success";

}

/*
 * @OneToMany(cascade = CascadeType.ALL)
 * 
 * @JoinTable(name = "employee_leave", joinColumns = { @JoinColumn(name = "id") }, inverseJoinColumns = {
 * 
 * @JoinColumn(name = "employeeId") })
 */
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "user")
public Set<EmployeeLeave> getLeaveInfo() {
return leaveInfo;
}

public void setLeaveInfo(Set<EmployeeLeave> leaveInfo) {
this.leaveInfo = leaveInfo;
}

}

Employeeleave.java

        @Entity
     @Table(name = "employeeLeave")
     public class EmployeeLeave implements Serializable {

/**
 * 
 */

private static final long serialVersionUID = 1L;

private Long employeeId;
private Date startDate;
private Date endDate;
private String reason;
private Long totalSickLeave;
private Long totalPaidoffLeave;
private String status = "Waiting";
private User user;

@Id
@Column(name = "employeeId")
public Long getEmployeeId() {
return employeeId;
}

public void setEmployeeId(Long employeeId) {
this.employeeId = employeeId;
}

@Column(name = "startdate", nullable = false)
public Date getStartDate() {
return startDate;
}

public void setStartDate(Date startDate) {
this.startDate = startDate;
}

@Column(name = "enddate", nullable = false)
public Date getEndDate() {
return endDate;
}

public void setEndDate(Date endDate) {
this.endDate = endDate;
}

@Column(name = "reason", nullable = false)
public String getReason() {
return reason;
}

public void setReason(String reason) {
this.reason = reason;
}

@Column(name = "totalsickleave", nullable = false, columnDefinition = "bigint default 15")
public Long getTotalSickLeave() {
return totalSickLeave;
}

public void setTotalSickLeave(Long totalSickLeave) {
this.totalSickLeave = totalSickLeave;
}

@Column(name = "totalpaidoffleave", nullable = false, columnDefinition = "bigint default 15")
public Long getTotalPaidoffLeave() {
return totalPaidoffLeave;
}

public void setTotalPaidoffLeave(Long totalPaidoffLeave) {
this.totalPaidoffLeave = totalPaidoffLeave;
}

@Column(name = "status", nullable = false, columnDefinition = "TEXT")
public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "id", nullable = false)
@ForeignKey(name = "fk_authorities_users")
public User getUser() {
return user;
}

public void setUser(User user) {
this.user = user;
}
}

离开App.xml

       <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<!-- LeaveAppication business object -->
<bean id="leaveBo" class="com.nagra.leave.UserAutenBo.LeaveUserInfoBoImpl">
    <property name="leaveuserDao" ref="leaveuserDao" />
</bean>

<!-- LeaveAppication Data Access Object -->
<bean id="leaveuserDao" class="com.nagra.leave.UserAuthe.Dao.LeaveUserinfoDaoImpl">
    <property name="sessionFactory" ref="sessionFactory"></property>
</bean>


 </beans>

我收到以下错误:

Caused by: org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany   targeting an unmapped class: com.nagra.leave.User.leaveInfo[com.nagra.leave.EmployeeLeave]

【问题讨论】:

    标签: hibernate integration spring-webflow


    【解决方案1】:

    您的会话工厂应该列出 Hibernate 映射的所有类。鉴于错误,您只是忘记将 com.nagra.leave.EmployeeLeave 添加到此列表!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-17
      • 2015-12-22
      • 1970-01-01
      相关资源
      最近更新 更多