当前位置: 首页 > 知识库问答 >
问题:

在此ManagedType上找不到具有给定名称的属性

柳涵意
2023-03-14

冬眠5.2.12.1决赛

我想用CriteriaBuilder替换depreracted session.createCriteria(参见org.hibernate.Criteria),我有以下异常

在此ManagedType[com…DonneReference]上找不到具有给定名称[filtre]的属性

上:

其中(builder.equal(root.get(“filter”),filter));

@Repository
    public class ReferenceDaoImpl implements ReferenceDao {

        @Autowired
        private SessionFactory sessionFactory;

        ...
        private static final String REQUEST_ALL_VERSIONS = "select  tableref  from " + DonneeReference.class.getName() + " tableref "
            + "where tableref.table = ? and tableref.dateAnnulation = null";

        private static final String REQUETE_RECHERCHE_LOCALITE = "select loc from " + Localite.class.getName() + " loc "
            + "where loc.codeInsee = ? AND loc.codePostal = ? ";

        private static final String REQUETE_RECHERCHE_LOCALITES_COMMUNE = "select loc from " + Localite.class.getName() + " loc where loc.codeInsee = ?";

        ...
        // Constructeurs
        public ReferenceDaoImpl() {
        }


        @Override
        public DonneeReference rechercherDonneeUnique(String identab, String critere, String filtre, Date dateEffet) {
            DonneeReference donneeReference = null;
            Session session = sessionFactory.getCurrentSession();

            // Sample try
            CriteriaBuilder builder = sessionFactory.getCriteriaBuilder();
            CriteriaQuery<DonneeReference> criteria = builder.createQuery(DonneeReference.class);
            Root<DonneeReference> root = criteria.from(DonneeReference.class);
            criteria.select(root);
            criteria.where(builder.equal(root.get("filtre"), filtre));
            List<DonneeReference> results = session.createQuery(criteria).getResultList();
            return results.get(0);
        }
    }

CriteriaBuilder似乎只适用于实体,而不适用于旧的hbm映射文件

    <class name="ReferenceLog" table="REF_REFPARAM_LOG">
        <id name="id" column="id">
            <generator class="increment"/>
        </id>
        <property name="tableMaj" not-null="true"/>
        <property name="code" not-null="true"/>
        <property name="userMaj" not-null="true"/>
        <property name="message" not-null="true"/>
        <property name="dateMaj" not-null="true"/>
    </class>

    <class name="DonneeReference" table="REF_REFPARAM">
        <composite-id>
            <key-property name="table" column="CT0_IDENTAB"/>
            <key-property name="code" column="CT0_CRIT1"/>
            <key-property name="filtre" column="CT0_CRIT2"/>
            <key-property name="version" column="CT0_NOVERS"/>
        </composite-id>

        <property name="zone3" column="CT0_ZONX3"/>
        <property name="libelle" column="CT0_ZONX2"/>
        <property name="libelleCourt" column="CT0_ZONX1"/>
        <property name="nombre1" column="CT0_NB1"/>
        <property name="nombre2" column="CT0_NB2"/>
        <property name="nombre3" column="CT0_NB3"/>
        <property name="ordreAffichage" column="REF_ORDRE"/>
        <property name="dateEffet" column="REF_DTEFFET"/>
        <property name="dateMiseAJour" column="REF_DTMAJ"/>
        <property name="dateCreation" column="REF_DTCREAT"/>
        <property name="dateAnnulation" column="REF_DTANNUL"/>
        <property name="redacteur" column="CT0_REDACT"/>
        <property name="action" column="REF_ACTION"/>
    </class>

    <query name="get_reference_data">
        <![CDATA[
            select referenceData 
            from DonneeReference referenceData
            where
                referenceData.table = :table
                and referenceData.version = (
                    select min(tab.version)
                    from DonneeReference tab
                    where
                        tab.table = referenceData.table
                        and tab.code = referenceData.code
                        and tab.filtre = referenceData.filtre
                        and referenceData.dateEffet <= :date
                    )
                and referenceData.dateAnnulation is null
            order by referenceData.ordreAffichage ASC, referenceData.filtre ASC, referenceData.code ASC
        ]]>
    </query>

</hibernate-mapping>

共有1个答案

许焕
2023-03-14

我遇到了同样的错误,通过将复合键定义为一个独立的java类来解决这个问题,该类实现了Serializable,并且具有正确的等于

public class DonneeReference {
    private DonneeReferenceId donneeReferenceId;  // <-- composite key goes here
    private String zone3;
    private String libelle;
    <other fields here>

    <getter & setter for all the fields>
}

public DonneeReferenceId implements Serializable {
    private String table;
    private String code;
    private String filtre;
    private String version;

    <getter & setter for all the fields>

    <equals & hashCode involving all fields here>
}

并且hbm文件应该相应地

<class name="DonneeReference" table="REF_REFPARAM">
    <composite-id name="donneeReferenceId" class="DonneeReferenceId">
        <key-property name="table" column="CT0_IDENTAB"/>
        <key-property name="code" column="CT0_CRIT1"/>
        <key-property name="filtre" column="CT0_CRIT2"/>
        <key-property name="version" column="CT0_NOVERS"/>
    </composite-id>

    <property name="zone3" column="CT0_ZONX3"/>
    <property name="libelle" column="CT0_ZONX2"/>
    ...
</class>

参考:技能大师

 类似资料: