Question

I have two Entities:

  • Customer
    • ID
  • Car
    • CustomerID
    • LicenceNumber

All the examples I found using criteria and join were about going from the car to the customer, but I need to go the other way around:

I want to select customers that own a car with a given licence number. My problem is how to connect from the customer table to the car table since the foreign key is in the other table.

Was it helpful?

Solution

You need to have a bidirectional relation to do that in Criteria. Make sure you create a *ToMany relation to Car in Customer. Then you can simply select on Customer where Car list is not empty. Note that this does not change the database schema (unless you don't do the mapping correctly) and hibernate will use the fk on Customer in the Car table.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top