I have a OneToMany relation in hibernate defined like this:
@Entity
@Table(name = "groups")
public class Group extends BaseModel {// BaseModel defines id as @Id and @GeneratedValue
@OneToMany
@JoinColumn(name = "group_id")
private List<User> users;
// other fields, getters and setters omitted
}
@Entity
@Table(name = "users")
public class User extends BaseModel {
@ManyToOne
@JoinColumn(name = "group_id")
private Group group;
// other fields, getters and setters omitted
}
Column group_id
is in the users table.
Calling methods Group.getUsers()
and User.getGroup()
work fine. But I also need to do a query after the column group_id
:
Criteria criteria = Activator.getDefault().getSQLSession().createCriteria(User.class);
Criterion c = Restrictions.eq("group_id", 1); // an id of a group
criteria.add(c);
The Criterion
object is created in a method, and it can be for other one-to-many tables or can contain other columns, so I can't use method getUsers()
.
Unfortunatelly, the code above gives the following exception:
org.hibernate.QueryException: could not resolve property: group_id of: com.example.User
at org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:81)
at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:75)
at org.hibernate.persister.entity.AbstractEntityPersister.getSubclassPropertyTableNumber(AbstractEntityPersister.java:1482)
at org.hibernate.persister.entity.BasicEntityPropertyMapping.toColumns(BasicEntityPropertyMapping.java:62)
and so on ...
What could be the problem?
Edit:
After the change that user759837 suggested (Criterion c = Restrictions.eq("group", 1);
), when I call criteria.list()
, I get this error message: could not get a field value by reflection getter of com.example.Group.id
java.lang.IllegalArgumentException: Can not set java.lang.Long field com.example.BaseModel.id to java.lang.Long
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(Unknown Source)
at sun.reflect.UnsafeObjectFieldAccessorImpl.get(Unknown Source)
at java.lang.reflect.Field.get(Unknown Source)
at org.hibernate.property.DirectPropertyAccessor$DirectGetter.get(DirectPropertyAccessor.java:59)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:227)
at org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:3875)
at org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:3583)
at org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:203)
at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:242)
at org.hibernate.type.EntityType.getIdentifier(EntityType.java:456)
at org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:130)
...
The BaseModel class is
@MappedSuperclass
public abstract class BaseModel {
@Id
@GeneratedValue
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
I tried with long id
too, but it's the same error.
Edit 2:
After a lot of digging, it looks that the Criterion
object should receive a group object as parameter, not an id: Restrictions.eq("group", {A_GROUP_OBJECT});
Could it be possible that I send there an id?
your column is group_id and you should use the property which is group ... Criterion c = Restrictions.eq("group", 1); // an id of a group ...
This seems to work:
Criterion c = Restrictions.eq("group.id", 1); // an id of a group
If you are using oracle as your DB, the reason might be that group_id is a keyword in it.Change the name to something else and try.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With