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.
I got 3 classes involed, those are :
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.
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
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 :
@Id
@Column(name = "username", unique = true, nullable = false)
private String username;
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.
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);
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).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.