无法在此ManagedType上找到具有给定名称的Attribute

时间:2018-03-06 19:39:38

标签: hibernate jpa hibernate-mapping

Hibernate 5.2.12.Final

我想用CriteriaBuilder构建器替换depreacted session.createCriteria(请参阅org.hibernate.Criteria),我有以下异常

  

无法在此处找到具有给定名称[filtre]的Attribute   ManagedType [com ... DonneeReference]

on:

  

criteria.where(builder.equal(root.get(" filtre"),filtre));

@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 个答案:

答案 0 :(得分:0)

我遇到了相同的错误,该错误通过将组合键定义为实现Serializable并具有正确的equalshashCode的单独的Java类而解决。

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>

参考:skill-guru