jpa在数据库中看不到表
-
07-09-2020 - |
题
当我执行时
@NamedQuery(name = "GroupsHasStudent.findByGroupsidGroups", query = "从 GroupsHasStudent g 中选择 g,其中 g.groupsHasStudentPK.groupsidGroups = :groupsidGroups"),
的 组有学生 实体,结果生成正确。但另一个 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
实际上我在数据库中没有这样的表,但正确的名称是 *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 中的表重命名为 组有学生 还有另一个异常
Unknown column 'ghs.groupsHasStudentPK.studentUserslogin' in 'where clause'
Іце сумно..
解决方案
它不是本机(SQL)查询,而是 JPQL 查询,因此您应该使用 createQuery()
代替 createNativeQuery()
.
不隶属于 StackOverflow