1

我需要在每个查询中完成的CompanyUser.companyRolCompanyUsers具有关系的属性。OneToMany

JPA company_usr 实体:

@Entity
@Table(name = "company_usr")
public class CompanyUser extends BaseModel implements Serializable {

    @Id
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(nullable = false)
    private Company company;
    @Id
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "usr_id", nullable = false)
    private User user;

    @Column(nullable = false)
    private Boolean external;

    @OneToMany(fetch = FetchType.EAGER)
    private List<CompanyRolCompanyUser> companyRolCompanyUsers;

....

JPA companyRol_companyUsr 实体:

@Entity
@Table(name = "companyRol_companyUsr")
public class CompanyRolCompanyUser extends BaseModel implements Serializable {

    @Id
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumns({
        @JoinColumn(name="companyuser_company_id",    referencedColumnName="company_id"),
        @JoinColumn(name="companyuser_usr_id", referencedColumnName="usr_id")
        })
    private CompanyUser companyUser;

    @Id
    @ManyToOne(fetch = FetchType.EAGER)
    private CompanyRol companyRol;

....

如何正确设置mappedBy属性companyRolCompanyUsers

4

2 回答 2

0

如果我得到你想要达到的目标,我认为你需要类似的东西:

@OneToMany(fetch = FetchType.EAGER, mappedBy="companyUser")
private List<CompanyRolCompanyUser> companyRolCompanyUsers;
于 2013-03-22T11:41:54.320 回答
0

同一列可以有两个属性,如下所示:

@JoinColumn(name = "CGRADO_CODIGO", referencedColumnName = "CGRADO_CODIGO")
    @ManyToOne
    @NotFound(action=NotFoundAction.IGNORE)
    private SipreGrado                  sipreGrado;

    @Column(name = "CGRADO_CODIGO",insertable=false,updatable=false)
    private String                      sipreGradoCodigo;

请记住,如果您有时有实体 NULL,您可以使用该注释跳过它

@NotFound(action=NotFoundAction.IGNORE)

另外,记得设置

insertable=false,updatable=false 

对于您不想包含在插入/更新查询中的那个。

于 2015-11-26T17:45:01.107 回答