13 Replies Latest reply on Apr 17, 2013 9:58 AM by ybxiang.china

    persistence.xml (named query not found)

    akash_bansal

      Hi, I have multiple EJB Jars files (packed in single EAR) which contains session beans as well as JPA entities. And each EJB jar contains its own persistence.xml file. My problem is that JBoss 7.1.2 is not able to process all the persistence.xml file, it just process the first persistence.xml file which ever loads first. Due to this the named query not found exception is being thrown.

       

      I have also tried creating single persistence.xml file keepting it in META-INF folder of EAR, but it is also not working.

       

      Request to experts to pls. provide some work around for the problem.

        • 1. Re: persistence.xml (named query not found)
          ybxiang.china

          I am NOT sure if J2EE specification/JBoss AS7 allows several persistence.xml in one ear.

          Maybe there is security issue if you want to do so.

           

          Why NOT merge all ejb jars into one ejb jar?

          • 2. Re: persistence.xml (named query not found)
            ybxiang.china

            And you can deploy several ejb jars directly (do NOT put them in ear).

            • 3. Re: persistence.xml (named query not found)
              jaikiran

              You should be able to package individual persistence.xml in a EJB jar module of its own. Please post the relevant exception stacktrace and the relevant persisten.xml file(s)

              • 4. Re: persistence.xml (named query not found)
                nickarls

                (and include the location of the persistence.xml in the jar as verified by unzipping it)

                • 5. Re: persistence.xml (named query not found)
                  akash_bansal

                  This is the content of my persistence.xml file:

                   

                  <?xml version="1.0" encoding="UTF-8"?>

                  <persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

                      version="2.0"

                      xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"

                      xmlns="http://java.sun.com/xml/ns/persistence">

                      <persistence-unit name="FDS_PU">

                          <jta-data-source>java:jboss/datasources/FloodDB</jta-data-source>

                          <properties>

                              <property name="hibernate.show_sql" value="true" />

                              <property name="hibernate.format_sql" value="true" />

                              <property name="hibernate.show_type" value="true" />

                              <property name="hibernate.query.startup_check" value="true" />

                              <property name="hibernate.default_schema" value="FC_ENT"/>

                          </properties>

                      </persistence-unit>

                  </persistence>

                   

                  And I am packaging it in following manner:

                   

                  EAR

                       EJB-JAR1

                            META-INF\persistence.xml

                       EJB-JAR2

                            META-INF\persistence.xml

                       EJB-JAR2

                            META-INF\persistence.xml

                   

                  Each EJB-JAR contains JPA entities and session beans. And when I deploy this EAR and looksup session beans which calls JPA entities, it throws following exception :

                   

                  Caused by: java.lang.IllegalArgumentException: Named query not found: Company.findByName

                  at org.hibernate.ejb.AbstractEntityManagerImpl.createNamedQuery(AbstractEntityManagerImpl.java:601)

                  at org.jboss.as.jpa.container.AbstractEntityManager.createNamedQuery(AbstractEntityManager.java:356)

                  • 6. Re: persistence.xml (named query not found)
                    ybxiang.china

                    please post the content of this method:

                    Company.findByName

                    • 7. Re: persistence.xml (named query not found)
                      akash_bansal

                      this is not a method, it named query for JPA.

                      • 8. Re: persistence.xml (named query not found)
                        nickarls

                        And the named query can be found on an entity? Crank up the log levels and see which jars are picked up and what is scanned.

                        • 9. Re: persistence.xml (named query not found)
                          ybxiang.china

                          Why NOT post related java classes?

                           

                          We are just guessing and wasting time!

                           

                           

                          Here is an example of Named Query:

                          (1) You must put Named Query on Entity beans through annotations

                          @NamedQueries({

                              @NamedQuery(name=User.NamedQuery_name_FindByUsername,query=User.NamedQuery_jpql_FindByUsername),

                              @NamedQuery(name=User.NamedQuery_name_UpdatePassword,query=User.NamedQuery_jpql_UpdatePassword),

                              @NamedQuery(name=User.NamedQuery_name_CountEmailOccurence,query=User.NamedQuery_jpql_CountEmailOccurence),

                              @NamedQuery(name=User.NamedQuery_name_CountUsernameOccurence,query=User.NamedQuery_jpql_CountUsernameOccurence)

                             

                          })

                          @Entity

                          public class User implements Serializable{

                              //*********************************[static fields]*********************************//

                              private static final long serialVersionUID = 1L;

                              //JPA

                              public static final String JPA_GEN_NAME = "GEN_USER";

                              public static final String JPA_PK_COL_VAL = "NEXT_PK_USER";

                              //JOIN TABLE

                              public static final String JOIN_TABLE_User_UserGroup = "User_UserGroup";

                              public static final String JOIN_TABLE_User_UserGroup_JoinColumn = "user_id";

                              public static final String JOIN_TABLE_User_UserGroup_InverseJoinColumn = "usergroup_id";

                              //NamedQuery:FindByUsername

                              public static final String NamedQuery_name_FindByUsername = "User__NamedQuery_name_FindByUsername";

                              public static final String NamedQuery_jpql_FindByUsername = "SELECT obj FROM User obj WHERE obj.username=:_username";

                              public static final String NamedQuery_param_FindByUsername_username = "_username";

                              //NamedQuery:UpdatePassword

                              public static final String NamedQuery_name_UpdatePassword = "User__NamedQuery_name_UpdatePassword";

                              public static final String NamedQuery_jpql_UpdatePassword = "UPDATE User obj set obj.hashedPassword=:_hashedPassword, obj.lastChangePasswordDate=:_lastChangePasswordDate where obj.username=:_username";

                              public static final String NamedQuery_param_UpdatePassword_hashedPassword = "_hashedPassword";

                              public static final String NamedQuery_param_UpdatePassword_lastChangePasswordDate = "_lastChangePasswordDate";

                              public static final String NamedQuery_param_UpdatePassword_username = "_username";

                              //NamedQuery:CountEmailOccurence

                              public static final String NamedQuery_name_CountEmailOccurence = "User__NamedQuery_name_CountEmailOccurence";

                              public static final String NamedQuery_jpql_CountEmailOccurence = "SELECT count(obj.id) FROM User obj WHERE obj.email=:_email";//不要统计是否激活!

                              public static final String NamedQuery_param_CountEmailOccurence_email = "_email";

                              //NamedQuery:CountEmailOccurence

                              public static final String NamedQuery_name_CountUsernameOccurence = "User__NamedQuery_name_CountUsernameOccurence";

                              public static final String NamedQuery_jpql_CountUsernameOccurence = "SELECT count(obj.id) FROM User obj WHERE obj.username=:_username";

                              public static final String NamedQuery_param_CountUsernameOccurence_username = "_username";

                           

                           

                          (2) You can use it like this:

                          @Stateless

                          @Local(IUserSession.class)

                          public class UserSession implements IUserSession{

                           

                              @PersistenceContext()

                              private EntityManager em;

                             

                              @PermitAll()

                              public User getUserByName(String username){

                                  return (User)em.createNamedQuery(User.NamedQuery_name_FindByUsername)

                                  .setParameter(User.NamedQuery_param_FindByUsername_username, username)

                                  .getSingleResult();

                              }

                              ...

                          }

                          • 10. Re: persistence.xml (named query not found)
                            akash_bansal

                            I'm sorry, here is the file content for the same:

                             

                            @Entity

                            @Table(name = "company")

                            @NamedQueries({ @NamedQuery(name = "Company.findByName", query = "SELECT o FROM Company o WHERE o.name = :param1"),

                                            @NamedQuery(name = "Company.loadWholeEntity", query = "SELECT o FROM Company o left join fetch o.relationships left join fetch o.preferences where o.id=:primaryKey")})

                            public class Company extends CLBaseEntity<java.lang.Integer> {

                            ...

                            ...

                            }

                            • 11. Re: persistence.xml (named query not found)
                              nickarls

                              Crank up the log levels - it will show you what jars/classes are discovered at deployment.

                              • 12. Re: persistence.xml (named query not found)
                                ybxiang.china

                                What is your session bean looks like?

                                 

                                And when I deploy this EAR and looksup session beans which calls JPA entities, it throws following exception :

                                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Why do NOT post related codes and full log?

                                 

                                I think you can deploy ejb jars one by one, or try to put all ejb entity beans and session beans in one jar. Try to find out

                                (1) if it is caused by the separating into 3 ejb jars.

                                (2) if it is caused by your wrong codes.

                                 

                                What you post here is too less!

                                Are your codes too secret to post?

                                • 13. Re: persistence.xml (named query not found)
                                  ybxiang.china

                                  Is the exception thrown during deployment?

                                  Is the exception thrown after deployment and during "looksup session beans"?