views:

1624

answers:

1

Hi,

Is it possible to join two classes without specified mapping between them (using Criteria API)?

I must join two classes and retrieve data from both but I can't mapping them. I know only foreign key SomeID in the first class and primary key ID in second.

How to create Criteria to join them? Is it possible without mapping?

Please, help, I'm really need it but I'm stuck. :/

PS

I know 'any' mapping but I have 10 fields like SomeID. Creating any mappings for 10 fields only for creating joins is overkill. If there is no other resolution I'll do it but I don't want to.

+6  A: 

I don't know the criteria version, but in HQL you can do it like this:

select customer, order from Customer customer, Order order 
    where order.CustomerID = customer.Id
    and customer.Id = 42

The result set will then be a list of object[] where the customer will be repeated times the number of orders he has placed (assuming of course that there is one customer to many orders).

Please note that the result will be empty if there aren't any ordes.

mookid8000
Yeap. I'm using it by Criteria.I'm looking for possibility to create something like this:select customer, order from Customer customer, Order order, User, user where order.SomeID_1 = customer.Id order.SomeID_2 = user.IdI don't know at design time what field SomeID_xxx contains. :/
dario-g
OK, I got it. HQL is only one option.
dario-g

related questions