ContactInfo类的数据库中有一个表。现在我想找到其中 customerId = id 和 isDeleted = false
的值<!-- getInfo.php -->
<?PHP
echo "hello world";
?>
联系信息类别:
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();
}
PersonalInfo类:
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;
}
}
BaseInfo类:
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;
}
答案 0 :(得分:1)
您的 customerId 存在于 PersonalInfo 实体中,因此您的条件查询应如下所示。
criteria.select(root).where(
builder.equal(root.get("customer").get("customerId"), id)
);
请尝试这个。
我希望这能解决您的问题。