简体   繁体   English

想要的建议-SPRING JPA JoinColumns

[英]Advice wanted - SPRING JPA JoinColumns

Just started exploring SPRING and JPA. 刚刚开始探索SPRING和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. **为了保持附件的灵活性(以便我可以在需要附件的其他地方重用相同的表/类),我使用REF_TYPEREF_ID的组合来查找引用对象。

What would be the best way to do this? 最好的方法是什么?
- Should be even doing this? -还要这么做吗? Is there a better approach to the DB/Class design? 是否有更好的DB / Class设计方法?

- Should I use @JoinColumns? -我应该使用@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? -还是应该将加载附件的逻辑放到Service / DAO类中,而findByRefTypeAndRefId(String refType,long refId)函数会回退到JPARepository @Autowired接口?

You will not be able to map such relation in JPA. 您将无法在JPA中映射此类关系。

OneToMany or ManyToOne mappings (from Employee to Attachment or Attachment to Employee) are @ID based. OneToMany或ManyToOne映射(从员工到附件或从附件到员工)都是基于@ID的。 Your Employe has simple one column ID, but in your attachment join depends on value of two columns. 您的Employe具有简单的一列ID,但在附件中的连接取决于两列的值。

Assuming your attachment table would have records from other table including Employee, eg company, etc. 假设您的附件表将包含其他表(包括员工,例如公司等)中的记录。

First, You would have to write a JPA query, @JoinColumns would not work, since you should not map one column mapping to two tables as foreign-primary relationship. 首先,您将不得不编写一个JPA查询,@ JoinColumns将不起作用,因为您不应该将一个列映射作为外部主关系映射到两个表。

Logic to load any db queries should always go to DAO class. 加载任何数据库查询的逻辑应始终归入DAO类。 and you should follow, service and DAO patterns to make simplication of code. 并且您应该遵循服务和DAO模式来简化代码。

Solution1: If you want to go with current approach, use JPA query + your column type REF_TYPE as CHAR(1), eg E,C, etc. 解决方案1:如果要使用当前方法,请使用JPA查询+列类型REF_TYPE作为CHAR(1),例如,E,C等。

Solution2: Make separate tables for attachment (users, company,etc), remove REF_TYPE column. 解决方案2:为附件创建单独的表(用户,公司等),删除REF_TYPE列。 create a foreign primary relation between the id column and use join. 在id列之间创建外部主要关系并使用join。 This will make your code also look neat. 这将使您的代码看起来也很整洁。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM