java 使用条件和限制进行查询时,休眠异常“无法解析属性”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6103336/
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
Hibernate exception 'could not resolve property' when doing a query with Criteria and Restrictions
提问by True Soft
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.
Calling methods Group.getUsers()
and User.getGroup()
work fine. But I also need to do a query after the column group_id
:
列group_id
在用户表中。
调用方法Group.getUsers()
并User.getGroup()
正常工作。但我还需要在列之后做一个查询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-manytables or can contain other columns, so I can't use method getUsers()
.
该Criterion
对象是在一个方法中创建的,它可以用于其他一对多表,也可以包含其他列,所以我不能使用 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 user759837suggested (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 类是
@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
对象应该接收一个组对象作为参数,而不是一个 id:Restrictions.eq("group", {A_GROUP_OBJECT});
Could it be possible that I send there an id?
我可以向那里发送一个 id 吗?
采纳答案by True Soft
This seems to work:
这似乎有效:
Criterion c = Restrictions.eq("group.id", 1); // an id of a group
回答by anfy2002us
your column is group_idand you should use the property which is group... Criterion c = Restrictions.eq("group", 1); // an id of a group ...
您的列是group_id,您应该使用group属性 ... Criterion c = Restrictions.eq(" group", 1); // 一个组的 id ...
回答by Prasanna
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 是其中的关键字。将名称更改为其他名称并尝试。