我在一对多的关系中遇到了麻烦。我有一个用户,user_company(将一个用户与一个公司相关联,一个用户可以有几个公司)和一个用户角色(一个用户可以在不同的公司有不同的角色)。问题是它只加载了第一个用户的用户角色。如果我测试任何其他用户,则用户角色为空。真的很奇怪,我有 10 个用户,只有第一个有效。这是代码:
用户:
@Id
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 14)
@Column(name = "user_name")
private String userName;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 14)
@Column(name = "user_password")
private String userPassword;
@Size(max = 1)
@Column(name = "user_logged")
private String userLogged;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "userCompanyUserName")
private Collection<UserCompany> userCompanyCollection;
用户公司:
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "user_company_id")
private Integer userCompanyId;
@JoinColumn(name = "user_company_user_name", referencedColumnName = "user_name")
@ManyToOne(optional = false)
private Users userCompanyUserName;
@JoinColumn(name = "user_company_company", referencedColumnName = "company_id")
@ManyToOne(optional = false)
private Companies userCompanyCompany;
/*
@JoinColumns({
@JoinColumn(name = "user_company_user_roles", referencedColumnName = "user_role_id"),
@JoinColumn(name = "user_company_id", referencedColumnName = "user_role_company_id", insertable = false, updatable = false)
})*/
@JoinColumns({
@JoinColumn(name = "user_company_user_roles", referencedColumnName = "user_role_id"),
@JoinColumn(name = "user_company_id", referencedColumnName = "user_role_company_id", insertable = false, updatable = false)
})
@ManyToOne(optional = false)
private UserRoles userCompanyUserRoles;
用户角色:
@OneToMany(cascade = CascadeType.ALL, mappedBy = "userCompanyUserRoles")
private Collection<UserCompany> userCompanyCollection;
private static final long serialVersionUID = 1L;
@EmbeddedId
protected UserRolesPK userRolesPK = new UserRolesPK();
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 50)
@Column(name = "user_role_name")
private String userRoleName;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 200)
@Column(name = "user_role_description")
private String userRoleDescription;
关于数据:我有 10 个用户,叫:test1,test2,test3.... 我有 user_company 的 10 条记录。它们将所有用户与 company1 和 userrole1 相关联。10 的数据基本相同,这就是为什么我认为只有 test1 有效的原因很奇怪。
提前感谢所有帮助。
问候,丹尼尔