我有两个实体--Group和UserGroup,它们与groupId连接。 " \"是因为postgre区分大小写,这样我们就可以纠正这个事实。
@Entity
@Table(name = "\"Group\"")
public class Group {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "\"groupId\"")
private int groupId;
@Column(name = "\"groupName\"")
private String groupName;
@OneToMany(mappedBy = "group")
List<Project> projects;
@OneToMany(mappedBy = "group")
private List<UserGroup> members;
public Group(String groupName) {
this.groupName = groupName;
}
public Group() {
}
public int getGroupId() {
return groupId;
}
public void setGroupId(int groupId) {
this.groupId = groupId;
}
public String getGroupName() {
return groupName;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
}
public List<Project> getProjects() {
return projects;
}
public void setProjects(List<Project> projects) {
this.projects = projects;
}
public List<UserGroup> getMembers() {
return members;
}
public void setMembers(List<UserGroup> members) {
this.members = members;
}
@Override
public String toString() {
return "Group{" +
"groupId=" + groupId +
", groupName='" + groupName + '\'' +
'}';
}
}
和UserGroup
@Entity
@Table(name = "\"UserGroup\"")
@IdClass(GroupAssociationId.class)
public class UserGroup {
@Id
@Column(name = "\"userId\"")
private int userId;
@Id
@Column(name = "\"groupId\"")
private int groupId;
@ManyToOne
@PrimaryKeyJoinColumn(name = "\"userId\"", referencedColumnName = "\"userId\"")
private User member;
@ManyToOne
@PrimaryKeyJoinColumn(name = "\"groupId\"", referencedColumnName = "\"groupId\"")
private Group group;
@ManyToOne
@JoinColumn(name = "\"accessId\"")
private Access access;
public UserGroup(Group group, User member, Access access) {
this.group = group;
this.member = member;
this.access = access;
}
public UserGroup() {
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public int getGroupId() {
return groupId;
}
public void setGroupId(int groupId) {
this.groupId = groupId;
}
public User getMember() {
return member;
}
public void setMember(User member) {
this.member = member;
}
public Group getGroup() {
return group;
}
public void setGroup(Group group) {
this.group = group;
}
public Access getAccess() {
return access;
}
public void setAccess(Access access) {
this.access = access;
}
@Override
public String toString() {
return "UserGroup{" +
"userId=" + userId +
", groupId=" + groupId +
", access=" + access.getAccessName() +
'}';
}
}
当我尝试在表UserGroup中创建一行时出错:
引起:org.postgresql.util.PSQLException:错误:列&#34; group_groupId&#34;关系&#34; UserGroup&#34;不存在
为什么呢?这发生在字符串&#34; em.getTransaction()。commit()上。真的很奇怪。
答案 0 :(得分:0)
在UserGroup表中,有一列:
"group_`groupId`"
已生成(因为您正在使用&#34;&#34;以保持区分大小写。
您可以在postgres中编辑列的名称(以及foreing键):
"group_`groupId`" ---> "group_groupId"
JPA正在寻找group_groupId。
答案 1 :(得分:0)
我设法回答了这个问题。问题在于序列生成。在嵌入式数据库中生成时,我不知道为什么,生成类型序列不起作用。相反,我使用了Identity类型,一切都开始工作了