jpa没有在db中看到表

时间:2011-05-20 15:20:28

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

执行时

@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中没有这样的表,但正确的名称是* 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'

Іцесумно..

1 个答案:

答案 0 :(得分:1)

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