User.java
private int userId;
@Column(name="status")
private String userStatus;
@ManyToOne
@JoinFormula(value="SELECT l.lookup_key FROM tb_lookup l WHERE l.category ='XXX'", referencedColumnName="status")
private Lookup statusLookup;
Lookup.java
private int lookupId;
@Column(name="category")
private String lookupCategory;
@Column(name="lookup_key")
private String lookupCode;
private String lookupDescription;
@OneToMany(mappedBy="lookupEntity", cascade=CascadeType.ALL, fetch=FetchType.EAGER)
private List<LookupDetail> lookupDetail;
LookupDetail.java
@Id
@Column(name="lookup_detail_id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int lookupDetailId;
@Column(name="language_code")
private String languageCode;
@Column(name="lookup_val")
private String lookupValue;
@ManyToOne
@JoinColumn(name="lookup_id", nullable=false)
private Lookup lookupEntity;
所以基本上我需要为user.java创建多对一的映射注释,以便它可以提供sql查询
select * from User u
inner join Lookup l in u.userStatus = l.lookupCode and l.lookupCategory = 'XXX'
请注意,lookup_code不是primary_key(主键是lookup_id) 它是一个遗留数据库,因此我无法做很多事情。
感谢
答案 0 :(得分:0)
User
实体与非主键列的关联可以@ManyToOne
:
@ManyToOne
@JoinColumn(name="userStatus", referencedColumnName="lookupCode")
public Lookup getLookup() { return lookup; }
现在您的查询如下所示:
select u
from User u
inner join u.lookup l
where lookupCategory = :lookupCategory
如果您想同时包含该联接的两个条件,则必须使用@JoinFormula
:
@ManyToOne
@JoinFormula(value="SELECT l.lookupCode FROM Lookup l WHERE l.lookupCategory ='XXX'", referencedColumnName="userStatus")
public Lookup getLookup() { return lookup; }
这使用辅助SELECT,但由于您使用的是旧版架构,因此没有太多其他选项。