Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Unable to create query.
Unable to create query. [message #846684] Mon, 16 April 2012 13:18 Go to next message
Ara Yeritsian is currently offline Ara Yeritsian
Messages: 2
Registered: April 2012
Junior Member
Hello
I am new to jpa. When I create the "SELECT g FROM Game g WHERE g.player1_id=1" 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 + " ]";
}
}
Re: Unable to create query. [message #849816 is a reply to message #846684] Thu, 19 April 2012 09:24 Go to previous messageGo to next message
James Sutherland is currently offline James Sutherland
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

Your Game class does not have an attribute player1_Id it is player1Id. Also it is not an integer, it is a User object.

Try,
SELECT g FROM Game g WHERE g.player1Id.id = 1

or,
SELECT g FROM Game g WHERE g.player1Id = :aPlayer

Also, the attribute should be called player1 not player1Id, unless you want to map it as a Basic.

Note that your query is JPQL not SQL, if you want to use SQL use createNativeQuery()


James : Wiki : Book : Blog : Twitter
Re: Unable to create query. [message #849844 is a reply to message #849816] Thu, 19 April 2012 09:51 Go to previous message
Ara Yeritsian is currently offline Ara Yeritsian
Messages: 2
Registered: April 2012
Junior Member
Thanks for your reply James Sutherland.

BTW how I can set your message as "answer" I am new in this forum Smile
Previous Topic:Generate index for foreign keys in oracle
Next Topic:2.1.0 to 2.3.2
Goto Forum:
  


Current Time: Sun Apr 20 09:30:14 EDT 2014

Powered by FUDForum. Page generated in 0.03376 seconds