Search code examples
javajpajointoplink

Why cannot join 3rd table using JPA TopLink annotation?


I have simple 3 tables:

Users
-id
-name

Addresses
-id
-user_id  //FK
-location

Phone
-id
-number
-address_id //FK
  • Users can have many Addresses
  • Addresses can have many Phone

Thus, annotations applied to each entity class would look like:

Users

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "user")
    private List<Addresses> addresses;

Addresses

    @ManyToOne()
    @JoinColumn(name = "user_id")
    private Users user;

    @OneToMany(mappedBy = "address")
    private List<Phone> phone;

Phone

    @ManyToOne()
    @JoinColumn(name = "address_id")
    private Addresses address;

The annotation above should create relationship:

users  (1) ----- (n) addresses (1) ------ (n) phone

Then I created some test data in MySQL and tried to run test to see if query result that returns "users" would include reference to addresses and phone.

I've tried with 2 table only using users and addresses and got result ok.

Now that I added "phone" table which is 3rd table then I'm getting error shown below. Is this really right way to join 3 or more tables?

Test code

      try {
          //get entity manager
          String WEB_PU = "NoJSFPU";
          EntityManagerFactory factory =                 Persistence.createEntityManagerFactory(WEB_PU);
          EntityManager em = factory.createEntityManager();

          //run query
          List<Users> usersList = em.createQuery("SELECT u FROM Users u").getResultList();

           //loop to print out each result
           for(Users item : usersList) {
               //display user info
               System.out.println(item.getId() + " " + item.getFirstname());

               List<Addresses> addrs = item.getAddresses();
               for(Addresses ad : addrs) {
                  //display address info
                  System.out.println("Address:::" + ad.getLocation());

                  List<Phone> pho = ad.getPhone();
                  for(Phone p : pho) {
                     //display phone info
                     System.out.println("phone#" + p.getNumber());
                  }

                }//end inner for
            }//end outer for
                System.out.println("==========================");

            }
            catch(Exception e) {
                System.out.println("seriously wtf: " + e.toString());
                System.exit(1);
            }

Error

Exception Description: predeploy for PersistenceUnit [NoJSFPU] failed.
Internal Exception: Exception [TOPLINK-7250] (Oracle TopLink Essentials - 2.0.1 (Build b09d-fcs (12/06/2007))): oracle.toplink.essentials.exceptions.ValidationException
Exception Description: [class local.test.session.Addresses] uses a non-entity [class local.test.session.Phone] as target entity in the relationship attribute [private java.util.List local.test.session.Addresses.phone].

Solution

  • I figured out. Had to specify by using @JoinTable annotation.

    Now If I run the test code, data from all 3 tables can be obtained :D

    Addresses

    @ManyToOne
    @JoinTable(name = "Addresses", joinColumns = {@JoinColumn(name="user_id", 
    referencedColumnName = "user_id") } )
    private Users user;
    

    Phone

    @ManyToOne
    @JoinTable(name = "Phone", joinColumns = {@JoinColumn(name="address_id", 
    referencedColumnName = "address_id") } )
    private Addresses address;