Java transient vs @Transient: Serialization and Persistence

📘 Java Learning | transient vs @Transient (Real-Time Example) Today I learned a very important Java concept that often creates confusion in real projects and interviews 👇 🔹 transient (Core Java) transient is a Java keyword Used to exclude a variable from serialization When an object is converted into a byte stream, transient fields are NOT saved 👉 After deserialization, these fields get default values: null → Objects 0 → Numbers false → Boolean class User implements Serializable { String username; transient String password; } 📌 Real-time use case: Passwords, OTPs, session data should never travel over the network or be stored in files. 🔹 @Transient (JPA / Hibernate) @Transient is a JPA annotation It tells Hibernate NOT to persist the field in the database The field exists only in memory, not in DB tables 🖼️ Image Explanation: The attached image shows: How sensitive fields are skipped during serialization How transient values become default after deserialization How @Transient fields are never stored in DB 📌 Key Takeaway: ✔ transient → controls serialization ✔ @Transient → controls database persistence ✔ Knowing this difference is crucial for real-time projects & interviews #Java #CoreJava #SpringBoot #Hibernate #JPA #BackendDevelopment #JavaDeveloper #Serialization #ProgrammingConcepts #DailyLearning #SoftwareEngineering

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories