错误 java.sql.SQLException: 未找到列 'id'

Posted

技术标签:

【中文标题】错误 java.sql.SQLException: 未找到列 \'id\'【英文标题】:ERROR java.sql.SQLException: Column 'id' not found错误 java.sql.SQLException: 未找到列 'id' 【发布时间】:2019-11-11 16:08:23 【问题描述】:

执行查询计数 (*) 后,出现意外错误。我使用工作台图形界面满足了这个要求,那里一切都很好。在我的代码中,发生错误。我不明白原因。

这是执行我的数据库工作的实体管理器:

@PersistenceContext
    private val entityManager: EntityManager? = null

    fun count(request: HttpServletRequest): Int 
        val query = entityManager!!.createNativeQuery("SELECT COUNT(*) FROM resume r", Resume::class.java)
        return (query.getSingleResult() as Number).toInt()
    

这是一个你需要获得数量的课程:

@Entity
@ValidWorkType
@ValidProfField
@ValidEducation
@ValidAddress
@ValidDescResume
data class Resume (

        @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
        var id: Long? = null,

        @ManyToOne(fetch = FetchType.LAZY)
        var user: User? = null,

        var datecreate: LocalDateTime = LocalDateTime.now(),

        @get:Size(min = 1, max = 60, message = "error.work.profession")
        var profession: String? = null,

        @OneToOne(fetch = FetchType.LAZY, cascade = [CascadeType.ALL], orphanRemoval = true)
        @get:Valid
        var numberPhone: NumberPhone? = null,

        @get:Email(message = "text.error.user.email")
        @get:Size(min = 1, max = 100, message = "text.error.user.email.size")
        var email: String? = null,

        @ManyToOne(cascade = [CascadeType.MERGE])
        @get:NotNull(message = "error.work.prof.field")
        override var profField: ProfField? = null,

        @ManyToOne(cascade = [CascadeType.MERGE])
        @get:NotNull(message = "error.work.work.type")
        override var workType: WorkType? = null,

        @OneToOne(cascade = [CascadeType.ALL], orphanRemoval = true)
        @get:Valid
        var wage: Wage? = null,

        @ManyToOne
        override var education: Education? = null,

        var experience: Int? = null,

        @OneToOne(cascade = [CascadeType.ALL], fetch = FetchType.LAZY, orphanRemoval = true)
        @get:Valid
        override var description: DescriptionResume? = null,

        @OneToOne(cascade = [CascadeType.ALL], orphanRemoval = true)
        override var address: Address? = null,

        @OneToOne(fetch = FetchType.LAZY, cascade = [CascadeType.ALL], orphanRemoval = true)
        var photo: ContainerSquareImages? = null,

        @OneToOne(cascade = [CascadeType.ALL], orphanRemoval = true)
        var views: Views = Views(),

        @Transient
        var rating: Int? = null

): ParentWorkType, ParentProfField, ParentEducation, ParentAddress, ParentDescResume 

        fun getPeriodExperience(): Period? 
                if (experience!=null) 
                        return Period.between(LocalDate.now(), LocalDate.now().plusDays(experience!!.toLong()))
                 else  return null 
        

        override fun equals(other: Any?): Boolean 
                if (this === other) return true
                if (javaClass != other?.javaClass) return false

                other as Resume

                if (user != other.user) return false
                if (profession != other.profession) return false
                if (numberPhone != other.numberPhone) return false
                if (email != other.email) return false
                if (profField != other.profField) return false
                if (workType != other.workType) return false
                if (wage != other.wage) return false
                if (education != other.education) return false
                if (experience != other.experience) return false
                if (description != other.description) return false
                if (address != other.address) return false

                return true
        

        override fun hashCode(): Int 
                var result = user?.hashCode() ?: 0
                result = 31 * result + (profession?.hashCode() ?: 0)
                result = 31 * result + (numberPhone?.hashCode() ?: 0)
                result = 31 * result + (email?.hashCode() ?: 0)
                result = 31 * result + (profField?.hashCode() ?: 0)
                result = 31 * result + (workType?.hashCode() ?: 0)
                result = 31 * result + (wage?.hashCode() ?: 0)
                result = 31 * result + (education?.hashCode() ?: 0)
                result = 31 * result + (experience?.hashCode() ?: 0)
                result = 31 * result + (description?.hashCode() ?: 0)
                result = 31 * result + (address?.hashCode() ?: 0)
                return result
        

        fun getYearsCalendar() 

        

        companion object 
                const val NAME_PARAM = "resume"
        

这里是错误日志本身不能理解为什么,为什么,请帮忙:

Hibernate: SELECT COUNT(*) FROM resume r
[WARN ] 2019-07-01 00:37:08.485 [http-nio-8080-exec-1] SqlExceptionHelper - SQL Error: 0, SQLState: S0022
[ERROR] 2019-07-01 00:37:08.485 [http-nio-8080-exec-1] SqlExceptionHelper - Column 'id' not found.
[ERROR] 2019-07-01 00:37:08.525 [http-nio-8080-exec-1] [dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute query] with root cause
java.sql.SQLException: Column 'id' not found.
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129) ~[mysql-connector-java-8.0.15.jar:8.0.15]
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) ~[mysql-connector-java-8.0.15.jar:8.0.15]
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89) ~[mysql-connector-java-8.0.15.jar:8.0.15]
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63) ~[mysql-connector-java-8.0.15.jar:8.0.15]
    at com.mysql.cj.jdbc.result.ResultSetImpl.findColumn(ResultSetImpl.java:581) ~[mysql-connector-java-8.0.15.jar:8.0.15]
    at com.mysql.cj.jdbc.result.ResultSetImpl.getLong(ResultSetImpl.java:928) ~[mysql-connector-java-8.0.15.jar:8.0.15]

【问题讨论】:

如果没有计数,您是否会收到此错误。意味着只是一个计划全选。或者尝试在计数语句中放置一个字段。 为 Hibernate 开启 SQL 日志并显示执行的语句是什么。 【参考方案1】:

createNativeQuery 的文档如下:

查询 createNativeQuery(String sqlString, 类结果类)

sqlString - 原生 SQL 查询字符串

resultClass - 结果实例的类

您的查询是:

SELECT COUNT(*) FROM resume r

这是访问resume 表,但它返回一个整数 - 而不是resume 实例。这就是您收到Column 'id' not found. 的原因。

解决方法是简单地将 Integer 作为结果类传递,所以:

val query = entityManager!!.createNativeQuery("SELECT COUNT(*) FROM resume r", Integer::class.java)

由于没有映射标量,解决方法是不传递结果类,所以:

val query = entityManager!!.createNativeQuery("SELECT COUNT(*) FROM resume r")

【讨论】:

不起作用 - org.hibernate.MappingException:未知实体:java.lang.Integer 抱歉,标量未映射,因此不应传递任何映射类。我已经更新了答案。【参考方案2】:

使用 javax.persistence.Query JPA 接口:选择 NamedQuery 或 TypedQuery

例子

@javax.persistence.NamedQuery(
    name="resumeCountTest", query = "SELECT COUNT(*) FROM resume r")
    public class Resume 
 -------------
Query query = entityManager.createNamedQuery("resumeCountTest");
long resumeCount = query.getSingleResult();
return resumeCount ;

更多信息Running JPA Queries

【讨论】:

需要创建动态查询【参考方案3】:

使用 JPQL 解决了问题:

@PersistenceContext
    private val entityManager: EntityManager? = null

    fun count(request: HttpServletRequest): Long 
        val query = entityManager!!.createQuery("SELECT COUNT(resume.id) FROM Resume resume")
        return query.singleResult as Long
    

【讨论】:

以上是关于错误 java.sql.SQLException: 未找到列 'id'的主要内容,如果未能解决你的问题,请参考以下文章

java.sql.SQLException: ORA-00604: 递归 SQL 级别 1 发生错误

错误 java.sql.SQLException: 未找到列 'id'

java.sql.SQLException:指定的 Oracle URL 无效错误

如何解决错误“java.sql.SQLException:结果集开始之前” [重复]

获取异常 java.sql.SQLException:套接字创建错误

错误:java.sql.SQLException:用户'root@localhost'@'localhost'的访问被拒绝(使用密码:是)