Simple EJB3 mapping question

Discussions

EJB design: Simple EJB3 mapping question

  1. Simple EJB3 mapping question (1 messages)

    Hi everyone,

    I'm trying to perform what I think should be a straight forward mapping, however I can't find the answer in the documentation. Say I have 2 DB tables, EMPLOYEE and PARKING. Each one has numerous fields. My Employee entity bean currently looks like this:

    @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

  2. Simple EJB3 mapping question[ Go to top ]

    Hi Jim

    I think you aproach is right since you need data from both tables, i'm assuming you need to persist just the Employee entity, remember by default every field in an entity is persistent. When you don't need persistence you can add the @Transient annotation like this:

    @Transient

    @Column(name="PARK_SIZE", table = "PARK", updatable = false, insertable = false)

     

    Saludos!