无法在此 ManagedType 上找到具有给定名称的属性
Posted
技术标签:
【中文标题】无法在此 ManagedType 上找到具有给定名称的属性【英文标题】:Unable to locate Attribute with the the given name on this ManagedType 【发布时间】:2018-03-06 19:39:37 【问题描述】:休眠 5.2.12.Final
我想用 CriteriaBuilder 构建器替换已弃用的 session.createCriteria(请参阅 org.hibernate.Criteria),我有以下异常
无法在此找到具有给定名称 [过滤器] 的属性 ManagedType [com...DonneeReference]
开:
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】:我遇到了同样的错误,通过将复合键定义为实现Serializable
并具有正确的equals
和hashCode
的单独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
【讨论】:
以上是关于无法在此 ManagedType 上找到具有给定名称的属性的主要内容,如果未能解决你的问题,请参考以下文章
错误显示“无法获取样式表 (#) 的文本:未找到具有给定 id 的样式表”,这是啥意思?
核心数据:“无法在此模型中找到名为 ... 的实体”(仅在设备上)