我正在将SQL视图映射到带有休眠@Subselect
注释的实体类。
基本上,它看起来像这样:
@Subselect(
"SELECT table1.*, table2.id as tid FROM "
+ "table1 INNER JOIN table2 on table2.field = table1.field"
)
@Entity
@Immutable
class Entity {
// fields
}
加入后,我可能会收到类似以下的内容
========================================
| table1.id | table1.field | table2.id |
========================================
| 1 | 1 | 1 |
========================================
| 1 | 1 | 2 |
========================================
因此table2中的几条记录可以连接到table1中的一行。很好,但是在Java实体中,我想将其映射为一对多关系(一个实体映射到多个table2实体),这是我写的,适用于其他类型的关系:
@Subselect(
"SELECT table1.*, table2.id as tid FROM "
+ "table1 INNER JOIN table2 on table2.field = table1.field"
)
@Entity
@Immutable
class Entity {
@OneToMany
@JoinColumn(name = "tid", updatable = false, insertable = false)
private Set<Table2Entity> elements = new HashSet<>();
}
但是,实体中的集合始终为空,为什么呢? 上述方法适用于一对一和多对一关系。
答案 0 :(得分:0)
转起来,不需要与sql联接即可为视图实体带来一对多的关系。我是这样解决的:
Invocation.Builder invocationBuilder3 = targetWithParams.request(new String[] { "application/hal+json" });
invocationBuilder3.header("Authorization", "Bearer " + accessToken);
invocationBuilder3.header("accept", "application/json");
invocationBuilder3.header("content-type", "multipart/form-data");
Form form = new Form().param("sip", "0801752b800ee8a6_864deeb87909431896d7a861eecdda98.zip")
.param("format", "sip_zip");
final FileDataBodyPart filePart = new FileDataBodyPart("sip", new File("0801752b800ee8a6_864deeb87909431896d7a861eecdda98.zip"));
System.out.println("name:"+filePart.getName());
FormDataMultiPart formDataMultiPart = new FormDataMultiPart();
final FormDataMultiPart multipart = (FormDataMultiPart) formDataMultiPart.field("format", "sip_zip").bodyPart(filePart);
Response response = invocationBuilder3.post(Entity.entity(multipart.toString().getBytes(), MediaType.MULTIPART_FORM_DATA_TYPE));
我只需要在需要连接的表上放置列名。