1. javaeetutorial

Criteria query code snippets use untype-safe metamodel classes


    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 6.0.7-5
    • Fix Version/s: 6.0.7-6
    • Component/s: doc
    • Labels:


      From the feedback alias:
      I've got a question about an example on creating criteria queries in Part VI Chapter 35 of the Java EE 6 tutorial. Link:
      In the "Querying Relationships Using Joins" section of the chapter, there is an example code that doesn't work for me.

      CriteriaQuery<Pet> cq = cb.createQuery(Pet.class);
      Metamodel m = em.getMetamodel();
      EntityType<Pet> Pet_ = m.entity(Pet.class);

      Root<Pet> pet = cq.from(Pet.class);
      Join<Pet, Owner> owner = pet.join(Pet_.owners);

      Since Pet_ is an instance of class EntityType, it doesn't define any attributes named "owners", which is why Pet_.owners gives an error. Is that a mistake?
      Searching through forums I found a thread discussing this problem here
      If this is indeed a mistake, could you please correct the code.

      The code snippets used dynamic metamodel classes for clarity on where, e.g., Pet_ comes from. But in practice, it gives the impression that this is a good practice for creating queries, which is not the case. The metamodel classes should be statically generated by an annotation processor at development time to make the code compilable and type-safe.


        Ian Evans created issue -
        Ian Evans made changes -
        Field Original Value New Value
        Assignee Ian Evans [ ievans ]
        Ian Evans made changes -
        Status Open [ 1 ] In Progress [ 3 ]
        Ian Evans made changes -
        Status In Progress [ 3 ] Resolved [ 5 ]
        Resolution Fixed [ 1 ]


          • Assignee:
            Ian Evans
            Ian Evans
          • Votes:
            0 Vote for this issue
            0 Start watching this issue


            • Created: