我有一个db视图,它返回一个数据集,如下所示
-----------------------------------------------------------------------------
| customer_id | order_id | order_detail_id | product_id | product_name |
-----------------------------------------------------------------------------
| 1 | 10 | 101 | 1011 | P1 |
-----------------------------------------------------------------------------
| 1 | 10 | 102 | 1012 | P2 |
-----------------------------------------------------------------------------
| 2 | 20 | 201 | 1011 | P1 |
-----------------------------------------------------------------------------
| 2 | 20 | 202 | 1012 | P2 |
-----------------------------------------------------------------------------
| 2 | 30 | 301 | 1011 | P1 |
-----------------------------------------------------------------------------
我想将其上传到对象图中,如下所示
@Entity
@Table (name= "vw_all_customers_with_orders")
public Class Customer {
protected String customerId;
@ElementCollection
protected Set<Order> orders;
}
@Embeddable
public Class Order {
@Column(name = "order_id", nullable = false, insertable = false, updatable = false)
protected String orderId;
@ElementCollection
protected Set<OrderItem> orderItems;
}
@Embeddable
public Class OrderItem{
@Column(name = "order_detail_id", nullable = false, insertable = false, updatable = false)
protected String orderDetailId;
@ElementCollection
protected Set<Product> products;
}
@Embeddable
public Class Product{
@Column(name = "product_id", nullable = false, insertable = false, updatable = false)
protected String productId;
}
这种剂量似乎无论如何都有效。我正在使用JPA2.1和hibernate 4.3.7
我哪里错了?
答案 0 :(得分:0)
由于我的声誉很低,我无法发表评论。而且我不确定我的回答
代码是不完整的。因为实体始终具有主键但客户没有@ID。 对于Set中的可嵌入对象,我们需要另一个表
@Entity
public class User {
[...]
public String getLastname() { ...}
@ElementCollection
@CollectionTable(name="Addresses", joinColumns=@JoinColumn(name="user_id"))
@AttributeOverrides({
@AttributeOverride(name="street1", column=@Column(name="fld_street"))
})
public Set<Address> getAddresses() { ... }
}
@Embeddable
public class Address {
public String getStreet1() {...}
[...]
}
答案 1 :(得分:0)
两个实体对象不应具有相同的主键值,在您的情况下,customer_id具有相同的值。从这个角度来看,我们不能制作实体对象列表。我在商店程序上尝试了同样的事情。对数据库存储过程fire查询,然后将结果发送到实体对象
@NamedNativeQuery( name = NamedQueryConstants.STORE_PROC_CA_AWS_HIT_WEB_BY_TOC, query =&#34; SPGET_WEB_ACTIVE_USER:fromAccessDate,:endAccessDate,:activeDays,:accessType,:tocCode&#34;, resultClass = FgraCaHitWebInfo.class )
如果我为两个实体发送相同的主键,则会出现错误。如果我发送不同的主键,则错误未到来。我在商店程序上尝试了同样的事情。对数据库存储过程fire查询,然后将结果发送到实体对象