使用原生 SQL 将外部列加入到 Hibernate 实体
Join external column to Hibernate entity using native SQL
我有一个(简化的)table 结构,看起来像这样:
客户table:
id name
-------------------
1 customer1
别名table:
customer_id alias
-------------------------------
1 customer one
1 customer uno
当我 运行 以下查询时,我很容易获得每个客户的别名列表:
select * from customer_alias where customer_id=1;
我想在我的休眠中使用这个查询来填充类型 String
的列表。我尝试使用 @Formula
如下:
@Entity
@Table(name = "customer")
public class Customer {
@Id
@Column(name = "id")
@GeneratedValue(strategy= GenerationType.AUTO)
private Long id;
@Column(name="name")
private String name;
@Formula("(select alias from customer_alias where customer_id = id)")
private List<String> aliases;
// Getters, setters, etc...
}
它没有用,我得到了这个异常:
Could not determine type for: java.util.List, at table: customer, for columns: [org.hibernate.mapping.Formula( (select alias from customer_alias where customer_id = id) )]
有什么办法可以实现吗?当然不必与 @Formula
一起使用。任何合理的方式都会很好。
这是我的例子SQLFiddle
您可以使用 @ElementCollection
获得相关别名列表,而无需映射整个实体:
@ElementCollection
@CollectionTable(name = "customer_alias", joinColumns = @JoinColumn(name = "customer_id") )
@Column(name = "alias")
private List<String> aliases;
另请参阅:
- Difference between @OneToMany and @ElementCollection?
我认为你不想使用 OneToMany
注释,因为第二个 table 只是一个字符串列表,你想找到更优雅的东西,不需要我创建另一个实体.
您可以使用 @ElementCollection
如下:
@Entity
@Table(name="college")
class College implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name="college_id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int collegeId;
@Column(name="name")
private String collegeName;
@ElementCollection
@CollectionTable(name="student", joinColumns=@JoinColumn(name="college_id"))
@Column(name="student_name")
private Set<String> students;
public College() {
}
public Set<String> getStudents() {
return students;
}
public void setStudents(Set<String> students) {
this.students = students;
}
public int getCollegeId() {
return collegeId;
}
public void setCollegeId(int collegeId) {
this.collegeId = collegeId;
}
public String getCollegeName() {
return collegeName;
}
public void setCollegeName(String collegeName) {
this.collegeName = collegeName;
}
@Override
public String toString() {
return "College [collegeId=" + collegeId + ", collegeName=" + collegeName + ", students=" + students + "]";
}
}
我认为 @Formula
注解不支持集合,它只能应用于单值属性。不能说是否有任何调整。
我有一个(简化的)table 结构,看起来像这样:
客户table:
id name
-------------------
1 customer1
别名table:
customer_id alias
-------------------------------
1 customer one
1 customer uno
当我 运行 以下查询时,我很容易获得每个客户的别名列表:
select * from customer_alias where customer_id=1;
我想在我的休眠中使用这个查询来填充类型 String
的列表。我尝试使用 @Formula
如下:
@Entity
@Table(name = "customer")
public class Customer {
@Id
@Column(name = "id")
@GeneratedValue(strategy= GenerationType.AUTO)
private Long id;
@Column(name="name")
private String name;
@Formula("(select alias from customer_alias where customer_id = id)")
private List<String> aliases;
// Getters, setters, etc...
}
它没有用,我得到了这个异常:
Could not determine type for: java.util.List, at table: customer, for columns: [org.hibernate.mapping.Formula( (select alias from customer_alias where customer_id = id) )]
有什么办法可以实现吗?当然不必与 @Formula
一起使用。任何合理的方式都会很好。
这是我的例子SQLFiddle
您可以使用 @ElementCollection
获得相关别名列表,而无需映射整个实体:
@ElementCollection
@CollectionTable(name = "customer_alias", joinColumns = @JoinColumn(name = "customer_id") )
@Column(name = "alias")
private List<String> aliases;
另请参阅:
- Difference between @OneToMany and @ElementCollection?
我认为你不想使用 OneToMany
注释,因为第二个 table 只是一个字符串列表,你想找到更优雅的东西,不需要我创建另一个实体.
您可以使用 @ElementCollection
如下:
@Entity
@Table(name="college")
class College implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name="college_id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int collegeId;
@Column(name="name")
private String collegeName;
@ElementCollection
@CollectionTable(name="student", joinColumns=@JoinColumn(name="college_id"))
@Column(name="student_name")
private Set<String> students;
public College() {
}
public Set<String> getStudents() {
return students;
}
public void setStudents(Set<String> students) {
this.students = students;
}
public int getCollegeId() {
return collegeId;
}
public void setCollegeId(int collegeId) {
this.collegeId = collegeId;
}
public String getCollegeName() {
return collegeName;
}
public void setCollegeName(String collegeName) {
this.collegeName = collegeName;
}
@Override
public String toString() {
return "College [collegeId=" + collegeId + ", collegeName=" + collegeName + ", students=" + students + "]";
}
}
我认为 @Formula
注解不支持集合,它只能应用于单值属性。不能说是否有任何调整。