gpt4 book ai didi

java - 实体在映射时如何获取非主键值?

转载 作者:行者123 更新时间:2023-11-30 07:27:40 25 4
gpt4 key购买 nike

AssignCoScholastic.java

@Entity
@Table(name="assignCoScholastic")
public class AssignCoScholastic
{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int assginCoScholasticId;
@ManyToOne
@JoinColumn(name="assignClassId")
private AssignClass assignClass;
@ManyToOne
@JoinColumn(name="coscholasticId")
private CoScholastic coscholastic;
@ManyToOne
@JoinColumn(name="subCoScholasticId")
private SubCoScholasticActivity subCoScholasticActivity;
private String year;
@ManyToOne
@JoinColumn(name="school_id")
private SchoolModel schoolModel;

CoScholastic.java

@Entity
@Table(name="coscholastic")
public class CoScholastic {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int coScholasticId;
@Column(name="Coscholastic_Name")
private String coscholasticName;
// Getters and Setters

实现:

@Override
public List<Object[]> listOfAssignCoScholastics(int assiginedClass, int schoolId, String year) {
Session session=sessionFactory.openSession();
session.beginTransaction();
List<Object[]> listOfAssignCoScholastics=new ArrayList<Object[]>();
try
{
Criteria criteria=session.createCriteria(AssignCoScholastic.class);
criteria.setProjection(Projections.property("coscholastic.coScholasticId"));
criteria.setProjection(Projections.property("coscholastic.coscholasticName"));
criteria.add(Restrictions.eq("assignClass.assignID", assiginedClass));
criteria.add(Restrictions.eq("schoolModel.school_id", schoolId));
criteria.add(Restrictions.like("year", year));
listOfAssignCoScholastics=criteria.list();
session.getTransaction().commit();
}catch(Exception ex){
ex.printStackTrace();
}finally{
session.close();
sessionFactory.close();
}
return listOfAssignCoScholastics;
}

我正在尝试执行上面的代码并遇到异常。

什么是org.hibernate.QueryException:无法解析属性:以及如何解决它?

异常(exception):

org.hibernate.QueryException: could not resolve property: coscholastic.coscholasticName of: com.slv.model.AssignCoScholastic
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.toType(AbstractEntityPersister.java:1465)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getType(CriteriaQueryTranslator.java:547)
at org.hibernate.criterion.PropertyProjection.getTypes(PropertyProjection.java:60)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getProjectedTypes(CriteriaQueryTranslator.java:362)
at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:100)
at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:82)
at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:92)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1697)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:347)
at com.slv.daoimpl.RegistrationDaoImpl.listOfAssignCoScholastics(RegistrationDaoImpl.java:1907)
at com.slv.controller.SchoolController.loadAssignCoscholastic(SchoolController.java:920)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

最佳答案

您需要告诉 hibernate 您需要哪个类属性。这就像您在 sql 查询中编写的连接。

试试这个方法

Criteria criteria=session.createCriteria(AssignCoScholastic.class,"assignCoScholastic");
criteria.createAlias("assignCoScholastic.coscholastic", "coscholastic");
criteria.createAlias("assignCoScholastic.assignClass", "assignClass");
criteria.createAlias("assignCoScholastic.schoolModel", "schoolModel");
criteria.setProjection(Projections.property("coscholastic.coScholasticId"));
criteria.setProjection(Projections.property("coscholastic.coscholasticName"));
criteria.add(Restrictions.eq("assignClass.assignID", assiginedClass));
criteria.add(Restrictions.eq("schoolModel.school_id", schoolId));//school_id is the property in class schoolModel
criteria.add(Restrictions.like("assignCoScholastic.year", year));

听说classAliasHavingYear是为具有年份属性的类创建的别名。

你的其余代码...

关于java - 实体在映射时如何获取非主键值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36595749/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com