2016-09-07 53 views
0

我在休眠取特定属性处于休眠一一对多的关系

CustomerAccountEnduserOrderDetails.class

@Entity @Table(name="customer_account_enduser_order_details") 
public class CustomerAccountEnduserOrderDetails implements Serializable{ 

private static final long serialVersionUID = 1L; 

@Id 
@GeneratedValue(strategy=GenerationType.AUTO) 
@Column(name="id") 
private Long id; 

@ManyToOne(fetch=FetchType.EAGER) 
@JoinColumn(name = "product_id", insertable = false, updatable = false) 
private CustomerCmsProduct customerCmsProduct; 
} 

其次有两个pojo班,one-to-many relationshipCustomerCmsProduct.class

@Entity 
@Table(name="customer_cms_product") 
@JsonIgnoreProperties(ignoreUnknown = true) 
public class CustomerCmsProduct { 
    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    @Column(name="id") 
    private Long id; 

    @Column(name="name") 
    private String name; 

    @Column(name="offer_price") 
    private String offerPrice; 

    @Column(name="original_price") 
    private String originalPrice; 

    @Column(name="discount") 
    private String discount; 
} 

她e如果我取CustomerAccountEnduserOrderDetails类的对象,那么我会得到CustomerCmsProduct类也,我的问题是,这里我想CustomerCmsProduct的特定列(不是所有默认我收到全部)像只ID和originalPrice。

我怎么能这样做projection在这里?

+1

为什么你需要复杂化hibernate的自动获取策略?为什么不能将其更改为另一个级别,将所有数据库对象转换为UI中的Pojo级别? – VinayVeluri

+0

我dint得到你想说的,你可以给一个例子或提示,如何转换在另一个层面,我在手动想我可以为不需要的字段设置空值,但它是非常昂贵的,如果我加载100对象在一个时间 –

回答

1

在服务层或web服务层(如果这是一个web项目)创建两个不同于@Entity的类作为DTO(数据传输对象),它有助于将数据从一层传输到另一层。

public class CustomerAccountEnduserOrderDetailsPojo { 
    private List<CustomerCmsProductPojo> productPojoList = new ArrayList<>(); 
    // getter and setter 
} 

public class CustomerCmsProductPojo {} 

按照下面的步骤

  • 通过执行从服务层查询检索@Entity类数据。
  • 迭代所有字段并仅将必需字段复制到pojo层
  • 使用服务方法将数据公开到其他图层。

这样,我们可以避免更改自定义休眠行为,因为它与许多参数(如缓存,每次迭代触发的一对多查询)相关联。

而且,在此图层中执行所需的任何自定义。希望这是多层次的项目,你有不同层次的服务器不同的目的。

+0

好吧..但我想知道是否有任何注释,我们可以指定特定的列,我们要检索..迭代和复制是非常昂贵的,性能问题将在那里。 –

+0

AFAIK,我们没有任何。如果可以在许多地方重用,您可以创建基于'AOP'的自定义注释。 – VinayVeluri