我在 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
在users表中。调用方法
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);
)之后,当我调用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)
...
BaseModel类是
@MappedSuperclass
public abstract class BaseModel {
@Id
@GeneratedValue
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
我也尝试过
long id
,但这是相同的错误。编辑2:
经过大量挖掘后,看来
Criterion
对象应该接收一个组对象作为参数,而不是id:Restrictions.eq("group", {A_GROUP_OBJECT});
我可以在那里发送一个ID吗?
最佳答案
您的列是 group_id ,您应该使用属性 group
...
条件c = Restrictions.eq(“ group ”,1);//群组的ID
...