0

我有以下表格:

Login -> (id, username, password)
Members -> (member_id, name, address)

id 与 member_id 具有一对一的映射关系。

我写了两个模型

登录型号:

@Entity
@Table(name = "Login", uniqueConstraints = { @UniqueConstraint(columnNames = "username") })
public class Login {

private int id;
private String username;
private String password;
private Members members;

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

@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, optional = true)
@JoinColumn(name = "id", referencedColumnName = "member_id")
public Members getMembers() {a
    return members;
}

}

会员型号:

@Entity
@Table(name = "Members")
public class Members {
private int member_id;
private String name;
private String address;

@Id
@GeneratedValue(generator = "gen")
@GenericGenerator(name = "gen", strategy = "foreign", parameters = { @Parameter(name = "property", value = "Login") })
@Column(name = "member_id")
public int getMember_id() {
    return member_id;
}

private Login login;

@OneToOne(fetch = FetchType.LAZY, optional = true, cascade = CascadeType.ALL)
public Login getLogin() {
    return login;
}

}

为简单起见,我删除了一些 getter 和 setter。无论如何,你能告诉我我哪里错了吗?在更改注释的值时,我得到了诸如未找到映射、未知持久类等错误。

错误

org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建类路径资源 [database/Hibernate.xml] 中定义的名称为“sessionFactory”的 bean 时出错:调用 init 方法失败;嵌套异常是 org.hibernate.AnnotationException: @OneToOne 或 @ManyToOne on org.sparsham.login.model.Login.members 引用了一个未知实体:org.sparsham.members.model.Members org.springframework.web.servlet.FrameworkServlet。 processRequest(FrameworkServlet.java:894) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778) javax.servlet.http.HttpServlet.service(HttpServlet.java:621) javax.servlet.http.HttpServlet。服务(HttpServlet.java:722)

4

1 回答 1

1

希望这可以解决您的最后一个问题。试试这些改变。

登录模式:

@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, optional = true)
@PrimaryKeyJoinColumn
public Members getMembers() {a
    return members;
}

会员型号:

@Id
@GeneratedValue(generator = "gen")
@GenericGenerator(name = "gen", strategy = "foreign", parameters = { @Parameter(name =          "property", value = "login") })
@Column(name = "member_id")
public int getMember_id() {
    return member_id;
}

private Login login;

@OneToOne(fetch = FetchType.LAZY, optional = true, cascade = CascadeType.ALL, mappedBy="members")
@PrimaryKeyJoinColumn
public Login getLogin() {
    return login;
}

注意@GenericGenerator 注解中的@Parameter 部分,“login”可能区分大小写。

于 2012-05-30T08:10:16.667 回答