Entity:

package com.igorgorbunov3333.core.entities.domain; import com.igorgorbunov3333.core.entities.enums.Proceeding; import javax.persistence.*; import java.util.Date; /** * Created by Игорь on 03.04.2016. */ @Entity @Table(name = "cases") @NamedQueries({ @NamedQuery(name = "getAllCases", query = "SELECT c FROM Case c JOIN FETCH c.client JOIN FETCH c.firstInstance " + "JOIN FETCH c.secondInstance JOIN FETCH c.thirdInstance JOIN FETCH c.category") }) public class Case { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private long id; @Column(name = "case_number") private String caseNumber; @ManyToOne @JoinColumn(name = "client_id") private Client client; @OneToOne @JoinColumn(name = "first_instance_id") private FirstInstance firstInstance; @OneToOne @JoinColumn(name = "second_instance_id") private SecondInstance secondInstance; @OneToOne @JoinColumn(name = "third_instance_id") private ThirdInstance thirdInstance; @Enumerated(EnumType.ORDINAL) private Proceeding proceeding; @Temporal(TemporalType.DATE) @Column(name = "closing_date") private Date closingDate; @ManyToOne @JoinColumn(name = "category_id") private Category category; private float price; public long getId() { return id; } public String getCaseNumber() { return caseNumber; } public Client getClient() { return client; } public FirstInstance getFirstInstance() { return firstInstance; } public SecondInstance getSecondInstance() { return secondInstance; } public ThirdInstance getThirdInstance() { return thirdInstance; } public Proceeding getProceeding() { return proceeding; } public Date getClosingDate() { return closingDate; } public Category getCategory() { return category; } public float getPrice() { return price; } public void setId(long id) { this.id = id; } public void setCaseNumber(String caseNumber) { this.caseNumber = caseNumber; } public void setClient(Client client) { this.client = client; } public void setFirstInstance(FirstInstance firstInstance) { this.firstInstance = firstInstance; } public void setSecondInstance(SecondInstance secondInstance) { this.secondInstance = secondInstance; } public void setThirdInstance(ThirdInstance thirdInstance) { this.thirdInstance = thirdInstance; } public void setProceeding(Proceeding proceeding) { this.proceeding = proceeding; } public void setClosingDate(Date closingDate) { this.closingDate = closingDate; } public void setCategory(Category category) { this.category = category; } public void setPrice(float price) { this.price = price; } } 

Here I receive NullPointerException :

 public Case findById(long id) { return entityManager.find(Case.class, id); } 

According to the database table I checked it - the id is what I need, but the entity is not located. All entities get fine:

 public List<Case> findAll() { Query query = entityManager.createNamedQuery("getAllCases"); return query.getResultList(); } 

What could be the problem? Container - Spring, supplier - Hibernate.

  • Enable the option to show sql: <property name="show_sql">true</property> and execute this query separately with your id, does the result return the required data? - MrFylypenko
  • Enabled, does not return! - Igor Gorbunov
  • Try this query with: entityManager.createQuery( "SELECT c FROM Case c WHERE c.id = :id") .setParameter("id", id) .getResultList(); , but if it does not help, add the code to the githab to reproduce in yourself - MrFylypenko
  • @MrFylypenko thanks for the comments. Solved a problem. In the servlet, I created a CaseService like this: private CaseService caseService = new CaseServiceImpl() . It was necessary so: private CaseService caseService = SpringAppContextManager.getAppContext() .getBean("CaseService", CaseService.class); - Igor Gorbunov

0