Results 1 to 2 of 2
  1. #1
    beginner_user is offline Member
    Join Date
    Sep 2013
    Posts
    1
    Rep Power
    0

    Default TomEE and MySql - query don't returns any results

    Hi,

    I am using TomeEE + MySql and i have problem because function createNamedQuery don't returns any results. I thought that problem is with my
    entityManager but i checked in debugMode and is injected.


    This is my code:

    User Entity:
    Java Code:
    package pl.gsite.db.model;
    
    import java.io.Serializable;
    import javax.persistence.Basic;
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.Id;
    import javax.persistence.NamedQueries;
    import javax.persistence.NamedQuery;
    import javax.persistence.Table;
    import javax.validation.constraints.NotNull;
    import javax.validation.constraints.Size;
    import javax.xml.bind.annotation.XmlRootElement;
    
    
    @Entity
    @Table(name = "user")
    @XmlRootElement
    @NamedQueries({
        @NamedQuery(name = "User.loginAndPassword", query = "SELECT u FROM User u WHERE u.login = :login and u.password = :password"),    
        @NamedQuery(name = "User.findAll", query = "SELECT u FROM User u"),
        @NamedQuery(name = "User.findById", query = "SELECT u FROM User u WHERE u.id = :id"),
        @NamedQuery(name = "User.findByLogin", query = "SELECT u FROM User u WHERE u.login = :login"),
        @NamedQuery(name = "User.findByPassword", query = "SELECT u FROM User u WHERE u.password = :password")})
    public class User implements Serializable {
        private static final long serialVersionUID = 1L;
        @Id
        @Basic(optional = false)
        @NotNull
        @Column(name = "id")
        private Integer id;
        @Size(max = 100)
        @Column(name = "login")
        private String login;
        @Size(max = 100)
        @Column(name = "password")
        private String password;
    
        public User() {
        }
    
        public User(Integer id) {
            this.id = id;
        }
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getLogin() {
            return login;
        }
    
        public void setLogin(String login) {
            this.login = login;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
        @Override
        public int hashCode() {
            int hash = 0;
            hash += (id != null ? id.hashCode() : 0);
            return hash;
        }
    
        @Override
        public boolean equals(Object object) {
            // TODO: Warning - this method won't work in the case the id fields are not set
            if (!(object instanceof User)) {
                return false;
            }
            User other = (User) object;
            if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
                return false;
            }
            return true;
        }
    
        @Override
        public String toString() {
            return "pl.gsite.db.model.User[ id=" + id + " ]";
        }
        
    }
    Java Code:
    package pl.gsite.bean.request;
    
    import java.util.ArrayList;
    import java.util.List;
    import javax.ejb.Stateful;
    import javax.inject.Named;
    import javax.enterprise.context.RequestScoped;
    import javax.enterprise.context.spi.Context;
    import javax.inject.Inject;
    import javax.persistence.EntityManager;
    import javax.persistence.PersistenceContext;
    import javax.persistence.TypedQuery;
    import pl.gsite.bean.session.LoggedUserBean;
    import pl.gsite.db.model.User;
    import javax.persistence.NoResultException;
    import javax.persistence.PersistenceContextType;
    import javax.persistence.Query;
    
    
    my managedBean:
    
    @Named(value = "loginRequest")
    @RequestScoped
    public class LoginRequest {
       @PersistenceContext(unitName = "gsitePU")
       private EntityManager em;
       
       @Inject
       private LoggedUserBean loggedUserBean;
       private String login;
       private String password;
        
        public LoginRequest() {
        }
        
        public String authentication() {        
            try {
                List<User> uList = new ArrayList<User>();            
                TypedQuery qq = em.createNamedQuery("User.findAll", User.class);
                uList = qq.getResultList(); // <-- returns empty list
                TypedQuery<User> query = em.createNamedQuery("User.loginAndPassword", User.class).setParameter("login", login).setParameter("password", password);    
                User u = query.getSingleResult(); // <-- throws an NoResultException
                this.loggedUserBean.setLoggedUser(u);            
            }
            catch(NoResultException e) {
                e.printStackTrace();
            }
            
           return "index";
        }
    
        /**
         * @return the login
         */
        public String getLogin() {
            return login;
        }
    
        /**
         * @param login the login to set
         */
        public void setLogin(String login) {
            this.login = login;
        }
    
        /**
         * @return the password
         */
        public String getPassword() {
            return password;
        }
    
        /**
         * @param password the password to set
         */
        public void setPassword(String password) {
            this.password = password;
        }
    }
    persistance.xml:
    Java Code:
    <?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="gsitePU" transaction-type="JTA">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <jta-data-source>gsite_mysql</jta-data-source>
        <properties>
          <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(foreignKeys=true)"/>
        </properties>
      </persistence-unit>
    </persistence>

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,669
    Rep Power
    5

    Default Re: TomEE and MySql - query don't returns any results

    So apparently the username and password combination you're using does not exist. Don't say "yes it does", no it doesn't. Your code proves it. There is no evil wizard blocking JPA (and ultimately JDBC) from getting the data that is there.

    Here is a debugging tip: select all and dump the users through a System.out to see what is really in there. My guess: nothing, the database is empty. I say this because I spot one thing in your configuration that makes me frown:

    Java Code:
    <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(foreignKeys=true)"/>
    I don't know much about OpenJPA but I'd say this instructs JPA to build the database schema for you. Why do you have it configured like that? Are you sure you are connecting to the right database?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Replies: 4
    Last Post: 05-18-2011, 09:21 AM
  2. MySQL JDBC query returns no response
    By PDXErik in forum New To Java
    Replies: 1
    Last Post: 08-20-2010, 09:07 AM
  3. MySQL/JDBC Mysql query output
    By thelinuxguy in forum Advanced Java
    Replies: 4
    Last Post: 02-13-2009, 01:57 AM
  4. how to take query results to a jlist!!
    By themburu in forum JDBC
    Replies: 1
    Last Post: 06-07-2008, 10:51 AM
  5. how to take query results to a jlist!!
    By themburu in forum New To Java
    Replies: 3
    Last Post: 06-07-2008, 10:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •