简体   繁体   English

使用不同的列值作为JPA实体类ID

[英]Using a distinct column value as JPA Entity Class Id

I have a situation where I have a join table defined in the database but not both of the single tables referenced in the join. 我遇到的情况是我在数据库中定义了一个联接表,但联接中没有引用两个表。 For example: 例如:

Student_Course
--------------
studentID
courseID

Course
------
courseID
title
description

Normally (using proper normalization) you would also have a Student table, and the Student_Course table would join Course and Student: 通常(使用适当的规范化),您还将有一个Student表,并且Student_Course表将联接Course和Student:

Student
-------
studentID

Is there a way in JPA with annotations (or otherwise) to pull unique studentIDs out of the Student_Course table and create an Entity class of Student, but also use Student_Course as a join table? JPA中是否有一种方法可以使用注释(或其他方法)从Student_Course表中拉出唯一的studentID并创建Student的Entity类,而且还可以使用Student_Course作为联接表? In SQL I would just use the SELECT DISTINCT statement. 在SQL中,我只使用SELECT DISTINCT语句。 I do not have control over the database and design, so I'm stuck with this. 我没有对数据库和设计的控制权,所以我受此困扰。

Normally, I would use the @JoinTable in the Student and Course classes to reference the Student_Course table. 通常,我将在Student和Course类中使用@JoinTable来引用Student_Course表。

Thanks! 谢谢!

The easiest solution is to create a database view out of your select distinct sql query, and map this view to an jpa entity. 最简单的解决方案是从选择的不同sql查询中创建数据库视图,然后将此视图映射到jpa实体。

A drawback is that creating the Student entity might not be possible from JPA (i know some database support inserting data in views, but i've never used this functionnality) 缺点是无法通过JPA创建Student实体(我知道某些数据库支持在视图中插入数据,但我从未使用过此功能)

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

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