Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Unable to find id
PostPosted: Wed May 31, 2017 5:32 am 
Newbie

Joined: Wed May 31, 2017 5:26 am
Posts: 1
Hi Guys,

I have been trying to solve an issue with my entity. I get this exception everytime to try to update an object ; javax.persistence.EntityNotFoundException: Unable to find com.nibssplc.icadservice.entity.Institution with id 3. In the database, the specified ID actually exists.

The entity
Code:
public class InstitutionBranch extends AbstractFlag implements Serializable, com.nibssplc.icadservice.admin.model.Entity {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "institutionbranchid")
    private Long branchId;
    @Basic(optional = false)
//    @NotNull
    @Size(min = 1, max = 64)
    @Column(name = "branchname")
    private String branchName;

    @Basic(optional = false)
    //@NotNull
    @Column(name = "createddate")
    @Temporal(TemporalType.TIMESTAMP)
    private Date createdDate;

    @Basic(optional = false)
    //@NotNull
    //@Size(min = 1, max = 5)
    @Column(name = "flag", length = 5)
    private String flag;

    @Size(max = 150)
    @Column(name = "location")
    private String location;

    @Basic(optional = false)
//    @NotNull
    @Size(min = 1, max = 10)
    @Column(name = "branchcode")
    private String branchCode;
   
   
    @JoinColumn(name = "institution", referencedColumnName = "institutionid")
    @ManyToOne(optional = false, fetch = FetchType.EAGER)
   private Institution institution;
//    @XmlTransient
//    @OneToMany(cascade = CascadeType.ALL, mappedBy = "branch", fetch = FetchType.LAZY)
//    private List<UserIdentity> userIdentityList;

    public InstitutionBranch() {
    }

    public InstitutionBranch(Long branchId) {
        this.branchId = branchId;
    }

    public Long getBranchId() {
        return branchId;
    }

    public void setBranchId(Long branchId) {
        this.branchId = branchId;
    }

    public String getBranchName() {
        return branchName;
    }

    public void setBranchName(String branchName) {
        this.branchName = branchName;
    }

    @XmlTransient
    public Date getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(Date createdDate) {
        this.createdDate = createdDate;
    }

    @Override
    public String getFlag() {
        return flag;
    }

    @Override
    public void setFlag(String flag) {
        this.flag = flag;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public String getBranchCode() {
        return branchCode;
    }

    public void setBranchCode(String branchCode) {
        this.branchCode = branchCode;
    }

    @XmlTransient
    public Institution getInstitution() {
        return institution;
    }

    public void setInstitution(Institution institution) {
        this.institution = institution;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (branchId != null ? branchId.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof InstitutionBranch)) {
            return false;
        }
        InstitutionBranch other = (InstitutionBranch) object;
        if ((this.branchId == null && other.branchId != null) || (this.branchId != null && !this.branchId.equals(other.branchId))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return getClass().getSimpleName() + "[ branchId=" + branchId + " ]";
    }

    @XmlTransient
    @Override
    public Object getEntityId() {
        return branchId;
    }

    @Transient
    private Long institutionId;

    public Long getInstitutionId() {
        return institutionId;
    }

//    @XmlElement
//    public String getInstitutionName() {
//        return getInstitution() != null ? getInstitution().getInstitutionName() : null;
//    }

}


The implementing method
Code:
public T update(T item) {
        T t = null;
        try
        {
           
         
          t = (T) this.em.merge(item);
          this.em.flush();
          this.em.refresh(t);
        }
        catch(Exception e)
        {
            System.out.println("WARNING [ERROR]  IN update() ");
            e.printStackTrace();
        }
        return t; //(T) this.entityManager.merge(item);
       
    }


Please help me resolve . Thanks


Top
 Profile  
 
 Post subject: Re: Unable to find id
PostPosted: Wed May 31, 2017 9:56 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1386
Maybe you didn't map Institution to the right table. Activate JDBC logging and see the executed queries.

_________________
If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.