13

I'm retreiving some data from the database in my DAO with this method :

@Override
@Transactional
public List<History> findLastHistoriesByCount(int gHistoriesCount) {

    List<History> result = new ArrayList<History>();
    result = entityManager
            .createQuery(
                    "SELECT h FROM History h ORDER BY h.eventDate DESC",
                    History.class).setMaxResults(gHistoriesCount).getResultList();

    return result;
}

and this is my History Class :

public class History implements Serializable {

...

@ManyToOne
@JoinColumn(name = "ID_CONTRACT_HISTORY", nullable = true)
private ContractHistory contractHistory;

@ManyToOne
@JoinColumn(name = "ID_SALARY_HISTORY", nullable = true)
private SalaryHistory salaryHistory;

...
}

but I'm facing this exception :

javax.persistence.EntityNotFoundException: Unable to find com.xxx.model.ContractHistory with id 108

While I'm sure I have that row in my database in the table ContractHistory

EDIT

I've already seen the solution with @NotFound annotation and I don't want it cause it's just a bypass for me cause my data is there !! and I want to retreive it.

EDIT 2 **ContractHistory : **

public class ContractHistory extends implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
...
}

EDIT 3 :

ContractHistory : (Don't mind the table names I've changed it before only for this question :

enter image description here

History :

enter image description here

EDIT 4

The sql query just before the exception :

 Hibernate: 
22:22:29,211 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)     select
22:22:29,211 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         companycon0_.id as id234_13_,
22:22:29,212 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         companycon0_.ID_COMPANY_CONTRACT as ID3_234_13_,
22:22:29,212 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         companycon0_.ID_COMPANY_CONTRACT_AVENANT as ID4_234_13_,
22:22:29,212 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         companycon0_.ID_EVENT_TYPE as ID5_234_13_,
22:22:29,213 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         companycon0_.timestamp as timestamp234_13_,
22:22:29,213 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         companycon0_.ID_USER as ID6_234_13_,
22:22:29,213 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         companycon1_.id as id218_0_,
22:22:29,214 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         companycon1_.IDCLIENT as IDCLIENT218_0_,
22:22:29,214 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         companycon1_.endDate as endDate218_0_,
22:22:29,215 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         companycon1_.invoicePaymentDay as invoiceP3_218_0_,
22:22:29,215 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         companycon1_.percentage_Extra_Hours as percentage4_218_0_,
22:22:29,216 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         companycon1_.IDPROFILE as IDPROFILE218_0_,
22:22:29,216 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         companycon1_.rate as rate218_0_,
22:22:29,216 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         companycon1_.reference as reference218_0_,
22:22:29,217 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         companycon1_.secondRate as secondRate218_0_,
22:22:29,217 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         companycon1_.startDate as startDate218_0_,
22:22:29,218 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         companycon1_.thirdRate as thirdRate218_0_,
22:22:29,218 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         companycon1_.UPDATE_TIMESTAMP as UPDATE10_218_0_,
22:22:29,218 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         companycon1_.IDUSER as IDUSER218_0_,
22:22:29,219 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         client2_.id as id215_1_,
22:22:29,219 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         client2_.address as address215_1_,
22:22:29,219 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         client2_.BILLING_ADDRESS as BILLING3_215_1_,
22:22:29,220 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         client2_.CLIENT_NAME as CLIENT4_215_1_,
22:22:29,220 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         client2_.COMMENTS as COMMENTS215_1_,
22:22:29,220 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         client2_.IDCOMPANY as IDCOMPANY215_1_,
22:22:29,221 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         client2_.logo as logo215_1_,
22:22:29,221 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         client2_.siren as siren215_1_,
22:22:29,222 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         client2_.TVA_INTRACOMMUNAUTAIRE as TVA8_215_1_,
22:22:29,222 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         client2_.UPDATE_TIMESTAMP as UPDATE9_215_1_,
22:22:29,222 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         client2_.IDZIPCODE as IDZIPCODE215_1_,
22:22:29,223 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         profile3_.id as id178_2_,
22:22:29,223 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         profile3_.CV_FILENAME_PATH as CV2_178_2_,
22:22:29,223 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         profile3_.birthdate as birthdate178_2_,
22:22:29,224 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         profile3_.IDCIVILITY as IDCIVILITY178_2_,
22:22:29,225 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         profile3_.IDCOMPANY as IDCOMPANY178_2_,
22:22:29,225 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         profile3_.IDCOUNTRY as IDCOUNTRY178_2_,
22:22:29,226 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         profile3_.deleted as deleted178_2_,
22:22:29,226 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         profile3_.disponibility as disponib5_178_2_,
22:22:29,226 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         profile3_.ENTRANCE_DATE as ENTRANCE6_178_2_,
22:22:29,227 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         profile3_.EXIT_DATE as EXIT7_178_2_,
22:22:29,227 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         profile3_.EXTERNE as EXTERNE178_2_,
22:22:29,227 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         profile3_.firstname as firstname178_2_,
22:22:29,228 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         profile3_.IMMEDIATE_DISPONIBILITY as IMMEDIATE10_178_2_,
22:22:29,228 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         profile3_.lastname as lastname178_2_,
22:22:29,228 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         profile3_.mail as mail178_2_,
22:22:29,228 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         profile3_.OWNER as OWNER178_2_,
22:22:29,229 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         profile3_.photo as photo178_2_,
22:22:29,229 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         profile3_.SOCIALESECURITYCODE as SOCIALE14_178_2_,
22:22:29,230 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         profile3_.UPDATE_TIMESTAMP as UPDATE15_178_2_,
22:22:29,230 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         profile3_.IDZIPCODE as IDZIPCODE178_2_,
22:22:29,230 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         user4_.id as id165_3_,
22:22:29,231 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         user4_.IDCIVILITY as IDCIVILITY165_3_,
22:22:29,231 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         user4_.ID_COMPANY as ID10_165_3_,
22:22:29,231 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         user4_.Fax_Number as Fax2_165_3_,
22:22:29,232 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         user4_.First_Name as First3_165_3_,
22:22:29,232 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         user4_.Last_Name as Last4_165_3_,
22:22:29,232 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         user4_.Mail_Address as Mail5_165_3_,
22:22:29,233 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         user4_.password as password165_3_,
22:22:29,233 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         user4_.phone_Number as phone7_165_3_,
22:22:29,233 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         user4_.photo as photo165_3_,
22:22:29,234 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         user4_.IDPROFILE as IDPROFILE165_3_,
22:22:29,234 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         user4_.IDUSERROLE as IDUSERROLE165_3_,
22:22:29,235 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         companycon5_.id as id219_4_,
22:22:29,235 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         companycon5_.ID_COMPANY_CONTRACT as ID11_219_4_,
22:22:29,235 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         companycon5_.dateAvenant as dateAven2_219_4_,
22:22:29,236 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         companycon5_.endDate as endDate219_4_,
22:22:29,236 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         companycon5_.invoicePaymentDay as invoiceP4_219_4_,
22:22:29,236 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         companycon5_.rate as rate219_4_,
22:22:29,237 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         companycon5_.reference as reference219_4_,
22:22:29,237 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         companycon5_.secondRate as secondRate219_4_,
22:22:29,238 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         companycon5_.startDate as startDate219_4_,
22:22:29,238 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         companycon5_.thirdRate as thirdRate219_4_,
22:22:29,238 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         companycon5_.UPDATE_TIMESTAMP as UPDATE10_219_4_,
22:22:29,239 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         companycon5_.IDUSER as IDUSER219_4_,
22:22:29,239 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         companycon6_.id as id218_5_,
22:22:29,239 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         companycon6_.IDCLIENT as IDCLIENT218_5_,
22:22:29,240 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         companycon6_.endDate as endDate218_5_,
22:22:29,240 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         companycon6_.invoicePaymentDay as invoiceP3_218_5_,
22:22:29,240 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         companycon6_.percentage_Extra_Hours as percentage4_218_5_,
22:22:29,241 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         companycon6_.IDPROFILE as IDPROFILE218_5_,
22:22:29,241 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         companycon6_.rate as rate218_5_,
22:22:29,241 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         companycon6_.reference as reference218_5_,
22:22:29,242 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         companycon6_.secondRate as secondRate218_5_,
22:22:29,242 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         companycon6_.startDate as startDate218_5_,
22:22:29,242 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         companycon6_.thirdRate as thirdRate218_5_,
22:22:29,242 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         companycon6_.UPDATE_TIMESTAMP as UPDATE10_218_5_,
22:22:29,243 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         companycon6_.IDUSER as IDUSER218_5_,
22:22:29,243 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         user7_.id as id165_6_,
22:22:29,243 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         user7_.IDCIVILITY as IDCIVILITY165_6_,
22:22:29,243 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         user7_.ID_COMPANY as ID10_165_6_,
22:22:29,243 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         user7_.Fax_Number as Fax2_165_6_,
22:22:29,244 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         user7_.First_Name as First3_165_6_,
22:22:29,244 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         user7_.Last_Name as Last4_165_6_,
22:22:29,244 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         user7_.Mail_Address as Mail5_165_6_,
22:22:29,246 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         user7_.password as password165_6_,
22:22:29,246 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         user7_.phone_Number as phone7_165_6_,
22:22:29,246 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         user7_.photo as photo165_6_,
22:22:29,247 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         user7_.IDPROFILE as IDPROFILE165_6_,
22:22:29,247 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         user7_.IDUSERROLE as IDUSERROLE165_6_,
22:22:29,247 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         companycon8_.id as id233_7_,
22:22:29,247 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         companycon8_.CODE_EVENT_TYPE as CODE2_233_7_,
22:22:29,247 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         companycon8_.libelle as libelle233_7_,
22:22:29,248 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         user9_.id as id165_8_,
22:22:29,248 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         user9_.IDCIVILITY as IDCIVILITY165_8_,
22:22:29,248 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         user9_.ID_COMPANY as ID10_165_8_,
22:22:29,248 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         user9_.Fax_Number as Fax2_165_8_,
22:22:29,249 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         user9_.First_Name as First3_165_8_,
22:22:29,249 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         user9_.Last_Name as Last4_165_8_,
22:22:29,249 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         user9_.Mail_Address as Mail5_165_8_,
22:22:29,250 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         user9_.password as password165_8_,
22:22:29,250 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         user9_.phone_Number as phone7_165_8_,
22:22:29,250 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         user9_.photo as photo165_8_,
22:22:29,251 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         user9_.IDPROFILE as IDPROFILE165_8_,
22:22:29,251 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         user9_.IDUSERROLE as IDUSERROLE165_8_,
22:22:29,252 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         civility10_.id as id167_9_,
22:22:29,252 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         civility10_.civility as civility167_9_,
22:22:29,253 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         company11_.id as id169_10_,
22:22:29,253 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         company11_.ID_TYPE as ID12_169_10_,
22:22:29,253 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         company11_.companyname as companyn2_169_10_,
22:22:29,253 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         company11_.CONTACT_MAIL as CONTACT3_169_10_,
22:22:29,254 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         company11_.CONTACT_NAME as CONTACT4_169_10_,
22:22:29,254 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         company11_.CONTACT_PHONE as CONTACT5_169_10_,
22:22:29,254 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         company11_.description as descript6_169_10_,
22:22:29,254 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         company11_.logo as logo169_10_,
22:22:29,255 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         company11_.siren as siren169_10_,
22:22:29,255 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         company11_.staffing as staffing169_10_,
22:22:29,255 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         company11_.street as street169_10_,
22:22:29,256 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         company11_.website as website169_10_,
22:22:29,256 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         company11_.IDZIPCODE as IDZIPCODE169_10_,
22:22:29,257 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         profile12_.id as id178_11_,
22:22:29,257 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         profile12_.CV_FILENAME_PATH as CV2_178_11_,
22:22:29,257 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         profile12_.birthdate as birthdate178_11_,
22:22:29,257 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         profile12_.IDCIVILITY as IDCIVILITY178_11_,
22:22:29,258 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         profile12_.IDCOMPANY as IDCOMPANY178_11_,
22:22:29,258 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         profile12_.IDCOUNTRY as IDCOUNTRY178_11_,
22:22:29,258 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         profile12_.deleted as deleted178_11_,
22:22:29,259 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         profile12_.disponibility as disponib5_178_11_,
22:22:29,259 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         profile12_.ENTRANCE_DATE as ENTRANCE6_178_11_,
22:22:29,259 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         profile12_.EXIT_DATE as EXIT7_178_11_,
22:22:29,259 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         profile12_.EXTERNE as EXTERNE178_11_,
22:22:29,260 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         profile12_.firstname as firstname178_11_,
22:22:29,260 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         profile12_.IMMEDIATE_DISPONIBILITY as IMMEDIATE10_178_11_,
22:22:29,260 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         profile12_.lastname as lastname178_11_,
22:22:29,260 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         profile12_.mail as mail178_11_,
22:22:29,261 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         profile12_.OWNER as OWNER178_11_,
22:22:29,261 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         profile12_.photo as photo178_11_,
22:22:29,261 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         profile12_.SOCIALESECURITYCODE as SOCIALE14_178_11_,
22:22:29,261 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         profile12_.UPDATE_TIMESTAMP as UPDATE15_178_11_,
22:22:29,262 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         profile12_.IDZIPCODE as IDZIPCODE178_11_,
22:22:29,262 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         userrole13_.id as id217_12_,
22:22:29,262 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         userrole13_.LABEL as LABEL217_12_ 
22:22:29,262 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)     from
22:22:29,262 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         company_contract_history companycon0_ 
22:22:29,263 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)     inner join
22:22:29,263 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         company_contract companycon1_ 
22:22:29,263 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)             on companycon0_.ID_COMPANY_CONTRACT=companycon1_.id 
22:22:29,263 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)     left outer join
22:22:29,263 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         client client2_ 
22:22:29,264 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)             on companycon1_.IDCLIENT=client2_.id 
22:22:29,264 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)     left outer join
22:22:29,264 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         profile profile3_ 
22:22:29,264 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)             on companycon1_.IDPROFILE=profile3_.id 
22:22:29,264 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)     left outer join
22:22:29,265 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         user user4_ 
22:22:29,265 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)             on companycon1_.IDUSER=user4_.id 
22:22:29,265 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)     inner join
22:22:29,265 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         company_contract_avenant companycon5_ 
22:22:29,265 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)             on companycon0_.ID_COMPANY_CONTRACT_AVENANT=companycon5_.id 
22:22:29,266 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)     left outer join
22:22:29,266 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         company_contract companycon6_ 
22:22:29,266 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)             on companycon5_.ID_COMPANY_CONTRACT=companycon6_.id 
22:22:29,266 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)     left outer join
22:22:29,266 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         user user7_ 
22:22:29,267 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)             on companycon5_.IDUSER=user7_.id 
22:22:29,267 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)     inner join
22:22:29,267 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         company_contract_event_type companycon8_ 
22:22:29,267 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)             on companycon0_.ID_EVENT_TYPE=companycon8_.id 
22:22:29,267 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)     inner join
22:22:29,268 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         user user9_ 
22:22:29,268 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)             on companycon0_.ID_USER=user9_.id 
22:22:29,268 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)     left outer join
22:22:29,268 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         civility civility10_ 
22:22:29,268 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)             on user9_.IDCIVILITY=civility10_.id 
22:22:29,269 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)     left outer join
22:22:29,269 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         company company11_ 
22:22:29,269 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)             on user9_.ID_COMPANY=company11_.id 
22:22:29,269 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)     left outer join
22:22:29,269 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         profile profile12_ 
22:22:29,270 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)             on user9_.IDPROFILE=profile12_.id 
22:22:29,270 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)     left outer join
22:22:29,270 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         user_role userrole13_ 
22:22:29,270 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)             on user9_.IDUSERROLE=userrole13_.id 
22:22:29,270 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)     where
22:22:29,271 INFO  [stdout] (http-localhost-127.0.0.1-8888-1)         companycon0_.id=?

22:22:29,273 GRAVE [org.zkoss] (http-localhost-127.0.0.1-8888-1) : javax.persistence.EntityNotFoundException: Unable to find com.epro.model.CompanyContractHistory with id 108

EDIT 5 :

I changed the query to : "SELECT gb FROM GlobalHistory gb LEFT JOIN FETCH gb.companyContractHistory cc where gb.id = 544 and cc.id = 108"

And with that I did get the line !! (Notice I'm using JOIN FETCH), any one care to explain ?

OddDev
  • 1,521
  • 7
  • 23
  • 45
  • 1
    Can you post your `ContractHistory` entity? – Andreas Oct 06 '17 at 20:14
  • yep I did, but it's a normal entity, and just a hint, I'm retreiving the data from `SalaryHistory` without any problem and they have the same mapping – OddDev Oct 06 '17 at 20:17
  • Are you able to turn on [sql logging](https://stackoverflow.com/a/1713464/3430807)? If so can you post some of the log? – Andreas Oct 06 '17 at 20:19
  • I think you have an entry in `ID_CONTRACT_HISTORY=108` in your `History` table but this id does not exist in your `ContractHistory` table. Can you post some screenshots of the contents of those 2 database tables ? – pleft Oct 06 '17 at 20:24
  • I've added screenshot, @Andreas, Just a sec I'll give the query – OddDev Oct 06 '17 at 20:31
  • @Andreas, SQL query added – OddDev Oct 06 '17 at 20:36
  • in which entity is this eventDate attribute there??? – Madhu Reddy Oct 06 '17 at 20:41
  • it's in History Entity – OddDev Oct 06 '17 at 20:44
  • Do you get any result if you run: `CompanyContractHistory result = entityManager .createQuery( "SELECT ch FROM CompanyContractHistory ch WHERE ch.id=108", CompanyContractHistory.class).getSingleResult();` ?? – pleft Oct 06 '17 at 20:45
  • At the point where your dao is pulling back `History`, is a transaction in progress? Perhaps from within the transaction the row has been deleted. – Andreas Oct 06 '17 at 20:47
  • @pleft, yep I'm getting a result and it's the actual data* – OddDev Oct 06 '17 at 20:50
  • @Andreas, before I execute this query I have another one just before that that counts the rows in the History entity – OddDev Oct 06 '17 at 20:52
  • eeeeh, Just now I puted the query @pleft gave me just before the other one, like this : `CompanyContractHistory resultc = entityManager .createQuery("SELECT ch FROM CompanyContractHistory ch WHERE ch.id=108", CompanyContractHistory.class) .getSingleResult(); List result = new ArrayList(); result = entityManager .createQuery( "SELECT h FROM History h ORDER BY gb.eventDate DESC", History.class) .setMaxResults(gHistoriesCount).getResultList();` – OddDev Oct 06 '17 at 20:53
  • And I did receive the line 108 !! – OddDev Oct 06 '17 at 20:55
  • Is there a difference between CompanyContractHistory and ContractHistory ? – Andreas Oct 06 '17 at 21:05
  • if it's like @Andreas said about the transaction(comment N°10), how should I proceed? – OddDev Oct 06 '17 at 21:05
  • @Andreas, no it's the same entity, i've just changed the names for the pupose of this post – OddDev Oct 06 '17 at 21:06
  • If you are getting 108 back while in the same transaction that isn't a problem. – Andreas Oct 06 '17 at 21:08
  • Yes I'm getting back that 108 line in the same transaction – OddDev Oct 06 '17 at 21:14
  • I'm out of ideas, sorry. If you post a minimal verifiable example, I'd be glad to pull it down and run it. – Andreas Oct 06 '17 at 21:46
  • Ok, thanks, I'll try to do that – OddDev Oct 06 '17 at 23:14
  • Are you getting this exception inside `findLastHistoriesByCount` or after returning, may be the entity is detached so you are not able to navigate its graph further more. – osama yaccoub Oct 09 '17 at 07:40
  • After `findLastHistoriesByCount`, But I've tried and removed the mentionned method and the same exception occur again* – OddDev Oct 09 '17 at 08:33
  • https://stackoverflow.com/questions/73234549/javax-persistence-entitynotfoundexception-unable-to-find-entity-with-id – KJEjava48 Aug 05 '22 at 06:50

4 Answers4

38

try with:

@ManyToOne(fetch = FetchType.LAZY)
@NotFound(action = NotFoundAction.IGNORE)
Khalil M
  • 1,788
  • 2
  • 22
  • 36
9

I found the solution :

In my ContractHistory I have :

public class ContractHistory extends implements Serializable {

  private static final long serialVersionUID = 1L;

  @Id
  @GeneratedValue(strategy=GenerationType.AUTO)
  private int id;

  @ManyToOne(optional = false)
  @JoinColumn(name="ID_COMPANY_CONTRACT", nullable = true)
  private CompanyContract companyContract;

  ...
}

Once I removed optional = false I did get the data without any Problem.

FIW : When you make the association mandatory (i.e. optional=false), it trusts you and assumes that an address exists, since the association is mandatory. So it directly populates the address field with a proxy, knowing that there is an address referencing the person (SOF).

Thomas Stubbe
  • 1,945
  • 5
  • 26
  • 40
OddDev
  • 1,521
  • 7
  • 23
  • 45
  • 4
    that was not even in your question. :( – Stelium Oct 09 '17 at 12:15
  • 1
    Yeah I didn't it was relevant and it's impact was in the SQL Query I posted :) – OddDev Oct 09 '17 at 12:28
  • @MoatezBouhdid I experienced this problem yesterday when I switched from Postgresql to Oracle. Turned out the table constraints were not set in Oracle. It worked once I added the foreign keys. Could this have been your initial situation? – kosgeinsky Mar 09 '18 at 05:42
  • @MoatezBouhdid I am facing a similar issue but removing the optinal=false is also not working in my case. Can you suggest any other option. – Nadeem Haider Dec 06 '19 at 18:13
  • 2
    @NadeemHaider pay attention about FetchTyp. In my case it was set up to EAGER loading for the other side in the bidirectional relation. It should be LAZY – david00f Feb 24 '20 at 11:57
  • @dappler that was my problem (missing `(fetch = FetchType.LAZY)` in other attribute) thank you very much – Gaspar Apr 07 '20 at 18:48
  • @MoatezBouhdid I have exactly the same problem and its not working with this solution also.. – KJEjava48 Aug 03 '22 at 09:51
  • @MoatezBouhdid https://stackoverflow.com/questions/73234549/javax-persistence-entitynotfoundexception-unable-to-find-entity-with-id – KJEjava48 Aug 05 '22 at 06:51
-1

I got similar problem| Error it self explaining problem 100% correctly that there is no record with particular ID

in My case I have 2 classes

  1. Book
  2. Publisher no mapping to books !!!

earlier it was working fine (smooth)

but have have changed class name publisher to polishers with s so in mysql there were two table one with new name publishers with s and publisher publisher table has all the data but publishers with s has no data that's why it throwing

"org.springframework.orm.jpa.JpaObjectRetrievalFailureException: Unable to find com.example.Books.Publishers.entity.Publisher with id 19"

error in my case

Solution:: remove the ambiguous table at the backend in my case i dropped publishers with s table and renamed publisher table name with publishers with s

I am pretty sure this will help you @Moatez Bouhdid

-1

I found a solution:

Use cascade = CascadeType.ALL with the Mapping annotation. In my case I used @OneToMany mapping like this:

@OneToMany(mappedBy = "column_name_from_anther_table", cascade = CascadeType.ALL)
flyingfishcattle
  • 1,817
  • 3
  • 14
  • 25