当用于从我的 MySQL 数据库EntityManager
中获取条目时,我有一个相当简单的本机查询。Character
class CharacterReader(
private val entityManager: EntityManager
) {
fun getCharacter(id: String): Character? {
return entityManager.createNativeQuery("SELECT * FROM `character` WHERE id = :id", Character::class.java)
.setParameter("id", id)
.resultList.first() as Character
}
}
我的表有这个数据
// converted to json for readability
[
{
"id": "666",
"name": "Peter",
"x": 5,
"y": 0
}
]
DAO 是这样定义的:
@Serializable
@Entity
@Table(name = "\"character\"")
data class Character(
@Id
val id: String,
val name: String,
@Embedded
var position: Position
) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as Character
if (id != other.id) return false
return true
}
override fun hashCode(): Int {
return id.hashCode()
}
override fun toString(): String {
return "Character(id='$id', name='$name', position=$position)"
}
}
但是由于某种原因,我无法读取该值。抛出的错误是:
org.hibernate.MappingException: Unknown entity: com.barbarus.state.entity.Character
在我的 SQL 控制台中执行查询确实可以正常工作。