0

I have this a very basic problem and i am sure that there is a very simple solution to it.

I have two entities having many-to-many relationship at db level.

Customer and Channel joined by Customer_Channel.

I am not a very pro at java persistence criteria and hibernate and this is part of a bigger application.

Now i have to bring this relationship in Customer. So should i model customer to have List<Channels> or List<Customer_Channels> making it a Many-To-Many Relationship or One-To-Many relationship.

I don't need anything in Channel Entity.

Now i need to have a criteria query created to Fetch Customer belonging to a particular channel. A very simple SQL would be

Select Customer from Customer, Channel, Customer_Channel where Customer.ID=Customer_Channel.CustomerID and Customer_Channel.ChannelID=Channel.ID and Channel.ID = ?

or a very direct could be

Select Customer from Customer, Customer_Channel where Customer.ID=Customer_Channel.CustomerID and Customer_Channel.ChannelID=?

So which approach to take and what would be the Criteria Query in this case. My project doesn't use MetaModel classes.

Thanks a lot for help

Bryan
  • 11,398
  • 3
  • 53
  • 78
  • possible duplicate of [Querying ManyToMany relationship with Hibernate Criteria](http://stackoverflow.com/questions/264339/querying-manytomany-relationship-with-hibernate-criteria) – Zeus Jul 09 '14 at 19:33
  • Thanks Zeus, i have checked the solution at the mentioned question this resembles my point but is there a way of doing this through Predicates, my project have those things already and without using sql. It will be good to have API's instead of sql as that will fit in directly into existing API's – user3681527 Jul 09 '14 at 20:07

1 Answers1

0

You will make it a @manytomany on a List. Use Jointable etc., annotations to get this thing done. Please refer to the solution here

Zeus
  • 6,386
  • 6
  • 54
  • 89