2 Replies Latest reply on May 4, 2009 8:08 AM by hpacheco

    join table class in hibernate

      Hi all,

      First of all, I don't kow if I am posting this question in the right forum, since the "old" hibernate forums (forum.hibernate.org) now redirect to the JBoss page and could find no trace of them.

      I have two tables in my database schema

      clients: clientid*, name
      products: productid*, name

      I want Hibernate to map it these to 3 POJOs:

      class Client { int id, Name name}
      class Product {int id, Name name}
      class Name { String name }

      Mapping the first two is simple, but is there a way to tell Hibernate that Name instances correspond to a join of the names from the two tables?
      What I would like is to be able to query all the Name instances in the database (and if removed one name, remove its entrances in both tables) without an explicit query.

      Thanks in advance,
      hugo

        • 1. Re: join table class in hibernate
          asookazian

          Hibernate supports polymorphic queries. So you can write an interface and have the two entity classes implement that interface.

          When you query the interface with no restrictions/criteria in where clause, for example, you'll get all records from both tables.

          read more in Ch. 5 of JPA/Hibernate book.

          • 2. Re: join table class in hibernate

            I haven't tought of that, but I wanted to do something like a "from Name" query, but that does not work since Name is not mapped.

            I will always have to query the Name elements from the other tables and merge the results.

            Thanks,
            hugo