无法将 java.lang.String 字段设置为 java.lang.String
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33303883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can not set java.lang.String field to java.lang.String
提问by Marcel
I am currently developing a small MMO application with a socket server. The database i am using is PostgreSQL and i am using the Hibernate ORM. I stumbled on to a exeption when requesting all the avatars an single user owns.
我目前正在开发一个带有套接字服务器的小型 MMO 应用程序。我使用的数据库是 PostgreSQL,我使用的是 Hibernate ORM。在请求单个用户拥有的所有头像时,我偶然发现了一个例外。
I got 3 classes involed, those are :
我参加了 3 个课程,它们是:
- GameServerClient
- Database
- Database.Queries
- 游戏服务器客户端
- 数据库
- 数据库.查询
When the user (client application) sends a request to the server via the sockets, a method is called which should return a JsonString of the all the Avatars.
当用户(客户端应用程序)通过套接字向服务器发送请求时,将调用一个方法,该方法应返回所有头像的 JsonString。
How ever, using the HQL query from UserOwnsAvatar where user = :username
and puting the result in an ArrayList of the UserOwnsAvatar object it returns an Can not set java.lang.String field nl.marcusink.mmo.server.database.table.User.username to java.lang.String
但是,使用 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
the full stackTrace is :
完整的堆栈跟踪是:
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
the query code is :
查询代码是:
Query query = session.createQuery("from UserOwnsAvatar where user = :username");
query.setParameter("username", username);
ArrayList<UserOwnsAvatar> ownedAvatars = (ArrayList<UserOwnsAvatar>) query.list();
The last line is the cause of the error, any ideas?
最后一行是错误的原因,有什么想法吗?
EDIT
编辑
@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;
The username here is equal to the username of the User object, which is :
这里的用户名等于 User 对象的用户名,即:
@Id
@Column(name = "username", unique = true, nullable = false)
private String username;
回答by SureshS
You will have to set the complete (or object with only relevant fields) instead of one particular value of that object.
您必须设置完整的(或仅具有相关字段的对象)而不是该对象的一个特定值。
What I understood is you are trying to set a String
while setting the parameter, but the column is of type User
. Hibernate is trying to call the getUsername
method on String
and that's why the error.
我的理解是您正在尝试String
设置参数,但该列的类型为User
。Hibernate 正在尝试调用该getUsername
方法String
,这就是错误的原因。
So change your code to something like this:
所以把你的代码改成这样:
User user = getSomeUser();
Query query = session.createQuery("from UserOwnsAvatar where user.username = :username");
query.setParameter("username", user);
回答by Forkmohit
user = :username is incorrect. user is not a string field of the entity. Either use a join or use hibernate criteria to create a alias for User and add a restriction, for ex, .add(Restriction.eq("user.username",username).
user = :username 不正确。用户不是实体的字符串字段。使用加入或使用休眠条件为用户创建别名并添加限制,例如,.add(Restriction.eq("user.username",username)。