4 Replies Latest reply on Sep 8, 2008 1:08 AM by Per Wiklander

    Entity from multiple tables

    Michal Rost Newbie

      Hi everybody i am sorry for noob question, but I'm absolutely feckless.


      I don't know how to create entity from two or more different database tables, joined on some of their foreign keys.


      If there is no @Table annotation after @Entity annotation in java entity class, i receive exception, that database table which corresponds with entity cannot be found.
      Also i haven't found any way how to join some entities using annotations and hql. @Entity with @NamedQuery doesn't work, when i wanna select from more than one table.
      I was also wondering if it is possible to create entity for a database view, so i created view in my db and used seam to generate entity from it, it worked well and create @Table annotation with name of my db view as attribute name. But when i started my app it hasn't worked again, i received hibernate exception that table with such name cannot be found.

        • 1. Re: Entity from multiple tables
          Joshua Jackson Master

          You need to join / relate the property in one object using either @ManyToMany, @OneToOne, @OneToMany, or @ManyToOne as such:



          @Entity
          @Table(name="product")
          public class Product{
             @ManyToOne private Category category; 
          }




          @Entity
          @Table(name="category")
          public class Category{
             @OneToMany private List<Product> products; 
          }



          Is this what you're looking for?


          • 2. Re: Entity from multiple tables
            Michal Rost Newbie

            thank you very much for your reply, but not at all...  i want to make more complicated select.. in my app, i have these tables:


            license
              licensepkid
              serialnumber
              other
            data


            computer
              licensepkid
              otherdata

            medium
              medium
            pkid
              other
            data


            licenseassignment
              assign
            pkid
              assign
            date
              licensefkid
              computerfkid


            mediuminstallation
              install
            pkid
              install
            date
              mediumfkid
              computerfkid


            returnedlicense (cant be used)
              return
            pkid
              license
            fkid

            I used seam to generate entities for all these tables ...those with fk
            ids are joined automatically  ...but what i wanna do is select all licenses (their serial numbers, date of assignment, medium on which they are) that has been installed but not returned ...so i wanna join licenseassignment with mediuminstallation on computerfkid somehow ...but i dunno what to do with resultlist is it possible to create entity bean for result of this query?



            • 3. Re: Entity from multiple tables
              Michal Rost Newbie
              oh ...sorry ... posting again without formatting

              thank you very much for your reply, but not at all... i want to make more complicated select.. in my app, i have these tables:

              license
                license_pk_id
                serial_number
                other_data

              computer
                license_pk_id
                other_data

              medium
                medium_pk_id
                other_data

              license_assignment
                assign_pk_id
                assign_date
                license_fk_id
                computer_fk_id

              medium_installation
                install_pk_id
                install_date
                medium_fk_id
                computer_fk_id

              returnedlicense (cant be used)
                return_pk_id
                license_fk_id

              I used seam to generate entities for all these tables ...those with fk_ids are joined automatically ...but what i wanna do is select all licenses (their serial numbers, date of assignment, medium on which they are) that has been installed but not returned ...so i wanna join license_assignment with medium_installation on computer_fk_id somehow ...but i dunno what to do with resultlist is it possible to create entity bean for result of this query?
              • 4. Re: Entity from multiple tables
                Per Wiklander Newbie

                How about creating a view of the query in the database and mapping your Entity to that view?