0

I am here trying to fetch data using the primary key as one(1) from the database using JPA. but here data is not printing in the console after the query is successfully run. Here is the code:

public void getMobData() {
    Optional<Mobile> getMobile_Data = mobileRepo.findById(1);
    if(getMobile_Data.isPresent())
    {System.out.println("Data is present::"+getMobile_Data.get());}
}

Output:

Hibernate: select m1_0.model_id,m1_0.mobile_company,m1_0.model_name,m1_0.year from mobile m1_0 where m1_0.model_id=?
**Data is present::com.Mapping.Entity.Mobile@79c849c7**
[2m2023-08-26T13:34:10.441+05:30[0;39m [32m INFO[0;39m [35m41892[0;39m [2m---[0;39m [2m[ionShutdownHook][0;39m [36mj.LocalContainerEntityManagerFactoryBean[0;39m [2m:[0;39m Closing JPA EntityManagerFactory for persistence unit 'default'
[2m2023-08-26T13:34:10.445+05:30[0;39m [32m INFO[0;39m [35m41892[0;39m [2m---[0;39m [2m[ionShutdownHook][0;39m [36mcom.zaxxer.hikari.HikariDataSource      [0;39m [2m:[0;39m HikariPool-1 - Shutdown initiated...
[2m2023-08-26T13:34:10.464+05:30[0;39m [32m INFO[0;39m [35m41892[0;39m [2m---[0;39m [2m[ionShutdownHook][0;39m [36mcom.zaxxer.hikari.HikariDataSource  

What is the mistake, and how do I fix it?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 2
    But the data *is* printed in the console, I see it in your snippet above. Do you mean why you are getting "com.Mapping.Entity.Mobile@79c849c7"? That is because you did not override the `toString()` method in class `Mobile`. See: [How do I print my Java object without getting "SomeType@2f92e0f4"?](https://stackoverflow.com/questions/29140402/how-do-i-print-my-java-object-without-getting-sometype2f92e0f4) – Jesper Aug 26 '23 at 08:31
  • Thnks @Jesper.....But after adding toString method in Mobile Class , I am getting new error like "Exception in thread "main" org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.Mapping.Entity.Mobile.specs: could not initialize proxy - n" – pmjangid Aug 26 '23 at 08:59
  • attach your Mobile entity code with the problem statement. – Zakaria Hossain Aug 26 '23 at 12:49

0 Answers0