[英]Hibernate exception 'could not resolve property' when doing a query with Criteria and Restrictions
I have a OneToMany relation in hibernate defined like this:我在 hibernate 中有一个 OneToMany 关系,定义如下:
@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.列
group_id
在 users 表中。
Calling methods Group.getUsers()
and User.getGroup()
work fine.调用方法
Group.getUsers()
和User.getGroup()
工作正常。 But I also need to do a query after the column group_id
:但我还需要在
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()
. Criterion
object 是在一个方法中创建的,它可以用于其他一对多表或可以包含其他列,所以我不能使用方法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
在user759837建议的更改(
Criterion c = Restrictions.eq("group", 1);
)之后,当我调用criteria.list()
时,我收到此错误消息: 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 BaseModel class 是
@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.我也尝试过使用
long id
,但这是同样的错误。
Edit 2:编辑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});
经过大量挖掘,看起来
Criterion
object 应该接收一个组 object 作为参数,而不是一个 id: Restrictions.eq("group", {A_GROUP_OBJECT});
Could it be possible that I send there an id?我可以在那里发送一个ID吗?
your column is group_id and you should use the property which is group ... Criterion c = Restrictions.eq(" group ", 1);您的列是group_id并且您应该使用属性group ... Criterion c = Restrictions.eq(" group ", 1); // an id of a group...
// 一个组的 id...
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.如果您使用 oracle 作为数据库,原因可能是 group_id 是其中的关键字。将名称更改为其他名称并尝试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.