jpa在数据库中看不到表

发布于 2024-11-08 17:13:50 字数 1789 浏览 6 评论 0原文

当我执行

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

of 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

实际上我在数据库中没有这样的表,但正确的名称是 *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'

І це сумно..

When I execute

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

of GroupsHasStudent entity, the result is generated correct. But another 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();

throws the Exception

Internal Exception:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:
Table 'mydb.groupshasstudent' doesn't exist

Actually I do don't have such table in db, but the correct name is *groups_has_student* which is specified in @Table:

(My entity:)

@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() {
    }

And even when I rename table in mysql to groupshasstudent there is another Exception

Unknown column 'ghs.groupsHasStudentPK.studentUserslogin' in 'where clause'

І це сумно..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

掀纱窥君容 2024-11-15 17:13:50

它不是本机 (SQL) 查询,而是 JPQL 查询,因此您应该使用 createQuery() 而不是 createNativeQuery()

It's not a native (SQL) query, it's a JPQL query, therefore you should use createQuery() instead of createNativeQuery().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文