2

有两个模型类。

public class Register implements Serializable{

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", unique = true, nullable = false)
    private Long id;
    @Column(name = "register_name_en")
    private String registerNameEn;
    @Column(name = "register_name_si")
    private String registerNameSi;
    @Column(name = "register_name_ta")
    private String registerNameTa;
    @Column(name = "tenant_id")
    private String tenantId;
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "category_id")
    private ElgCategory elgCategory;

    //getters and setters
}


public class RegisterNumber implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", unique = true, nullable = false)
    private Long id;
    @Column(name = "register_number_en")
    private String registerNumberEn;
    @Column(name = "register_number_si")
    private String registerNumberSi;
    @Column(name = "register_number_ta")
    private String registerNumberTa;
    @Column(name = "tenant_id")
    private String tenantId;
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "register_id")
    private Register register;

    //getters and setters
}

我想在休眠中编写一个条件查询,以通过给定的 registerNameEn 和 registerNumberEn 加载 RegisterNumber。

查询如下

    @Override
        public RegisterNumber getRegisterNumberByRegisterNumberAndRegisterName(
String registerNumberName, String registerName, String tenantId) {
            Session session = getSession(tenantId);
            try {
                session.beginTransaction();
                List<RegisterNumber> registerNumberList = (List<RegisterNumber>) session
                        .createCriteria(RegisterNumber.class)
                        .setFetchMode("register", FetchMode.JOIN)
                        .add(Restrictions.eq("registerNumberEn", registerNumberName))
                        .add(Restrictions.eq("register.registerNameEn", registerName))
                        .list();
                session.getTransaction().commit();
                if (registerNumberList.size() > 0) {
                    return registerNumberList.get(0);
                } else {
                    return null;
                }
            } finally {
                session.close();
            }
        }

但是当我执行此代码时,我会遇到以下异常。

org.hibernate.QueryException: could not resolve property: register.registerNameEn of: lk.gov.elg.orm.model.RegisterNumber
    org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:83)
    org.hibernate.persister.entity.AbstractPropertyMapping.toColumns(AbstractPropertyMapping.java:98)
    org.hibernate.persister.entity.BasicEntityPropertyMapping.toColumns(BasicEntityPropertyMapping.java:61)
    org.hibernate.persister.entity.AbstractEntityPersister.toColumns(AbstractEntityPersister.java:1912)
    org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumns(CriteriaQueryTranslator.java:523)
    org.hibernate.loader.criteria.CriteriaQueryTranslator.findColumns(CriteriaQueryTranslator.java:538)
    org.hibernate.criterion.SimpleExpression.toSqlString(SimpleExpression.java:66)
    org.hibernate.loader.criteria.CriteriaQueryTranslator.getWhereCondition(CriteriaQueryTranslator.java:419)
    org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:123)
    org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:92)
    org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:93)
    org.hibernate.internal.SessionImpl.list(SessionImpl.java:1603)
    org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:374)
    lk.gov.elg.orm.dao.impl.RegisterNumberDaoImpl.getRegisterNumberByRegisterNumberNameAndRegisterName(RegisterNumberDaoImpl.java:110)
    lk.gov.elg.core.service.impl.RegisterNumberServiceImpl.getRegisterNumberByRegisterNumberNameAndRegisterName(RegisterNumberServiceImpl.java:66)
    lk.gov.elg.web.action.RegisterNumberAction.validate(RegisterNumberAction.java:197)
    com.opensymphony.xwork2.validator.ValidationInterceptor.doBeforeInvocation(ValidationInterceptor.java:238)
    com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:250)
    org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:122)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:179)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:235)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:89)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:130)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:126)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:138)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:165)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:179)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    lk.gov.elg.core.interceptor.AuthenticationInterceptor.handleSingleSignOnRequest(AuthenticationInterceptor.java:163)
    lk.gov.elg.core.interceptor.AuthenticationInterceptor.intercept(AuthenticationInterceptor.java:58)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
    org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:488)
    org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
    org.displaytag.filter.ResponseOverrideFilter.doFilter(ResponseOverrideFilter.java:125)

帮助我克服这个问题。

提前致谢。

4

1 回答 1

2

这个问题隐藏createCriteria(). 该语句setFetchMode()很可能被误用,因为它的作用与您期望的不同。在此处查看更多信息15.5。动态关联获取

我们需要的是基于15.4的加入。关联,由createCriteria()或表示createAlias()。这将允许我们访问Register以进行过滤

... = (List<RegisterNumber>) session
    .createCriteria(RegisterNumber.class)

    // instead of this
    //.setFetchMode("register", FetchMode.JOIN) // it has different meaning

    // use this
    .createAlias("register", "register")

现在“注册”别名应该适用于:

.add(Restrictions.eq("register.registerNameEn", registerName))
于 2013-06-21T18:39:39.633 回答