我一直试图通过名为Guardian的中间类来映射两个用户之间的一些“OneToOne”关系。当我尝试检索用户(和他的监护人)时,我从Glassfish(Open Edition v4.0)得到了一个内部服务器错误。但是,日志中没有显示任何类型的堆栈跟踪或任何错误。我怀疑问题是我在JPA课程中的映射。
启动服务器我得到两个与Guardian类相关的警告,我不太明白:
警告:映射在元素[method getGuardianUserBean]上的引用列名[GUARDIAN]与映射引用上的有效id或基本字段/列不对应。将使用提供的引用列名。
警告:映射在元素[method getOwnerUserBean]上的引用列名[OWNER]与映射引用上的有效id或基本字段/列不对应。将使用提供的引用列名称。
SQL create statements:
create table HOMEFREE."user" (
userid integer GENERATED ALWAYS AS IDENTITY,
name varchar(255) not null,
displayname varchar(255) unique not null,
password varchar(255) not null,
tlf integer,
facebookID varchar(255),
googleid varchar(255),
authtoken varchar(255),
email varchar(255) unique not null,
primary key(userid)
);
create table HOMEFREE."guardian" (
guardianId integer GENERATED ALWAYS AS IDENTITY,
owner integer not null,
guardian integer not null,
confirmed boolean not null,
primary key(guardianId),
foreign key(owner) references homeFree."user"(userid),
foreign key(guardian) references homeFree."user"(userid)
);
实体类中的相关字段/注释:
@Entity
@Table(name = "\"user\"", schema = "HOMEFREE")
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int userId;
@OneToMany(mappedBy = "ownerUserBean", fetch = FetchType.EAGER)
private List<Guardian> guardians;
}
@Entity
@Table(name="\"guardian\"", schema="HOMEFREE")
public class Guardian implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int guardianId;
@OneToOne
@PrimaryKeyJoinColumn(name="OWNER", referencedColumnName="USERID")
private User ownerUserBean;
@OneToOne
@PrimaryKeyJoinColumn(name="GUARDIAN", referencedColumnName="USERID")
private User guardianUserBean;
private boolean confirmed;
}
答案 0 :(得分:0)
尝试使用@JoinColumn
代替@PrimaryKeyJoinColumn
。
@OneToOne
@JoinColumn(name="OWNER", referencedColumnName="USERID")
private User ownerUserBean;
@OneToOne
@JoinColumn(name="GUARDIAN", referencedColumnName="USERID")
private User guardianUserBean;
根据规范,后者应该用于将JOINED
映射策略中实体子类的主表连接到其超类的主表(可用的确切定义here)