3 Replies Latest reply on Oct 19, 2009 11:53 AM by nickarls

    Wiring components together in an EntityQuery class

    yannfromparis

      Hello,


      I can't figure out how to wire components in an EnityQuery class. I've got the following entity:




      @Entity
      @Table(name = "bottle")
      public class Bottle implements Serializable {
           private static final long serialVersionUID = ...
      
           private Appellation appellation;
           private Wine wine;



      Appellation and Wine classes are both entities too.


      The EntityQuery component looks like this:




      @Name("bottleList")
      @Scope(ScopeType.CONVERSATION)
      public class BottleList extends EntityQuery<Bottle> {
      
           private static final long serialVersionUID = ...
      
           private static final String EJBQL = "select b from Bottle b " +
           "join fetch b.appellation a " +
           "join fetch b.wine w";
      
           private static final String[] RESTRICTIONS = {
                "lower(a.name) like lower(concat(#{bottleList.bottle.appellation.name},'%'))",
                "lower(w.name) like lower(concat(#{bottleList.bottle.wine.name},'%'))",
                "b.year like concat(#{bottleList.bottle.year},'%')",};
      
           private Bottle bottle = new Bottle();



      As you can see, this is pretty much the code generated by seam-gen except for the query which loads eagerly dependent entities.


      The code above doesn't work because Appellation and Wine objects are not initialized. I can make in work by doing this, as detailed in seam documentation:


      components.xml




      <component name="bottleListAppellation" class="net.wait4it.bottles.model.Appellation" />
      <component name="bottleListWine" class="net.wait4it.bottles.model.Wine" />
      <component name="bottleListBottle" class="net.wait4it.bottles.model.Bottle">
           <property name="appellation">#{bottleListAppellation}</property>
           <property name="wine">#{bottleListWine}</property>
      </component>



      and by changing the EntityQuery class like this:




           @In(value="#{bottleListBottle}")
           private Bottle bottle;



      Could you please tell me what's the proper way to do the same in plain old java ? Because I've tried to create dependencies in my EntityQuery and to use appropriate setters, but it doesn't work - at all.


      Thanks a lot

        • 1. Re: Wiring components together in an EntityQuery class
          nickarls

          Why don't you map the entire hierarchy? Put @OneToMany etc on Wine and Appellation in Bottle and just do select b from Bottle b. Or am I missing something here?

          • 2. Re: Wiring components together in an EntityQuery class
            yannfromparis

            Sorry, my mistake. I've in fact defined these relations on the getters in Bottle entity:




            @NotNull
            @OneToOne
            @JoinColumn(name = "appellation_id")
            public Appellation getAppellation() {
                 return appellation;
            }
            
            @NotNull
            @OneToOne
            @JoinColumn(name = "wine_id")
            public Wine getWine() {
                 return wine;
            }



            But I've already made a test whith the request you mentioned and it doesn't work, I've got exceptions about null dependencies.

            • 3. Re: Wiring components together in an EntityQuery class
              nickarls

              Test it with Java SE, bootstrap a small JPA-block in main(String[]) and try do the stuff with just an entityManager, if it doesn't work, it's not a Seam bug ;-)


              (you might also want to check that you also have @Id on the getter, I have seen strange results sometimes when mixing field/getter annotations)


              And post full stack traces if you get it to work in hibernate but not in entityquery...