我正在尝试使用主列以外的列在两个表之间建立一对多关系。我想要Asset.assetnum和SoftLink.assetnum之间的联接。我正在使用休眠4,它总是与主对象之间产生关系。

来自ASSET的this_左外部,在this_.ASSETID = soft3_.ASSETNUM上的SOFTLINKS soft3_

我到底想要什么

来自ASSET的this_左外加入SOFTLINKS soft3_于this_.ASSETNUM = soft3_.ASSETNUM

请建议我是否有任何遗漏。

@Entity
@Table(name="ASSET")
public class Asset {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="ASSETID", nullable = false)
    private int assetid;

    @Column(name="ASSETNUM", nullable = true)
    private String assetnum;

    @OneToMany(fetch = FetchType.EAGER, mappedBy = "asset")
    private List<SoftLinks> softlinks = new ArrayList<SoftLinks>();
}

@Entity
@Table(name="SOFTLINK")
public class SoftLinks {
    @Id
    @Column(name="SOFTLINKID", nullable = false)
    private String softlinkid;

    @Column(name="ASSETNUM", nullable = true)
    private String assetnum;

    @ManyToOne
    @JoinColumn(name = "ASSETNUM", insertable = false, updatable = false)
    private Asset asset;
}

最佳答案

SoftLinks类中,您需要将@ManyToOne关联更改为:

@ManyToOne
@JoinColumn(name = "ASSETNUM", referencedColumnName = "ASSETNUM", insertable = false, updatable = false)
private Asset asset;

10-06 13:34