snippetjournal

– notes, snippet codes, and my journal

Hibernate Criteria : One To Many and Many To One

leave a comment »

Berikut ini snippet codes dari penggunaan hibernate criteria dengan kasus relation/cardinalitas one to many dan many to one.

Terdapat model/entity sebagai berikut :


....
 private Company company;
 private String isActive;
 private String proposalCode;
 private String proposalName;
 private Set<ProspectProposalManPower> prospectProposalManPowers = new HashSet<ProspectProposalManPower>(0);
 private Set<ProspectProposalCost> prospectProposalCosts = new HashSet<ProspectProposalCost>(0);

....

getter - setter

....
@ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="company_id")
    public Company getCompany() {
        return this.company;
    }

....

 @OneToMany(fetch = FetchType.LAZY, mappedBy = "prospectProposal")
    @Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE)
    public Set<ProspectProposalManPower> getProspectProposalManPowers() {
        return this.prospectProposalManPowers;
    }
....


Untuk Dao implementasi nya sebagai berikut:

public List<ProspectProposal> getAllDataActive(final Map searchParam, final int firstResult, final int maxResult, final boolean asc, final String sortField) {
 Object o;
 o = getHibernateTemplate().execute(new HibernateCallback() {

@Override
 public Object doInHibernate(Session sn) throws HibernateException, SQLException {
 Criteria criteria = sn.createCriteria(ProspectProposal.class);

 criteria.createAlias("prospectProposalManPowers", "manpower", Criteria.LEFT_JOIN );
 criteria.createAlias("prospectProposalCosts", "cost", Criteria.LEFT_JOIN);

criteria = searchFilters(criteria, searchParam);

 criteria.add(Restrictions.isNotNull("kode"));
 criteria.add(Restrictions.eq("isActive", "Y"));
 criteria.setFetchMode("company", FetchMode.JOIN);

if(asc == true){
 criteria.addOrder(Order.asc(sortField));
 }else{
 criteria.addOrder(Order.desc(sortField));
 }

 criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
 criteria.setFirstResult(firstResult);
 criteria.setMaxResults(maxResult);

return criteria.list();
 }
 });

 return (List<ProspectProposal>)o;

 }

 protected Criteria searchFilters(Criteria criteria, Map searchParam){

 if(searchParam.get("searchCode") != null && !searchParam.get("searchCode").equals("")){
 criteria.add(Restrictions.like("proposalCode", searchParam.get("searchCode").toString(), MatchMode.ANYWHERE));
 }

 if(searchParam.get("searchName") != null && !searchParam.get("searchName").equals("")){
 criteria.add(Restrictions.like("proposalName", searchParam.get("searchName").toString(), MatchMode.ANYWHERE));
 }

 if(searchParam.get("searchDecsMaker") != null && !searchParam.get("searchDecsMaker").equals("")){
 criteria.add(Restrictions.like("decisionMaker", searchParam.get("searchDecsMaker").toString(), MatchMode.ANYWHERE));
 }

 if(searchParam.get("searchCompany") != null && (Integer)searchParam.get("searchCompany") != 0){
 criteria.add(Restrictions.eq("company.kode", searchParam.get("searchCompany")));
 }

 if(searchParam.get("searchEmployee") != null && (Integer)searchParam.get("searchEmployee") != 0){
 criteria.add(Restrictions.eq("manpower.employee.kode", searchParam.get("searchEmployee")));
 }

 if(searchParam.get("searchCurrency") != null && (Integer)searchParam.get("searchCurrency") != 0){
 criteria.add(Restrictions.eq("cost.currency.kode", searchParam.get("searchCurrency")));
 }

 return criteria;
 }

codes: criteria.setFetchMode(“company”, FetchMode.JOIN);
Penggunaan FetchMode.JOIN digunakan agar entity company (many to one) di ikut sertakan dalam pengambilan data untuk menghindari Lazy Initialization

codes :criteria.createAlias(“prospectProposalManPowers”, “manpower”, Criteria.LEFT_JOIN );
Penggunaan Criteria.LEFT_JOIN digunakan agar relasi dari entity one to many ikut disertakan dalam query.

codes: criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
Penggunaan Criteria.DISTINCT_ROOT_ENTITY untuk menanggulangi pemakaian left join yang akan mengakibatkan data terduplikasi.

Written by snippetjournal

June 20, 2013 at 12:16 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: