我目前正在开发一个带有socket服务器的小型MMO应用程序。我在使用PostgreSQL数据库,使用Hibernate ORM。当我请求单个用户拥有的所有头像时,遇到了一个异常。
涉及到三个类:
- GameServerClient
- Database
- Database.Queries
当用户(客户端应用程序)通过sockets向服务器发送请求时,将调用一个方法,该方法应返回所有头像的JsonString。
但是,使用HQL查询from UserOwnsAvatar where user = :username
并将结果放入UserOwnsAvatar对象的ArrayList中时,会返回一个Can not set java.lang.String field nl.marcusink.mmo.server.database.table.User.username to java.lang.String
错误。
完整的堆栈跟踪为:
org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [private java.lang.String nl.marcusink.mmo.server.database.table.User.username] by reflection for persistent property [nl.marcusink.mmo.server.database.table.User#username] : Mjollnir94
at org.hibernate.property.access.spi.GetterFieldImpl.get(GetterFieldImpl.java:43)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:223)
at org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:4594)
at org.hibernate.type.EntityType.toLoggableString(EntityType.java:505)
at org.hibernate.internal.util.EntityPrinter.toString(EntityPrinter.java:87)
at org.hibernate.engine.spi.QueryParameters.traceParameters(QueryParameters.java:281)
at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:194)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1268)
at org.hibernate.internal.QueryImpl.list(QueryImpl.java:87)
at nl.marcusink.mmo.server.database.Database$Queries.avatarsRequest(Database.java:134)
at nl.marcusink.mmo.server.connection.GameServerClient.run(GameServerClient.java:91)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalArgumentException: Can not set java.lang.String field nl.marcusink.mmo.server.database.table.User.username to java.lang.String
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:58)
at sun.reflect.UnsafeObjectFieldAccessorImpl.get(UnsafeObjectFieldAccessorImpl.java:36)
at java.lang.reflect.Field.get(Field.java:393)
at org.hibernate.property.access.spi.GetterFieldImpl.get(GetterFieldImpl.java:39)
... 11 more
查询代码是:
Query query = session.createQuery("from UserOwnsAvatar where user = :username");
query.setParameter("username", username);
ArrayList<UserOwnsAvatar> ownedAvatars = (ArrayList<UserOwnsAvatar>) query.list();
最后一行是导致错误的原因,有什么想法吗?
编辑
@Id
@ManyToOne(targetEntity = User.class)
@JoinColumn(name = "username", nullable = false)
private User user;
@Id
@OneToOne(targetEntity = Avatar.class)
@JoinColumn(name = "avatar", nullable = false, unique = true)
private Avatar avatar;
这里的用户名等同于用户对象的用户名,即:
@Id
@Column(name = "username", unique = true, nullable = false)
private String username;
User
类中添加public void setUsername(String username) {this.username = username;}
。 - Blake Yarbroughpublic void setUsername(String username) { this.username = username; }
。我认为这不可能是问题所在。 - MarcelUnsafeFieldAccessorImpl.ensureObj
的源代码后,我得出结论,尽管错误消息关注的是值,但实际上是实例不属于User
类型,导致了这次尝试失败… - Holger