javaeetutorial
  1. javaeetutorial
  2. JAVAEETUTORIAL-139

Criteria query code snippets use untype-safe metamodel classes

    Details

    • 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:
      None

      Description

      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: http://docs.oracle.com/javaee/6/tutorial/doc/gjivm.html
      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 http://stackoverflow.com/questions/3879743/jpa-metamodel-strange-inconsistent-example-in-sun-docs
      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.

        Activity

        No work has yet been logged on this issue.

          People

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

            Dates

            • Created:
              Updated:
              Resolved: