mercredi 15 avril 2015

Advice wanted - SPRING JPA JoinColumns

Just started exploring SPRING and JPA. So bear with me, please.



Lets assume I have two tables



EMPLOYEE TABLE ATTACHMENT TABLE
--------------------------- -------------------------------------------
ID | FIRSTNAME | LASTNAME | ID | REF_TYPE | REF_ID | FILE
--------------------------- -------------------------------------------
1 | EMP1 | EMPL1 | 1 | "EMPLOYEE" | 1 | EMPL1_FIle1.jpg
--------------------------- -------------------------------------------
2 | EMP2 | EMPL2 | 2 | "EMPLOYEE" | 1 | EMPL1_FIle2.jpg
-------------------------------------------
3 | "EMPLOYEE" | 2 | EMPL2_FIle1.jpg


** To keep attachments flexible (so I can reuse the same table/class for other places where I need attachments), I use a combination of REF_TYPE and REF_ID to locate the referencing Object.


What would be the best way to do this?

- Should be even doing this? Is there a better approach to the DB/Class design?



- Should I use @JoinColumns? (But I am not sure how that would work...)



- Or should the logic to load attachments go into a Service/DAO class with a findByRefTypeAndRefId(String refType, long refId) function falling back to a JPARepository @Autowired interface?


Aucun commentaire:

Enregistrer un commentaire