I'm making an application in Java EE (Jersey) with JPA where I have a problem with the uninitialized entities. I have 3 entities Car, Owner, House where the car can have multiple owners and owners can have multiple houses. When i return (entityManager.find) Car then owner is initialized. When i return House then Owner is initialized, but Car is not. I would like to be able to call something like House.getOwner().getCar().getId(). Now I must call find on House and then call find on Owner to get Car. How do I resolve this?
@Entity
@Table(name = "House")
public class HouseEntity {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "owner_id", nullable = false)
private OwnerEntity owner;
}
@Entity
@Table(name = "Owner")
public class OwnerEntity {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "car_id")
private CarEntity car;
@OneToMany(mappedBy = "owner")
@JoinColumn(name = "house", nullable = false)
private Set<HouseEntity> house;
}
@Entity
@Table(name = "Car")
public class CarEntity {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@OneToMany(mappedBy = "owner")
private Set<OwnerEntity> owner;
}
Edit1: Sorry there was mistake in mapping, classes working well. But problem with initialization remains.