spring - Fetch specific property in hibernate One-to-many relationship -
i have 2 pojo
classes one-to-many relationship
in hibernate
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; }
second customercmsproduct.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; }
here if fetch object of customeraccountenduserorderdetails
class,then customercmsproduct
class , problem here i want specific column of customercmsproduct
table (not default getting all) id , originalprice.
how can projection
here?
in service layer or @ webservice layer( if web project) create 2 different classes other @entity
dto(data transfer objects) helps data transfer 1 layer other.
public class customeraccountenduserorderdetailspojo { private list<customercmsproductpojo> productpojolist = new arraylist<> (); // getter , setter } public class customercmsproductpojo {}
follow below steps
- retrieve
@entity
class data executing query service layer. - iterate on fields , copy required fields pojo layer
- expose data other layers using service method.
this way, can avoid changing custom hibernate behavior linked many parameters cache, 1 many queries fired per iteration.
and also, customization want in layer. hope multi layered project have different layers servers different purpose.
Comments
Post a Comment