jpa或hibernate如何通过注解定义别的实体中的字段
现有用户类:
@Entity
@Table(name = "tbl_user")
public class User {
private Long id;
private String loginName;
private Long orgId;
private String orgName;
@Id
@GeneratedValue(generator = "user", strategy = GenerationType.TABLE)
@TableGenerator(name = "user", table = "seq_int", pkColumnName = "name", valueColumnName = "value", pkColumnValue = "system_user", allocationSize = 1, initialValue = 1000)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Column(name = "login_name")
public String getLoginName() {
return loginName;
}
public void setLoginName(String loginName) {
this.loginName = loginName;
}
@Column(name = "org_id", insertable = false, updatable = false)
public Long getOrgId() {
return orgId;
}
public void setOrgId(Long orgId) {
this.orgId = orgId;
}
//如何写
public String getOrgName() {
return orgName;
}
public void setOrgName(String orgName) {
this.orgName = orgName;
}
}
用户表tbl_user 与 组织关系表tbl_org 是一对多的关系
tbl_user.org_id = tbl_org.id
现在我想在User类中引入orgName 字段。直接取tbl_org.name 的值,通过jpa或hibernate注解该如何写?
本人不需要再新建Org类,你们懂得。
------解决方案--------------------private Set<User> Org= new HashSet<User>();
@OneToMany(cascade = {CascadeType.REFRESH,CascadeType.REMOVE})
public Set<User> getOrg() {
return Org;
}
你试试看,这样可以不,第一次回帖。。。哈哈