3

i have two classes associated with OneToMany - ManyToOne mapping. When i select parent Entity it selects Child entity also but, all child instances assigned to it's every parent instances instead of assign related instances.

PurchaseEntry.java

@Entity
@Table(name="PURCHASE_ENTRY")
public class PurchaseEntry {

    public PurchaseEntry() {
    }
    @Id @Column(name="PURCHASE_ID") @GeneratedValue(strategy=GenerationType.AUTO)
    private long purchaseId;
    @Column(name="INVOICE_NO")
    private String invoiceNo;
    @Column(name="INVOICE_DATE")
    @Type(type="date")
    private Date invoiceDate;          

    @OneToMany(targetEntity=PurchaseDetails.class, cascade=CascadeType.ALL, fetch=FetchType.EAGER)
    @JoinColumn(name="PURCHASE_ID")
    private Collection<PurchaseDetails> purchaseDetailsList = new ArrayList<PurchaseDetails>(); 
}

PurchaseDetails.java

@Entity
@Table(name = "PURCHASE_DETAILS")
public class PurchaseDetails {

    public PurchaseDetails() {
    }

    @Id
    @Column(name = "PURCHASE_DETAIL_ID")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long purchaseDetailId;
    @Column(name = "AMOUNT")
    private float amount;    

    @ManyToOne
    @JoinColumn(name = "PURCHASE_ID")
    private PurchaseEntry purchaseEntry;

}

when saving PurchaseEntry.java object with PurchaseDetails.java instance Collection it works fine but while selecting parent table it selects related child table rows but this related all rows are assigned for every parent class object

select query

Criteria criteria = session.createCriteria(PurchaseEntry.class)
                    .add(Restrictions.between("invoiceDate", fromFilterDate, toFilterDate)).addOrder(Order.asc("invoiceDate"));

            purchaseEntryList = criteria.list();

for example Purchase_Entry table has row with

purchase_id - 1, invoice_date - 18-07-2014

and Purchase_details table has

PURCHASE_DETAIL_ID - 1, purchase_id - 1, ...

PURCHASE_DETAIL_ID - 2, purchase_id - 1, ...

PURCHASE_DETAIL_ID - 3, purchase_id - 1, ...

when i select PurchaseEntry using criteria where invoice_date '18-07-2014' it returns 3 PurchaseEntry objects, in every PurchaseEntry objects purchaseDetailsList has 3 PurchaseDetails objects related with purchase_id = 1

what's wrong with my configuration or any other???

expected : every PurchaseEntry objects has only one related PurchaseDetails instance in purchaseDetailsList

rpandidurai
  • 307
  • 1
  • 5
  • 13

3 Answers3

1

Unfortunately i found ans for this by using below code

@OneToMany(cascade=CascadeType.ALL) 
    @JoinColumn(name="PURCHASE_ID")
    @Fetch(FetchMode.SELECT)
    private Collection<PurchaseDetails> purchaseDetailsList = new ArrayList<PurchaseDetails>();

adding @Fetch(FetchMode.SELECT) working as i expected also i had recreate my schema

rpandidurai
  • 307
  • 1
  • 5
  • 13
0

I think you don't need to give @JoinColumn in PurchaseEntry. No need to give targetEntity also because It can infer to the type.

Try this configuration.

@OneToMany(mappedBy="purchaseEntry", cascade=CascadeType.ALL, fetch=FetchType.EAGER)
    private Collection<PurchaseDetails> purchaseDetailsList = new ArrayList<PurchaseDetails>(); 
bitkot
  • 4,466
  • 2
  • 28
  • 39
  • Amit - nope, it's not working, when i remove @JoinColumn annotation, values not get inserted in that joining column while saving. but no need to specify targetEntity in PurchaseEntry as u said is working normally but my expected result not coming. – rpandidurai Jul 21 '14 at 04:23
0

The Hibernate relational mapping will relate every object with the related Objects, Like if one table is related to another then it will Fetch from both the tables. You can use different fetch strategies to intelligently fetch the data.

In your case,

fetch=FetchType.EAGER

will fetch all the records at the beginning itself, But the use of FetchType.lazy will fetch only when its needed.

You can look into different fetch strategies here for more details.

Community
  • 1
  • 1
Dileep
  • 5,362
  • 3
  • 22
  • 38
  • but i have to call 'purchaseEntryList.get(0).purchaseDetailsList' from my view(JSP) that's why i use 'fetch=FetchType.EAGER' if i use 'FetchType.lazy' when i try to get purchaseDetailsList it throws exception – rpandidurai Jul 21 '14 at 05:12
  • 1
    yes it will throw the exception since the permission to access the database wont 'be there in the jsp. You must understand why it happens, if you want to fetch the data in single query then you must use join query. Else the least you can use is the `SUBSELECT` which is explained in the link i have provided. – Dileep Jul 21 '14 at 06:15