【问题标题】:jpa doesn't see table in dbjpa 在 db 中看不到表
【发布时间】:2011-08-29 18:20:02
【问题描述】:

当我执行时

@NamedQuery(name = "GroupsHasStudent.findByGroupsidGroups", query = "SELECT g FROM GroupsHasStudent g WHERE g.groupsHasStudentPK.groupsidGroups = :groupsidGroups"),

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里没有这样的表,但是正确的名字是@Table中指定的*groups_has_student*:

(我的实体:)

@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'

І це сумно..

【问题讨论】:

    标签: database jpa jakarta-ee database-connection jpql


    【解决方案1】:

    这不是原生 (SQL) 查询,而是 JPQL 查询,因此您应该使用 createQuery() 而不是 createNativeQuery()

    【讨论】:

      猜你喜欢
      • 2019-07-07
      • 2015-02-23
      • 2020-11-15
      • 2016-02-01
      • 1970-01-01
      • 2022-01-11
      • 1970-01-01
      • 2018-05-31
      • 1970-01-01
      相关资源
      最近更新 更多