5 Replies Latest reply on Jan 13, 2011 10:21 AM by lobz

    Custom EntityIdentifierStrategy vs. IdentifierPolicy

    lobz

      I tried to set a custom identifier strategy for one of my entities with the @Identifier annotation but after debuging the IdentifierPolicy class I found that my custom strategy is neglected in favor of the builtin EntityIdentifierStrategy.


      Inside the if (strategy == null) {} block my strategy is found and put into the strategies map as expected but then in the for statement it's replaced with de default one. Shouldn't the for statement be in an else block?


      The relevant code snippet from IdentifierPolicy.getIdentifier:


            if (strategy == null)
            {
               if (target.getClass().isAnnotationPresent(Identifier.class))
               {
                  Class<? extends IdentifierStrategy> strategyClass = 
                     target.getClass().getAnnotation(Identifier.class).value();
                  
                  if (strategyClass != IdentifierStrategy.class)
                  {
                     try
                     {
                        strategy = strategyClass.newInstance();
                        strategies.put(target.getClass(), strategy);
                     }
                     catch (Exception ex)
                     {
                        throw new RuntimeException("Error instantiating IdentifierStrategy for object " + target, ex);
                     }
                  }
               }
      
               for (IdentifierStrategy s : registeredStrategies)
               {
                  if (s.canIdentify(target.getClass()))
                  {
                     strategy = s;
                     strategies.put(target.getClass(), strategy);
                     break;
                  }
               }
            }