2 Replies Latest reply: Apr 16, 2012 12:42 PM by 930717 RSS

    Unable to create query.

    930717
      Hello
      I am new to jpa. When I create the "SELECT g FROM Game g WHERE g.player1_id=:user" I get the following error "unknown state or association field [player1_id] of class [entity.Game]".

      Here are netbean's generated entity classes.


      package entity;

      import java.io.Serializable;
      import java.util.Collection;
      import java.util.Date;
      import javax.persistence.*;
      import javax.validation.constraints.NotNull;
      import javax.validation.constraints.Size;
      import javax.xml.bind.annotation.XmlRootElement;
      import javax.xml.bind.annotation.XmlTransient;

      /**
      *
      * @author Ara Yeritsian
      */
      @Entity
      @Table(name = "game")
      @XmlRootElement
      @NamedQueries({
      @NamedQuery(name = "Game.findAll", query = "SELECT g FROM Game g"),
      @NamedQuery(name = "Game.findById", query = "SELECT g FROM Game g WHERE g.id = :id"),
      @NamedQuery(name = "Game.findByState", query = "SELECT g FROM Game g WHERE g.state = :state"),
      @NamedQuery(name = "Game.findByStartRulles", query = "SELECT g FROM Game g WHERE g.startRulles = :startRulles"),
      @NamedQuery(name = "Game.findByStartDate", query = "SELECT g FROM Game g WHERE g.startDate = :startDate"),
      @NamedQuery(name = "Game.findByClock", query = "SELECT g FROM Game g WHERE g.clock = :clock")})
      public class Game implements Serializable {
      private static final long serialVersionUID = 1L;
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Basic(optional = false)
      @NotNull
      @Column(name = "id")
      private Integer id;
      @Basic(optional = false)
      @NotNull
      @Size(min = 1, max = 16)
      @Column(name = "state")
      private String state;
      @Basic(optional = false)
      @NotNull
      @Column(name = "start_rulles")
      private short startRulles;
      @Column(name = "start_date")
      @Temporal(TemporalType.TIMESTAMP)
      private Date startDate;
      @Column(name = "clock")
      private Integer clock;
      @OneToMany(cascade = CascadeType.ALL, mappedBy = "gameId")
      private Collection<MoveHistory> moveHistoryCollection;
      @JoinColumn(name = "player2_id", referencedColumnName = "id")
      @ManyToOne
      private User player2Id;
      @JoinColumn(name = "player1_id", referencedColumnName = "id")
      @ManyToOne(optional = false)
      private User player1Id;

      public Game() {
      }

      public Game(Integer id) {
      this.id = id;
      }

      public Game(Integer id, String state, short startRulles) {
      this.id = id;
      this.state = state;
      this.startRulles = startRulles;
      }

      public Integer getId() {
      return id;
      }

      public void setId(Integer id) {
      this.id = id;
      }

      public String getState() {
      return state;
      }

      public void setState(String state) {
      this.state = state;
      }

      public short getStartRulles() {
      return startRulles;
      }

      public void setStartRulles(short startRulles) {
      this.startRulles = startRulles;
      }

      public Date getStartDate() {
      return startDate;
      }

      public void setStartDate(Date startDate) {
      this.startDate = startDate;
      }

      public Integer getClock() {
      return clock;
      }

      public void setClock(Integer clock) {
      this.clock = clock;
      }

      @XmlTransient
      public Collection<MoveHistory> getMoveHistoryCollection() {
      return moveHistoryCollection;
      }

      public void setMoveHistoryCollection(Collection<MoveHistory> moveHistoryCollection) {
      this.moveHistoryCollection = moveHistoryCollection;
      }

      public User getPlayer2Id() {
      return player2Id;
      }

      public void setPlayer2Id(User player2Id) {
      this.player2Id = player2Id;
      }

      public User getPlayer1Id() {
      return player1Id;
      }

      public void setPlayer1Id(User player1Id) {
      this.player1Id = player1Id;
      }

      @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 Game)) {
      return false;
      }
      Game other = (Game) 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 "entity.Game[ id=" + id + " ]";
      }

      }

      /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

      /*
      * To change this template, choose Tools | Templates
      * and open the template in the editor.
      */
      package entity;

      import java.io.Serializable;
      import java.util.Collection;
      import javax.persistence.*;
      import javax.validation.constraints.NotNull;
      import javax.validation.constraints.Size;
      import javax.xml.bind.annotation.XmlRootElement;
      import javax.xml.bind.annotation.XmlTransient;

      /**
      *
      * @author Ara Yeritsian
      */
      @Entity
      @Table(name = "user")
      @XmlRootElement
      @NamedQueries({
      @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.findByEmail", query = "SELECT u FROM User u WHERE u.email = :email"),
      @NamedQuery(name = "User.findByPassword", query = "SELECT u FROM User u WHERE u.password = :password"),
      @NamedQuery(name = "User.findByRating", query = "SELECT u FROM User u WHERE u.rating = :rating"),
      @NamedQuery(name = "User.findByNickname", query = "SELECT u FROM User u WHERE u.nickname = :nickname"),
      @NamedQuery(name = "User.findBySecurityQuestionAnswer", query = "SELECT u FROM User u WHERE u.securityQuestionAnswer = :securityQuestionAnswer")})
      public class User implements Serializable {
      private static final long serialVersionUID = 1L;
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Basic(optional = false)
      @NotNull
      @Column(name = "id")
      private Integer id;
      // @Pattern(regexp="[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", message="Invalid email")//if the field contains email address consider using this annotation to enforce field validation
      @Basic(optional = false)
      @NotNull
      @Size(min = 1, max = 45)
      @Column(name = "email")
      private String email;
      @Basic(optional = false)
      @NotNull
      @Size(min = 1, max = 45)
      @Column(name = "password")
      private String password;
      @Basic(optional = false)
      @NotNull
      @Column(name = "rating")
      private int rating;
      @Basic(optional = false)
      @NotNull
      @Size(min = 1, max = 45)
      @Column(name = "nickname")
      private String nickname;
      @Basic(optional = false)
      @NotNull
      @Size(min = 1, max = 45)
      @Column(name = "security_question_answer")
      private String securityQuestionAnswer;
      @OneToMany(cascade = CascadeType.ALL, mappedBy = "playerId")
      private Collection<MoveHistory> moveHistoryCollection;
      @OneToMany(mappedBy = "player2Id")
      private Collection<Game> gameCollection;
      @OneToMany(cascade = CascadeType.ALL, mappedBy = "player1Id")
      private Collection<Game> gameCollection1;
      @JoinColumn(name = "security_question_id", referencedColumnName = "id")
      @ManyToOne(optional = false)
      private SecurityQuestion securityQuestionId;

      public User() {
      }

      public User(Integer id) {
      this.id = id;
      }

      public User(Integer id, String email, String password, int rating, String nickname, String securityQuestionAnswer) {
      this.id = id;
      this.email = email;
      this.password = password;
      this.rating = rating;
      this.nickname = nickname;
      this.securityQuestionAnswer = securityQuestionAnswer;
      }

      public Integer getId() {
      return id;
      }

      public void setId(Integer id) {
      this.id = id;
      }

      public String getEmail() {
      return email;
      }

      public void setEmail(String email) {
      this.email = email;
      }

      public String getPassword() {
      return password;
      }

      public void setPassword(String password) {
      this.password = password;
      }

      public int getRating() {
      return rating;
      }

      public void setRating(int rating) {
      this.rating = rating;
      }

      public String getNickname() {
      return nickname;
      }

      public void setNickname(String nickname) {
      this.nickname = nickname;
      }

      public String getSecurityQuestionAnswer() {
      return securityQuestionAnswer;
      }

      public void setSecurityQuestionAnswer(String securityQuestionAnswer) {
      this.securityQuestionAnswer = securityQuestionAnswer;
      }

      @XmlTransient
      public Collection<MoveHistory> getMoveHistoryCollection() {
      return moveHistoryCollection;
      }

      public void setMoveHistoryCollection(Collection<MoveHistory> moveHistoryCollection) {
      this.moveHistoryCollection = moveHistoryCollection;
      }

      @XmlTransient
      public Collection<Game> getGameCollection() {
      return gameCollection;
      }

      public void setGameCollection(Collection<Game> gameCollection) {
      this.gameCollection = gameCollection;
      }

      @XmlTransient
      public Collection<Game> getGameCollection1() {
      return gameCollection1;
      }

      public void setGameCollection1(Collection<Game> gameCollection1) {
      this.gameCollection1 = gameCollection1;
      }

      public SecurityQuestion getSecurityQuestionId() {
      return securityQuestionId;
      }

      public void setSecurityQuestionId(SecurityQuestion securityQuestionId) {
      this.securityQuestionId = securityQuestionId;
      }

      @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 "entity.User[ id=" + id + " ]";
      }

      }

      Edited by: 927714 on Apr 15, 2012 1:04 AM