Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the association path in hibernate?

Consider createAlias method. The docs says: associationPath - A dot-seperated property path. I do not understand what it is? Say that there are 2 tables - Customer (id, name) and Ortder (id, customer_id). Supose that Criteria is created for Customer entity - what association path should be?

  1. "Customer.id = Order.customer_id"
  2. or just "Order" as I saw at some forums?

Could somebody provide several examples for several situations, like left join, inner join and of course join with clause?

like image 639
Cherry Avatar asked Nov 27 '25 21:11

Cherry


1 Answers

Suppose there are 2 Entities (not tables) Customer and Order mapped to the mentioned tables. Class Order has @ManyToOne reference to Customer. You try to search Orders and want to get all orders of a customer with name "Customer1".

Criteria cr=sessioncreateCriteria(Order.class)
        .createAlias("customer", "c", JoinType.INNER_JOIN);

Thus you use "customer" field name of Order entity class and make it alias with name "c".

Then you can add your condition for the alias "c"

cr.add(Restrictions.eq("c.name", customerNameParameter);
like image 93
StanislavL Avatar answered Nov 29 '25 11:11

StanislavL



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!