JPAはDBのテーブルを見ていません
-
07-09-2020 - |
質問
を実行したとき
@namedQuery(name="groupshasstudent.findbygroupsidgroups"、groupshastudent gからG.GroupShasStudentPk.GroupSidGroups=:GroupIDGroups ")、
GroupShasStudent のエンティティは正しい結果を生成します。しかし別のNativeQuery
getEntityManager().
createNativeQuery(
"SELECT d.idDiscipline, d.name, gy.idGroupsYear, gy.year, g.idGroups "
+ "FROM Discipline d, Groupsyear gy, Groups g, GroupsHasStudent ghs "
+ "WHERE ghs.groupsHasStudentPK.groupsidGroups=2").
getResultList();
.
例外をスローする
Internal Exception:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:
Table 'mydb.groupshasstudent' doesn't exist
.
実際にはDBにそのような表がありませんが、正しい名前は* GROUPS_HAS_STUDENT *が@table:
で指定されています。(私のエンティティ:)
@Entity
@Table(name = "groups_has_student")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "GroupsHasStudent.findAll", query = "SELECT g FROM GroupsHasStudent g"),
@NamedQuery(name = "GroupsHasStudent.findByGroupsidGroups", query = "SELECT g FROM GroupsHasStudent g WHERE g.groupsHasStudentPK.groupsidGroups = :groupsidGroups"),
@NamedQuery(name = "GroupsHasStudent.findByStudentUserslogin", query = "SELECT g FROM GroupsHasStudent g WHERE g.groupsHasStudentPK.studentUserslogin = :studentUserslogin")})
public class GroupsHasStudent implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
protected GroupsHasStudentPK groupsHasStudentPK;
public GroupsHasStudent() {
}
.
MySQLのテーブルの名前を変更しても GroupShasStudent には、別の例外
があります。Unknown column 'ghs.groupsHasStudentPK.studentUserslogin' in 'where clause'
.
іцесумно..
解決
ネイティブ(SQL)クエリではなく、JPQLクエリです。したがって、createQuery()
の代わりにcreateNativeQuery()
を使用する必要があります。
所属していません StackOverflow