I am using Hibernate in my java project. I am having following entity structure:
@Entity
@Table(name = "car")
public class Car implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private long id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "engine_id", nullable = false)
private Engine engine;
}
Now when I load Car
entity from db, obviously Engine
entity won't be loaded as it is lazy loaded field, but the id (primary key) for the Engine
will be loaded.
Now if I do car.getEngine().getId(), then it is throwing LazyInitialization Exception. As Id part is already loaded, then why getId() is throwing exception?