简体   繁体   中英

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
-------
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? In SQL I would just use the SELECT DISTINCT statement. 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.

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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