ClassCastException 尝试将检索到的 JPA 对象转换为具体类
我不确定我做错了什么,但我有一个类,其中有一个类,所以当我保存技能类时,用户类也会被创建,所以当我进行连接时,我想将所有内容都拉入有一次,我遇到了 ClasscastException。
这就是我调用查询的方式。
val retrieved_obj = em.createNamedQuery("findAllSkillsByUser").setParameter("username", user.username ).getResultList().asInstanceOf[java.util.List[Skill]]
assertEquals(1, retrieved_obj.size())
val retrieved = retrieved_obj.get(0).asInstanceOf[Skill]
这是我的查询:
这就是 hibernate 在我的应用程序中实际执行的操作测试:
Hibernate: insert into users (firstName, lastName, username, id) values (?, ?, ?, ?)
Hibernate: insert into skills (datestarted, name, position, rating, skill_user_fk, id) values (?, ?, ?, ?, ?, ?)
Hibernate: select skill0_.id as id64_0_, user1_.id as id63_1_, skill0_.datestarted as datestar2_64_0_, skill0_.name as name64_0_, skill0_.position as position64_0_, skill0_.rating as rating64_0_, skill0_.skill_user_fk as skill6_64_0_, user1_.firstName as firstName63_1_, user1_.lastName as lastName63_1_, user1_.username as username63_1_ from skills skill0_ inner join users user1_ on skill0_.skill_user_fk=user1_.id where user1_.username=?
我预计问题出在 select 中的所有 user1 部分。
Skill 类基本上只有一些 setter/getter,所以我删除了大部分注释,但外键注释:
var id : Int = _
var name : String = ""
var position : Int = _
var dateStarted : Date = new Date()
var rating : SkillRating.Value = SkillRating.unknown
@OneToOne{val fetch = FetchType.EAGER, val cascade=Array(CascadeType.PERSIST, CascadeType.REMOVE)}
@JoinColumn{val name = "skill_user_fk", val nullable = false}
var user : User = _
这是 User 类:
var id : Int = _
var firstName : String = ""
var lastName : String = ""
var username : String = ""
这是我的错误:
java.lang.ClassCastException: [Ljava.lang.目的;无法转换为 jblack.resumeapp.lift.model.Skill
我不想只选择 Skill 属性,然后必须为用户执行另一个查询,因为随着我的类变得更加复杂,这会降低效率。
I am not certain what I am doing wrong, but I have a class that has a class within it, so when I save the Skill class the user class also gets created, so when I do the join and I want to pull everything in at one time, I get a classcastexception.
This is how I am calling my query.
val retrieved_obj = em.createNamedQuery("findAllSkillsByUser").setParameter("username", user.username ).getResultList().asInstanceOf[java.util.List[Skill]]
assertEquals(1, retrieved_obj.size())
val retrieved = retrieved_obj.get(0).asInstanceOf[Skill]
This is my query:<query><![CDATA[from Skill a JOIN a.user u WHERE u.username=:username]]></query>
This is what hibernate is actually doing in my test:
Hibernate: insert into users (firstName, lastName, username, id) values (?, ?, ?, ?)
Hibernate: insert into skills (datestarted, name, position, rating, skill_user_fk, id) values (?, ?, ?, ?, ?, ?)
Hibernate: select skill0_.id as id64_0_, user1_.id as id63_1_, skill0_.datestarted as datestar2_64_0_, skill0_.name as name64_0_, skill0_.position as position64_0_, skill0_.rating as rating64_0_, skill0_.skill_user_fk as skill6_64_0_, user1_.firstName as firstName63_1_, user1_.lastName as lastName63_1_, user1_.username as username63_1_ from skills skill0_ inner join users user1_ on skill0_.skill_user_fk=user1_.id where user1_.username=?
I expect the problem is all the user1 parts in the select.
The Skill class just has some setters/getters, basically, so I removed most of the annotations but the foreign key one:
var id : Int = _
var name : String = ""
var position : Int = _
var dateStarted : Date = new Date()
var rating : SkillRating.Value = SkillRating.unknown
@OneToOne{val fetch = FetchType.EAGER, val cascade=Array(CascadeType.PERSIST, CascadeType.REMOVE)}
@JoinColumn{val name = "skill_user_fk", val nullable = false}
var user : User = _
This is the User class:
var id : Int = _
var firstName : String = ""
var lastName : String = ""
var username : String = ""
This is my error:
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to jblack.resumeapp.lift.model.Skill
I would rather no just select the Skill attributes and then have to do another query for the user, as that will be less efficient as my classes get more complicated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将选择两者
Skill
和User
并返回包含这两者作为元素的对象数组。您可以这样对待它,也可以稍微重写您的查询:
仅选择
Skill
但获取并填充其User
关联。You're selecting both
Skill
andUser
and getting back an object array containing both as elements.You can either treat it as such or rewrite your query a bit:
to select only
Skill
but fetch and populate itsUser
association.