Hibernate 异常“无法解析属性”使用条件和限制进行查询时
我在 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
}
列 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);
Criterion
对象是在方法中创建的,它可以用于其他一对多 表或可以包含其他列,因此我无法使用方法 getUsers()
。
不幸的是,上面的代码给出了以下异常:
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 ...
可能是什么问题?
编辑:
在user759837建议的更改之后(Criterion c = Restrictions.eq("group", 1);
),当我调用 < code>criteria.list(),我收到此错误消息: 无法通过 com.example.Group.id 的反射 getter 获取字段值
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)
...
我
@MappedSuperclass
public abstract class BaseModel {
@Id
@GeneratedValue
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
尝试使用 <代码>长id 也是如此,但这是同样的错误。
编辑2:
经过大量挖掘,看起来Criterion
对象应该接收一个组对象作为参数,而不是一个id:Restrictions.eq("group ", {A_GROUP_OBJECT});
我可以向那里发送一个 id 吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的列是group_id,您应该使用group属性
...
Criterion c = Restrictions.eq("group", 1); // 组的 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:
如果您使用oracle作为数据库,原因可能是group_id是其中的关键字。将名称更改为其他名称并尝试。
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.