【问题标题】:HQL Error "Could not resolve property"HQL 错误“无法解析属性”
【发布时间】:2023-04-04 05:06:01
【问题描述】:

在使用 HQL 从我的 Oracle 数据库中提取实体时遇到了一些问题。

这是来自我的 DAO 的代码

public ClientProject getClientProject(Client client, Product product) {
    ClientProject clientProject = null;
    Session session = factory.openSession();
    String name = client.getClientName();
    String id = "" + product.getProductId();
    String hql= "from ClientProject as cp where cp.client.name = :name and cp.product.id = :id";
    try {
        session.beginTransaction();
        Query query = session.createQuery(hql);
        query.setParameter("name", "%"+name+"%");
        query.setParameter("id", "%"+id+"%");

    } finally {
        session.close();
    }

    return clientProject;
}

这是 ClientProject 的代码

@Entity
@Table (name="client_project")
@SequenceGenerator(name="seq_client_project",sequenceName="****.SEQ_CLIENT_PROJECT", allocationSize=1, initialValue=1)
public class ClientProject {

//Fields
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq_client_project")
@Column(name="CLIENT_PROJECT_ID")
private int id;
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="client_id")
private Client client;
@OneToOne(cascade=CascadeType.ALL)
@JoinColumn(name="product_id")
private Product product;
@OneToMany(mappedBy="clientProject")
private Set<Mod> clientProjectMod;
@Column(name="PROJECT_CODE")
private String projectCode;

这是客户端类的代码

@Entity
@Table (name="client")
@SequenceGenerator(name="seq_client",sequenceName="****.SEQ_CLIENT", allocationSize=1, initialValue=1)
public class Client {

//Fields
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq_client")
@Column(name="CLIENT_ID")
private int id;
@Column(name="CLIENT_NAME")
private String clientName;
@Column(name="CLIENT_CODE")
private String clientCode;
@OneToMany(mappedBy="client")
private Set<ClientProject> clientProjects;

这是我收到的错误消息..

WARNING: #{newBean.handleGenerateForm}: org.hibernate.QueryException: could not resolve property: name of: com.manh.entries.Client [from com.manh.entries.ClientProject as cp where cp.client.name = :name and cp.product.id = :id] javax.faces.FacesException: #{newBean.handleGenerateForm}: org.hibernate.QueryException: could not resolve property: name of: com.manh.entries.Client [from com.manh.entries.ClientProject as cp where cp.client.name = :name and cp.product.id = :id]

有什么想法吗?我猜我只是搞砸了 HQL 查询的一部分……

提前致谢!

【问题讨论】:

  • 发布您的 ClientProjectClient 课程
  • 我猜你需要一个alias
  • @Reimeus 我添加了请求的代码

标签: java oracle hibernate hql


【解决方案1】:

属性名称是clientName 而不是name

String hql= "from ClientProject as cp where cp.client.clientName = :name and cp.product.id = :id";

【讨论】:

  • 这是一个很好的收获!代码仍然没有从查询中返回任何内容。有什么想法吗?
  • 您正在使用% 通配符。在查询中使用like 而不是=,或者不要使用这些字符
猜你喜欢
  • 2013-01-03
  • 1970-01-01
  • 1970-01-01
  • 2019-11-26
  • 2015-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多