表“ actels”具有四个字段
id_actels,name_actel,num_actel和region_id_region,这是“ region”表的外键。

我想获取所有具有特定region_id_region值的Actel。像这样:

Select DISTINCT * from Actels where region_id_region = 51 ORDER BY id_actels ASC;


详细:

我得到的错误是这样的:

on in thread "main" java.lang.IllegalArgumentException: org.hibernate.QueryException: could not resolve property: region_id_region of: com.springJPA.domain.Actels [Select DISTINCT e from com.springJPA.domain.Actels e where e.region_id_region = ?1 ORDER BY e.id_actels ASC]
        at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1364)
        at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1300)
        at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:309)
        at com.springJPA.service.ActelsService.listeActelParID_REGION(ActelsService.java:69)
        at com.springJPA.service.ActelsService$$FastClassByCGLIB$$9125bf15.invoke(<generated>)
        at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
        at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:692)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
        at org.springframework.aop.aspectj.AspectJAfterAdvice.invoke(AspectJAfterAdvice.java:42)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
        at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:50)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
        at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
        at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:625)
        at com.springJPA.service.ActelsService$$EnhancerByCGLIB$$7de8d622.listeActelParID_REGION(<generated>)
        at com.springJPA.service.FonctionDAO.afficherLISTE_ACTEL_PAR_REGION(FonctionDAO.java:913)
        at com.springJPA.test.Main.main(Main.java:39)
    Caused by: org.hibernate.QueryException: could not resolve property: region_id_region of: com.springJPA.domain.Actels [Select DISTINCT e from com.springJPA.domain.Actels e where e.region_id_region = ?1 ORDER BY e.id_actels ASC]
        at org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:83)
        at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:77)
        at org.hibernate.persister.entity.AbstractEntityPersister.toType(AbstractEntityPersister.java:1920)
        at org.hibernate.hql.internal.ast.tree.FromElementType.getPropertyType(FromElementType.java:313)
        at org.hibernate.hql.internal.ast.tree.FromElement.getPropertyType(FromElement.java:494)
        at org.hibernate.hql.internal.ast.tree.DotNode.getDataType(DotNode.java:598)
        at org.hibernate.hql.internal.ast.tree.DotNode.prepareLhs(DotNode.java:266)
        at org.hibernate.hql.internal.ast.tree.DotNode.resolve(DotNode.java:213)
        at org.hibernate.hql.internal.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:118)
        at org.hibernate.hql.internal.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:114)
        at org.hibernate.hql.internal.ast.HqlSqlWalker.resolve(HqlSqlWalker.java:881)
        at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.expr(HqlSqlBaseWalker.java:1264)
        at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.exprOrSubquery(HqlSqlBaseWalker.java:4297)
        at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.comparisonExpr(HqlSqlBaseWalker.java:3769)
        at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:1947)
        at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.whereClause(HqlSqlBaseWalker.java:794)
        at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:595)
        at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:299)
        at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:247)
        at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:248)
        at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:183)
        at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
        at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:105)
        at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
        at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:168)
        at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:219)
        at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:197)
        at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1732)
        at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:301)
        ... 15 more


ActelService.java:

package com.springJPA.service;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.springJPA.domain.Actels;
import com.springJPA.util.MyEntityManagerFactory;

@Component("actelService")
public class ActelsService {
    @Autowired @Qualifier("myEMF")
    private MyEntityManagerFactory emf;
    private EntityManager entityManager;

    /**
     * ajoutActel
     * @param actel
     */
    public void ajoutActel(Actels actel) {
        entityManager = emf.getEntityManager();
        entityManager.persist(actel);
    }

    /**
     * modifActel
     * @param actel
     */
    public void modifActel(Actels actel) {
        entityManager = emf.getEntityManager();
        entityManager.merge(actel);

    }

    /**
     * supprimerActel
     * @param actel
     */
    public void supprimerActel(Actels actel) {
        entityManager = emf.getEntityManager();
        entityManager.remove(actel);

    }

    /**
     * listeActel
     * @return listeActel
     */
    public List<Actels> listeActel() {
        entityManager = emf.getEntityManager();
        TypedQuery<Actels> query = entityManager.createQuery("Select DISTINCT e from Actels e ORDER BY e.id_actels ASC", Actels.class);
        List<Actels> results = query.getResultList();
        return results;

    }

    /**
     * listeActel
     * @param id_region
     * @return
     */
    public List<Actels> listeActelParID_REGION(int id_region) {
        entityManager = emf.getEntityManager();
        TypedQuery<Actels> query = entityManager.createQuery("Select DISTINCT e from Actels e where e.region_id_region = ?1 ORDER BY e.id_actels ASC", Actels.class);
        query.setParameter("1", id_region);
        List<Actels> results = query.getResultList();
        return results;

    }

    public EntityManager getEntityManager() {
        return entityManager;
    }

    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    public MyEntityManagerFactory getEmf() {
        return emf;
    }

    public void setEmf(MyEntityManagerFactory emf) {
        this.emf = emf;
    }
}


Actels.java:

package com.springJPA.domain;

import java.io.Serializable;
import java.util.Collection;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;

/**
 * Entity implementation class for Entity: Actels
 *
 */
@Entity

public class Actels implements Serializable {

    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "id_Sequence_actels")
    @SequenceGenerator(name = "id_Sequence_actels", sequenceName = "ID_SEQ_ACTELS")
    private int id_actels;
    private String nom_actel;
    private int num_actel;

    private static final long serialVersionUID = 1L;
    @OneToMany
    @JoinColumn(name = "Actels_id_actels", referencedColumnName = "id_actels")
    private Collection<Ville> ville;
    public Actels() {
        super();
    }

    public int getId_actels() {
        return id_actels;
    }
    public void setId_actels(int id_actels) {
        this.id_actels = id_actels;
    }

    public String getNomActels() {
        return this.nom_actel;
    }

    public void setNomActels(String nomActels) {
        this.nom_actel = nomActels;
    }

    public int getNum_actel() {
        return num_actel;
    }

    public void setNum_actel(int num_actel) {
        this.num_actel = num_actel;
    }

    public Collection<Ville> getVille() {
        return ville;
    }

    public void setVille(Collection<Ville> param) {
        this.ville = param;
    }


}


Region.java:

package com.springJPA.domain;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import com.springJPA.domain.Actels;
import java.util.Collection;
import javax.persistence.OneToMany;
import javax.persistence.JoinColumn;

/**
 * Entity implementation class for Entity: Reseau
 *
 */
@Entity

public class Region implements Serializable {


    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "id_Sequence_region")
    @SequenceGenerator(name = "id_Sequence_region", sequenceName = "ID_SEQ_REGION")
    private int id_region;
    private String nom_region;
    private int num_region;

    private static final long serialVersionUID = 1L;
    @OneToMany
    @JoinColumn(name = "Region_id_region", referencedColumnName = "id_region")
    private Collection<Actels> actels;
    public Region() {
        super();
    }


    public int getId_region() {
        return id_region;
    }
    public void setId_region(int id_region) {
        this.id_region = id_region;
    }
    public String getNom_region() {
        return this.nom_region;
    }

    public void setNom_region(String region) {
        this.nom_region = region;
    }

    public int getNum_region() {
        return num_region;
    }

    public void setNum_region(int num_region) {
        this.num_region = num_region;
    }


    public Collection<Actels> getActels() {
        return actels;
    }


    public void setActels(Collection<Actels> param) {
        this.actels = param;
    }


}

最佳答案

您的实体中没有region_id_region属性(Actels类)。您应该具有以下内容:

@Entity
public class Actels {
 ...
private int region_id_region;
 ...
 }

09-13 01:04