我正在尝试运行一个查询,让我根据客户的ID获得客户完成的特定工作。 我在使用JPQL进行队列时遇到麻烦,该队列实际上将根据customerId的外键检索所有数据。

我试过了SELECT w FROM WorkEntry w WHERE w.customerId = 1 ,这是行不通的。 每次尝试使用持久性单元测试运行它时,都会出现以下错误:

java.lang.IllegalArgumentException:在EntityManager中创建查询时发生异常:异常描述:编译时出现问题[SELECT w FROM WorkEntry w WHERE w.customerId = 1]。 [14,23]抽象模式类型'WorkEntry'是未知的。 [32,44]状态字段路径'w.customerId'无法解析为有效类型。 在org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManager‌Impl.java:1605)

我想知道我想念的是什么? 我是JPAJPQL新手。

这是客户类别:

@Entity
@Table(name = "customer")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Customer.findAll", query = "SELECT c FROM Customer c"),
@NamedQuery(name = "Customer.findByCustomerId", query = "SELECT c FROM Customer c WHERE c.customerId = :customerId"),
@NamedQuery(name = "Customer.findByFirstName", query = "SELECT c FROM Customer c WHERE c.firstName = :firstName"),
@NamedQuery(name = "Customer.findByLastName", query = "SELECT c FROM Customer c WHERE c.lastName = :lastName"),
@NamedQuery(name = "Customer.findByStreetAddress", query = "SELECT c FROM Customer c WHERE c.streetAddress = :streetAddress"),
@NamedQuery(name = "Customer.findByCity", query = "SELECT c FROM Customer c WHERE c.city = :city"),
@NamedQuery(name = "Customer.findByState", query = "SELECT c FROM Customer c WHERE c.state = :state"),
@NamedQuery(name = "Customer.findByPostalCode", query = "SELECT c FROM Customer c WHERE c.postalCode = :postalCode"),
@NamedQuery(name = "Customer.findByPhoneNumber", query = "SELECT c FROM Customer c WHERE c.phoneNumber = :phoneNumber"),
@NamedQuery(name = "Customer.findByEmail", query = "SELECT c FROM Customer c WHERE c.email = :email")})
public class Customer implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "customer_id")
private Integer customerId;
@Size(max = 75)
@Column(name = "first_name")
private String firstName;
@Size(max = 75)
@Column(name = "last_name")
private String lastName;
@Size(max = 250)
@Column(name = "street_address")
private String streetAddress;
@Size(max = 50)
@Column(name = "city")
private String city;
@Size(max = 50)
@Column(name = "state")
private String state;
@Size(max = 45)
@Column(name = "postal_code")
private String postalCode;
@Size(max = 45)
@Column(name = "phone_number")
private String phoneNumber;
// @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
@Size(max = 250)
@Column(name = "email")
private String email;
@OneToMany(mappedBy = "customerId")
private Collection<ExpenseEntry> expenseEntryCollection;
@OneToMany(mappedBy = "customerId")
private Collection<WorkEntry> workEntryCollection;

public Customer() {
}

public Customer(Integer customerId, String firstName, String lastName, String streetAddress, String city, String state, String postalCode, String phoneNumber, String email) {
    this.customerId = customerId;
    this.firstName = firstName;
    this.lastName = lastName;
    this.streetAddress = streetAddress;
    this.city = city;
    this.state = state;
    this.postalCode = postalCode;
    this.phoneNumber = phoneNumber;
    this.email = email;
}

public Customer(String firstName, String lastName, String streetAddress, String city, String state, String postalCode, String phoneNumber, String email) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.streetAddress = streetAddress;
    this.city = city;
    this.state = state;
    this.postalCode = postalCode;
    this.phoneNumber = phoneNumber;
    this.email = email;
}


public Customer(Integer customerId) {
    this.customerId = customerId;
}

public Integer getCustomerId() {
    return customerId;
}

public void setCustomerId(Integer customerId) {
    this.customerId = customerId;
}

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public String getStreetAddress() {
    return streetAddress;
}

public void setStreetAddress(String streetAddress) {
    this.streetAddress = streetAddress;
}

public String getCity() {
    return city;
}

public void setCity(String city) {
    this.city = city;
}

public String getState() {
    return state;
}

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

public String getPostalCode() {
    return postalCode;
}

public void setPostalCode(String postalCode) {
    this.postalCode = postalCode;
}

public String getPhoneNumber() {
    return phoneNumber;
}

public void setPhoneNumber(String phoneNumber) {
    this.phoneNumber = phoneNumber;
}

public String getEmail() {
    return email;
}

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

@XmlTransient
public Collection<ExpenseEntry> getExpenseEntryCollection() {
    return expenseEntryCollection;
}

public void setExpenseEntryCollection(Collection<ExpenseEntry> expenseEntryCollection) {
    this.expenseEntryCollection = expenseEntryCollection;
}

@XmlTransient
public Collection<WorkEntry> getWorkEntryCollection() {
    return workEntryCollection;
}

public void setWorkEntryCollection(Collection<WorkEntry> workEntryCollection) {
    this.workEntryCollection = workEntryCollection;
}

@Override
public int hashCode() {
    int hash = 0;
    hash += (customerId != null ? customerId.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 Customer)) {
        return false;
    }
    Customer other = (Customer) object;
    if ((this.customerId == null && other.customerId != null) || (this.customerId != null && !this.customerId.equals(other.customerId))) {
        return false;
    }
    return true;
}

@Override
public String toString() {
    return "consultant.billing.entity.Customer[ customerId=" + customerId + " ]";
}

}

这是我的WorkEntry类:

@Entity
@Table(name = "work_entry")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "WorkEntry.findAll", query = "SELECT w FROM WorkEntry w"),
@NamedQuery(name = "WorkEntry.findByWorkEntryId", query = "SELECT w FROM WorkEntry w WHERE w.workEntryId = :workEntryId"),
@NamedQuery(name = "WorkEntry.findByDate", query = "SELECT w FROM WorkEntry w WHERE w.date = :date"),
@NamedQuery(name = "WorkEntry.findByHoursWorked", query = "SELECT w FROM WorkEntry w WHERE w.hoursWorked = :hoursWorked")})
public class WorkEntry implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "work_entry_id")
private Integer workEntryId;
@Column(name = "date")
@Temporal(TemporalType.DATE)
private Date date;
// @Max(value=?)  @Min(value=?)//if you know range of your decimal fields consider using these annotations to enforce field validation
@Column(name = "hours_worked")
private Double hoursWorked;
@JoinColumn(name = "activity_id", referencedColumnName = "activity_id")
@ManyToOne
private Activity activityId;
@JoinColumn(name = "customer_id", referencedColumnName = "customer_id")
@ManyToOne
private Customer customerId;

public WorkEntry() {
}

public WorkEntry(Date date, Double hoursWorked, Customer customerId, Activity activityId) {
    this.date = date;
    this.hoursWorked = hoursWorked;
    this.activityId = activityId;
    this.customerId = customerId;
}


public WorkEntry(Integer workEntryId) {
    this.workEntryId = workEntryId;
}

public Integer getWorkEntryId() {
    return workEntryId;
}

public void setWorkEntryId(Integer workEntryId) {
    this.workEntryId = workEntryId;
}

public Date getDate() {
    return date;
}

public void setDate(Date date) {
    this.date = date;
}

public Double getHoursWorked() {
    return hoursWorked;
}

public void setHoursWorked(Double hoursWorked) {
    this.hoursWorked = hoursWorked;
}

public Activity getActivityId() {
    return activityId;
}

public void setActivityId(Activity activityId) {
    this.activityId = activityId;
}

public Customer getCustomerId() {
    return customerId;
}

public void setCustomerId(Customer customerId) {
    this.customerId = customerId;
}

@Override
public int hashCode() {
    int hash = 0;
    hash += (workEntryId != null ? workEntryId.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 WorkEntry)) {
        return false;
    }
    WorkEntry other = (WorkEntry) object;
    if ((this.workEntryId == null && other.workEntryId != null) || (this.workEntryId != null && !this.workEntryId.equals(other.workEntryId))) {
        return false;
    }
    return true;
}

@Override
public String toString() {
    return "consultant.billing.entity.WorkEntry[ workEntryId=" + workEntryId + " ]";
}

}

===============>>#1 票数:4 已采纳

我正在尝试运行一个查询,让我根据客户的ID获得客户完成的特定工作。 我在使用JPQL进行队列时遇到麻烦,该队列实际上将根据customerId的外键检索所有数据。

我试过了SELECT w FROM WorkEntry w WHERE w.customerId = 1 ,这是行不通的。 每次尝试使用持久性单元测试运行它时,都会出现以下错误:

java.lang.IllegalArgumentException:在EntityManager中创建查询时发生异常:异常描述:编译时出现问题[SELECT w FROM WorkEntry w WHERE w.customerId = 1]。 [14,23]抽象模式类型'WorkEntry'是未知的。 [32,44]状态字段路径'w.customerId'无法解析为有效类型。 在org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManager‌Impl.java:1605)

我想知道我想念的是什么? 我是JPAJPQL新手。

这是客户类别:

@Entity
@Table(name = "customer")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Customer.findAll", query = "SELECT c FROM Customer c"),
@NamedQuery(name = "Customer.findByCustomerId", query = "SELECT c FROM Customer c WHERE c.customerId = :customerId"),
@NamedQuery(name = "Customer.findByFirstName", query = "SELECT c FROM Customer c WHERE c.firstName = :firstName"),
@NamedQuery(name = "Customer.findByLastName", query = "SELECT c FROM Customer c WHERE c.lastName = :lastName"),
@NamedQuery(name = "Customer.findByStreetAddress", query = "SELECT c FROM Customer c WHERE c.streetAddress = :streetAddress"),
@NamedQuery(name = "Customer.findByCity", query = "SELECT c FROM Customer c WHERE c.city = :city"),
@NamedQuery(name = "Customer.findByState", query = "SELECT c FROM Customer c WHERE c.state = :state"),
@NamedQuery(name = "Customer.findByPostalCode", query = "SELECT c FROM Customer c WHERE c.postalCode = :postalCode"),
@NamedQuery(name = "Customer.findByPhoneNumber", query = "SELECT c FROM Customer c WHERE c.phoneNumber = :phoneNumber"),
@NamedQuery(name = "Customer.findByEmail", query = "SELECT c FROM Customer c WHERE c.email = :email")})
public class Customer implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "customer_id")
private Integer customerId;
@Size(max = 75)
@Column(name = "first_name")
private String firstName;
@Size(max = 75)
@Column(name = "last_name")
private String lastName;
@Size(max = 250)
@Column(name = "street_address")
private String streetAddress;
@Size(max = 50)
@Column(name = "city")
private String city;
@Size(max = 50)
@Column(name = "state")
private String state;
@Size(max = 45)
@Column(name = "postal_code")
private String postalCode;
@Size(max = 45)
@Column(name = "phone_number")
private String phoneNumber;
// @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
@Size(max = 250)
@Column(name = "email")
private String email;
@OneToMany(mappedBy = "customerId")
private Collection<ExpenseEntry> expenseEntryCollection;
@OneToMany(mappedBy = "customerId")
private Collection<WorkEntry> workEntryCollection;

public Customer() {
}

public Customer(Integer customerId, String firstName, String lastName, String streetAddress, String city, String state, String postalCode, String phoneNumber, String email) {
    this.customerId = customerId;
    this.firstName = firstName;
    this.lastName = lastName;
    this.streetAddress = streetAddress;
    this.city = city;
    this.state = state;
    this.postalCode = postalCode;
    this.phoneNumber = phoneNumber;
    this.email = email;
}

public Customer(String firstName, String lastName, String streetAddress, String city, String state, String postalCode, String phoneNumber, String email) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.streetAddress = streetAddress;
    this.city = city;
    this.state = state;
    this.postalCode = postalCode;
    this.phoneNumber = phoneNumber;
    this.email = email;
}


public Customer(Integer customerId) {
    this.customerId = customerId;
}

public Integer getCustomerId() {
    return customerId;
}

public void setCustomerId(Integer customerId) {
    this.customerId = customerId;
}

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public String getStreetAddress() {
    return streetAddress;
}

public void setStreetAddress(String streetAddress) {
    this.streetAddress = streetAddress;
}

public String getCity() {
    return city;
}

public void setCity(String city) {
    this.city = city;
}

public String getState() {
    return state;
}

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

public String getPostalCode() {
    return postalCode;
}

public void setPostalCode(String postalCode) {
    this.postalCode = postalCode;
}

public String getPhoneNumber() {
    return phoneNumber;
}

public void setPhoneNumber(String phoneNumber) {
    this.phoneNumber = phoneNumber;
}

public String getEmail() {
    return email;
}

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

@XmlTransient
public Collection<ExpenseEntry> getExpenseEntryCollection() {
    return expenseEntryCollection;
}

public void setExpenseEntryCollection(Collection<ExpenseEntry> expenseEntryCollection) {
    this.expenseEntryCollection = expenseEntryCollection;
}

@XmlTransient
public Collection<WorkEntry> getWorkEntryCollection() {
    return workEntryCollection;
}

public void setWorkEntryCollection(Collection<WorkEntry> workEntryCollection) {
    this.workEntryCollection = workEntryCollection;
}

@Override
public int hashCode() {
    int hash = 0;
    hash += (customerId != null ? customerId.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 Customer)) {
        return false;
    }
    Customer other = (Customer) object;
    if ((this.customerId == null && other.customerId != null) || (this.customerId != null && !this.customerId.equals(other.customerId))) {
        return false;
    }
    return true;
}

@Override
public String toString() {
    return "consultant.billing.entity.Customer[ customerId=" + customerId + " ]";
}

}

这是我的WorkEntry类:

@Entity
@Table(name = "work_entry")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "WorkEntry.findAll", query = "SELECT w FROM WorkEntry w"),
@NamedQuery(name = "WorkEntry.findByWorkEntryId", query = "SELECT w FROM WorkEntry w WHERE w.workEntryId = :workEntryId"),
@NamedQuery(name = "WorkEntry.findByDate", query = "SELECT w FROM WorkEntry w WHERE w.date = :date"),
@NamedQuery(name = "WorkEntry.findByHoursWorked", query = "SELECT w FROM WorkEntry w WHERE w.hoursWorked = :hoursWorked")})
public class WorkEntry implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "work_entry_id")
private Integer workEntryId;
@Column(name = "date")
@Temporal(TemporalType.DATE)
private Date date;
// @Max(value=?)  @Min(value=?)//if you know range of your decimal fields consider using these annotations to enforce field validation
@Column(name = "hours_worked")
private Double hoursWorked;
@JoinColumn(name = "activity_id", referencedColumnName = "activity_id")
@ManyToOne
private Activity activityId;
@JoinColumn(name = "customer_id", referencedColumnName = "customer_id")
@ManyToOne
private Customer customerId;

public WorkEntry() {
}

public WorkEntry(Date date, Double hoursWorked, Customer customerId, Activity activityId) {
    this.date = date;
    this.hoursWorked = hoursWorked;
    this.activityId = activityId;
    this.customerId = customerId;
}


public WorkEntry(Integer workEntryId) {
    this.workEntryId = workEntryId;
}

public Integer getWorkEntryId() {
    return workEntryId;
}

public void setWorkEntryId(Integer workEntryId) {
    this.workEntryId = workEntryId;
}

public Date getDate() {
    return date;
}

public void setDate(Date date) {
    this.date = date;
}

public Double getHoursWorked() {
    return hoursWorked;
}

public void setHoursWorked(Double hoursWorked) {
    this.hoursWorked = hoursWorked;
}

public Activity getActivityId() {
    return activityId;
}

public void setActivityId(Activity activityId) {
    this.activityId = activityId;
}

public Customer getCustomerId() {
    return customerId;
}

public void setCustomerId(Customer customerId) {
    this.customerId = customerId;
}

@Override
public int hashCode() {
    int hash = 0;
    hash += (workEntryId != null ? workEntryId.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 WorkEntry)) {
        return false;
    }
    WorkEntry other = (WorkEntry) object;
    if ((this.workEntryId == null && other.workEntryId != null) || (this.workEntryId != null && !this.workEntryId.equals(other.workEntryId))) {
        return false;
    }
    return true;
}

@Override
public String toString() {
    return "consultant.billing.entity.WorkEntry[ workEntryId=" + workEntryId + " ]";
}

}

  ask by Jamie Studzinski translate from so

未解决问题?本站智能推荐:

1回复

JPA / MySQL - 这个JPQL查询有什么问题?

我有实体Post : 实体Comment : 然后,在PostDAOBean有以下方法: 当我在servlet中调用此方法时,我得到下一个异常: 您可以看到命名查询是: SELECT p FROM Post p ORDER BY SIZE(p.comments)
1回复

使用JPA在JPQL中是否可以替代本机查询的“ GROUP_CONCAT”?

我正在尝试使用jpql和JPA来获取配置文件的配置文件菜单。 我的“个人资料”和“ ProfileMenus”实体具有多对一关系。 我尝试调查这些答案,但找不到任何有效的解决方案。 如何在Spring Boot应用程序中添加非标准化的sql函数? 向JPA和Hibernate
2回复

如何构建一个JPQL查询,从几个表中获取数据,克服使用JPA 1.0的延迟初始化?

我需要使用基于OpenJPA JPA 1.0的JPQL查询从几个表中获取数据。 使用自然连接构造和执行查询是有效的,但由于延迟初始化,我没有得到结果中的相关对象。 我可以使用join fetch构建查询(在此项目的其他情况下,join fetch对我有用): 然后我收到错误:
1回复

如何在jpa和jpql中加入多个表列

我是JPA和JPQL的新手。 我正在尝试从一个表中查询数据库抓取行,该表主要包含其他表中包含的名称的索引。 这是我要重新创建的mysql中的查询: 这是我所查询到的查询: 这是我收到的Java Glassfish错误:
2回复

JPA JPQl或MySQL中的存储过程

我正在开发一个使用JPA 2.0和MySQL 5数据库的应用程序。 我知道JPA隐藏了对数据库查询的调用并使其成为继续,我使用的只是JPQL, 现在这是否会减少对存储过程的需求? 以及如何在JPA 2.0中使用存储过程?
4回复

JPQL查询有什么问题?

我正在尝试在服务器上执行休假查询,这是我的查询: 对象PostMW(不完整,但带有我要说的必要数据): 这是LikeMW(完整的),因此,您看到的只有表USER和POST中的两个主键: 我收到nullpointerException! 我不知道为什么...例外:
2回复

使用JPQL / JPA时如何使用date_format

我正在将Java EE与MySQL作为数据库并在我的代码上实现JPA。 我从MySQL Workbench检索数据没有问题,但是当我将语法更改为JPQL时,它不起作用。 例如在MySQL中-它可以工作 在JPQL中-它不会 如何修改以适合JPA查询? 注意:在J
1回复

如何使用JPA(JPQL或Criteria API)在符文时选择列?

我在SO和网络上进行了搜索,但找不到此问题的答案。 如何使用JPQL或JPA的Criteria API编写以下MySQL查询? 我意识到这个特定的查询毫无意义,但是对我来说,问题是动态选择列。 即选择i18n.language,在运行时选择“语言”。 我可以想象它看起来像:
1回复

Spring Boot JPA:为同一参数(JPQL)传递多个值

我正在使用JPQL在JPA的CurdRepository接口中编写SQL查询。 我能够用命名参数编写多个查询,它们就像一个魅力一样工作。 但是,我的应用程序要求我将未知数目的字符串与一个表中的多个列匹配。 据我所知,在SQL中看起来像这样: 我在MySQL中试过,它工作正常。
1回复

将mysql查询转换为JPQL查询

我如何在JPQL中声明以下mysql查询 我尝试过的是以下内容: 但是在这种方法上会出错: