Java.lang.IllegalArgumentException: NamedQuery of name: InstantMessage.findAll not found

Glassfish error log

Caused by: java.lang.IllegalArgumentException: NamedQuery of name: InstantMessage.findAll not found.
	at org.eclipse.persistence.internal.jpa.QueryImpl.getDatabaseQueryInternal(QueryImpl.java:350)
	at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createNamedQuery(EntityManagerImpl.java:1107)
	at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createNamedQuery(EntityManagerImpl.java:1127)
	at com.sun.enterprise.container.common.impl.EntityManagerWrapper.createNamedQuery(EntityManagerWrapper.java:545)
	at com.netricite.dao.InstantMessageDao.findAll2(InstantMessageDao.java:62)

Reason

the NamedQuery is not found by the entitymanager.

Mispelling check

  • check persistence.xml (declare the class injecting the entity manager)
  • check link between @PersistenceContext and persitence.xml (persistence_unit value)
  • check the name given in the createNamedQuery statement and the name given in the @entity/@NamedQuery

Conflict check

  • Check conflict between various persistence.xml used in the project, including embedded jars. (ie persistence unit value cannot be the same in different persistence.xml)

JPA (java persistence API) configuration

Internet resources

Configuration and resources

  • Configuration file: persistence.xml

persistence.xml

Location

src/META-INF/persistence.xml

Example

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
 http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="netricite_PU" transaction-type="JTA">
        <jta-data-source>jdbc/bonecp_resource</jta-data-source>
        <class>com.netricite.entities.ApplicationCode</class>
        <class>com.netricite.entities.rbac.Webuser</class>
        <class>com.netricite.entities.rbac.Resource</class>
        <class>com.netricite.entities.rbac.Permission</class>
        <class>com.netricite.entities.rbac.PermissionResource</class>
        <class>com.netricite.entities.rbac.Role</class>
        <class>com.netricite.entities.rbac.RolePermission</class>
        <class>com.netricite.entities.rbac.Function</class>
        <class>com.netricite.entities.rbac.FunctionRole</class>
        <class>com.netricite.entities.rbac.Affinity</class>
         <class>com.netricite.entities.rbac.AffinityRole</class>       
        <properties>
            <property name="eclipselink.logging.level.sql" value="FINE"/>
            <property name="eclipselink.logging.parameters" value="true"/>
        </properties>        
    </persistence-unit>   
</persistence>

 

Next steps

  • none

Date management (SimpleDateFormat, JPA/Mmysql date ,Month/Quarter)

1.SimpleDateFormat example

   /**
     * @param lastVisit
     *            the lastVisit to set
     */
    public void setLastVisit( Date lastVisit ) {
        this.lastVisit = lastVisit;
    }

    public String getFormattedLastVisit() {
        return new SimpleDateFormat( "yyyy-MM-dd' 'HH:mm:ss.SSSZ" ).format( lastVisit );
    }

HTML

   h:outputText value="Last visit #{userSessionBean.formattedLastVisit}" 

output

Last visit 2014-05-08 18:22:44.750+0200

2. JPA/MYSQL date

  • @see EJB3 JPA: Dealing with Date, Time and Timestamp
  • Using java.sql.Timestamp, source code:

    @Column(name = "TIMESTAMP_FIELD") 
    private java.sql.Timestamp timestampField; 
    
    mysql result:
    2014-07-02 18:58:12
    SimpleDateFormat:
    2014-07-02 18:58:12.078+0200
    

    Using java.util.Date, source code:

    @Temporal(TemporalType.TIMESTAMP) @Column(name = "TIMESTAMP_FIELD") 
    private java.util.Date timestampField; 
    
    mysql result:
    2014-07-02 18:58:12
    SimpleDateFormat:
    2014-07-02 18:58:12.078+0200
    

    3.affect a month to a quarter example

    int month = Calendar.getInstance(Locale.FR).get( Calendar.MONTH ) + 1;
    int quarter = month % 3 == 0?  (month / 3): ( month / 3)+1;
    
    

    JPA relationship management (@ManyToMany)

    many to many relationship

    @see example

    Parent associated with child through parentchild (@ManyToMany in the Parent entity)

        // bi-directional many-to-many association to child table 
        @ManyToMany
        @JoinTable(
                name = "rbacparentchild"
                , joinColumns = {
                @JoinColumn( name = "ParentId", nullable = false )
                }
                , inverseJoinColumns = {
                @JoinColumn( name = "ChildId", nullable = false )
                } )
        private List           childs;
    
        // bi-directional many-to-one association to parentchild table
        @OneToMany( mappedBy = "parent" )
        private List parentchilds;
    
        // bi-directional many-to-one association to grandparentparent table
        @OneToMany( mappedBy = "parent" )
        private List     grandparentparents;
    

    ParentChild relationship, @ManyToOne in the ParentChild entity

     // foreign keys
        // bi-directional many-to-one association to parent table
        @ManyToOne
        @JoinColumn( name = "ParentId", referencedColumnName = "id", nullable = false )
        private Parent        parent;
    
        // bi-directional many-to-one association to child table
        @ManyToOne
        @JoinColumn( name = "ChildId", referencedColumnName = "id", nullable = false )
        private Child          child;
    

    inverse relationship, @OnetoMany in the child entity

     // bi-directional many-to-many association to Rbacparent
        private List         parents;
    
        // bi-directional many-to-one association to Rbacparentchild
        @OneToMany( mappedBy = "child" )
        private List parentchilds;