0

There is a table in database of ContactInfo class. now i want to find the value where customerId = id and 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:

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

BaseInfo class:

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
11
  • Please add your entity class ContactInfo in your question. Commented Oct 7, 2018 at 5:53
  • Please show us the code for ContactInfo class. Commented Oct 7, 2018 at 6:00
  • i have added all necessary classes.. plz have a look. Commented Oct 7, 2018 at 6:22
  • customerId is not part of ContactInfo but it is part of PersonalInfo class, so you should get customerId from PersonalInfo class only. Commented Oct 7, 2018 at 6:27
  • i am in learning phase. i have a little knowledge about these stuff. will u plz explain me how to get out of it or any resource that will help me with this problem. Commented Oct 7, 2018 at 6:30

1 Answer 1

1

your customerId is present into PersonalInfo entity, so your criteria query should be like below.

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

Please try this.

I hope this will solve your problem.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.