Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用CriteriaBuilder获取数据时出现问题_Java_Hibernate_Hibernate Criteria - Fatal编程技术网

Java 使用CriteriaBuilder获取数据时出现问题

Java 使用CriteriaBuilder获取数据时出现问题,java,hibernate,hibernate-criteria,Java,Hibernate,Hibernate Criteria,ContactInfo类的数据库中有一个表。现在我想找到customerId=id和isDeleted=false private EntityManager entityManager; public ContactInfo findById(long id) { CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<ContactInfo> criteria =

ContactInfo类的数据库中有一个表。现在我想找到customerId=idisDeleted=false

 private EntityManager entityManager;
 public ContactInfo findById(long id) {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<ContactInfo> criteria = builder.createQuery( ContactInfo.class );
    Root<ContactInfo> root = criteria.from(ContactInfo.class);
    criteria.select(root).where(
            builder.equal(root.get("customerId"), id),
            builder.equal(root.get("isDeleted"), false)
    );
    return entityManager.createQuery(criteria).getSingleResult();
}
个人信息类:

public class ContactInfo extends BaseInfo {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long cntId;
    @Column(columnDefinition="TEXT",length=4000)
    private String data;
    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="customer_id")
    private PersonalInfo customer;

    public ContactInfo(){ }
    public ContactInfo(Long id) {
        this.cntId = id;
    }
}
public class PersonalInfo extends BaseInfo {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long customerId;//
    private Long cardId;//
}
abstract public class BaseInfo {

    @CreatedDate
    private Date createdDate;
    @CreatedBy
    private String createdBy;
    @LastModifiedDate
    private Date modifiedDate;
    @LastModifiedBy
    private String modifiedBy;
    @Column(columnDefinition = "boolean default false", nullable = false)
    private boolean isDeleted;
}
BaseInfo类:

public class ContactInfo extends BaseInfo {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long cntId;
    @Column(columnDefinition="TEXT",length=4000)
    private String data;
    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="customer_id")
    private PersonalInfo customer;

    public ContactInfo(){ }
    public ContactInfo(Long id) {
        this.cntId = id;
    }
}
public class PersonalInfo extends BaseInfo {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long customerId;//
    private Long cardId;//
}
abstract public class BaseInfo {

    @CreatedDate
    private Date createdDate;
    @CreatedBy
    private String createdBy;
    @LastModifiedDate
    private Date modifiedDate;
    @LastModifiedBy
    private String modifiedBy;
    @Column(columnDefinition = "boolean default false", nullable = false)
    private boolean isDeleted;
}
如何绕过以下错误。提前谢谢。 错误

2018-10-07 10:47:11.742 ERROR 1168 --- [nio-8082-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: Unable to locate Attribute  with the the given name [customerId] on this ManagedType [com.exm.base.BaseInfo]; nested exception is java.lang.IllegalArgumentException: Unable to locate Attribute  with the the given name [customerId] on this ManagedType [com.csinfotechbd.base.BaseInfo]] with root cause

您的customerId存在于PersonalInfo实体中,因此您的条件查询如下所示

criteria.select(root).where(
    builder.equal(root.get("customer").get("customerId"), id)
);
请试试这个


我希望这能解决您的问题。

请在问题中添加您的实体类ContactInfo。请向我们显示
ContactInfo
类的代码。我已添加了所有必要的类。。请看一看。customerId不是ContactInfo的一部分,但它是PersonalInfo类的一部分,所以您应该只从PersonalInfo类获取customerId。我正在学习阶段。我对这些东西有点了解。请你告诉我如何摆脱它或任何资源,将帮助我解决这个问题。