我目前正在开发带有套接字服务器的小型MMO应用程序.我使用的数据库是PostgreSQL,而我使用的是Hibernate ORM.
当请求一个用户拥有的所有头像时,我偶然发现了一个例外.
我参与了3个班级的学习,分别是:
> GameServerClient
>数据库
>数据库查询
当用户(客户端应用程序)通过套接字向服务器发送请求时,将调用一个方法,该方法应返回所有头像的JsonString.
但是,使用来自UserOwnsAvatar的HQL查询(其中user =:username)并将结果放入UserOwnsAvatar对象的ArrayList中,它将返回无法设置的java.lang.String字段nl.marcusink.mmo.server.database.table.User .username到java.lang.String
完整的stackTrace是:
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 ownedAvatars = (ArrayList) 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;
这里的用户名等于User对象的用户名,即:
@Id
@Column(name = "username", unique = true, nullable = false)
private String username;
解决方法:
您将必须设置完整(或仅具有相关字段的对象),而不是该对象的一个特定值.
我了解的是您正在尝试在设置参数时设置字符串,但是列的类型为User. Hibernate试图在String上调用getUsername方法,这就是错误的原因.
因此,将您的代码更改为如下所示:
User user = getSomeUser();
Query query = session.createQuery("from UserOwnsAvatar where user.username = :username");
query.setParameter("username", user);
标签:sockets,server,hibernate,java
来源: https://codeday.me/bug/20191119/2036948.html