@Entity
@Table(name="EMPLOYEES")
public class Employee implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = IDENTITY)
private int id;
private int blahBlah;
@ManyToOne
@JoinColumn(name="parkingId", table="PARK", insertable = false)
private Parking customerId;
..
}
Obviously at the moment I'm creating a relationship to entity Parking, which will cause a query that will fetch and populate an entire Parking object. I don't want that, instead, I just want to have a single field from table PARK to my Employee Entity (for instance, a "private String parkingSize").
How do I map something like that? I tried using a combination of @SecondaryTable and @Column like so:
@SecondaryTable(name = "PARK", pkJoinColumns = @PrimaryKeyJoinColumn(name = "id", referencedColumnName = "parkingId")) and @Column(name="PARK_SIZE", table = "PARK", updatable = false, insertable = false)
private String parkingSize; Then a simple "SELECT e FROM Employee e" does the trick, the field parkingSize is populated correctly. However when I try to persist a new Employee entity, I get exceptions because the container tries to add an entry to the secondary table too! Since I have already specified insertable=false, I suspect that my approach is not appropriate here. What is the right way to do this? Any help greatly appreciated.
Many thanks
Dimitris