[英]Problem in fetching data using CriteriaBuilder
There is a table in database of ContactInfo class. ContactInfo类的数据库中有一个表。 now i want to find the value where customerId = id and isDeleted = false
现在我想找到其中customerId = id和isDeleted = 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();
}
Contact info class: 联系信息类别:
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;
}
}
PersonalInfo class: PersonalInfo类:
public class PersonalInfo extends BaseInfo {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long customerId;//
private Long cardId;//
}
BaseInfo class: BaseInfo类:
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;
}
How to bypass the following error. 如何绕过以下错误。 thanks in advance.
提前致谢。 Error
错误
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
your customerId is present into PersonalInfo entity, so your criteria query should be like below. 您的customerId存在于PersonalInfo实体中,因此您的条件查询应如下所示。
criteria.select(root).where(
builder.equal(root.get("customer").get("customerId"), id)
);
Please try this. 请尝试这个。
I hope this will solve your problem. 我希望这能解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.