gpt4 book ai didi

java - 当一个类映射到两个不同的表时,在 Hibernate 3.3.2 中创建 sql 查询

转载 作者:太空宇宙 更新时间:2023-11-04 12:32:20 26 4
gpt4 key购买 nike

我有一个实体类(协议(protocol)),它映射到两个表(例如协议(protocol)和协议(protocol)历史记录)(它们通过 hbm 映射文件中节点中的属性“实体名称”和“表”进行区分)。当我调用下面的代码时,得到“org.hibernate.MappingException:未知实体:.a.b.c.Agreement

public List findActiveSiteAgreement(final String siteId, final String customerId, final Date currentDate) {     

return (List) getHibernateTemplate().execute(new HibernateCallback() {

public Object doInHibernate(Session session) throws HibernateException, SQLException {

SQLQuery query = session.createSQLQuery(
"SELECT sa.* " +
"FROM SiteAgreement sa, AgreementHeader ah " +
"WHERE sa.siteAgreement_id = ah.siteAgreement_id " +
"AND sa.mainBaseStationSite_id=:siteId " +
"AND ah.partner_id=:customerId " +
"AND NVL(ah.expiryDate, SYSDATE+1)>SYSDATE AND NVL(ah.maintainedManually,0) = 0"
);

//query.addEntity(SiteAgreement.class);
query.addEntity("SiteAgreement");
query.setParameter("siteId", new String(siteId));
query.setParameter("customerId", new String(customerId));

List result = query.list();

return result;
}

当我调用 saveOrUpdate 方法时,我可以指定要使用哪个实体名称,但在这种情况下如何处理 hibernate 中的选择查询?

这是 SiteAgreement.hbm.xml

    <hibernate-mapping package="lt.webmedia.unibase.conan.domain">
<class name="SiteAgreement" entity-name="SiteAgreement" table="SiteAgreement">
<id name="id" column="siteagreement_id">
<generator class="lt.webmedia.unibase.conan.domain.LegacyIdGenerator">
<param name="prefix">cn</param>
</generator>
</id>

<property name="currency_tid" length="35"/>
<property name="freeText" length="2000"/>
<property name="partnerToBeInvoiced" length="35"/>
<property name="replacedAgreement_id" length="35"/>
<property name="mainBaseStationSite_id" length="35"/>
<property name="transactiontag" precision="38" scale="0"/>
<property name="agreementNumberOld" length="50"/>
<property name="paymentPeriod"/>
<property name="paymentRuleText" length="250"/>
<property name="offer_id" length="35"/>
<property name="manuallyPriced" precision="1" scale="0"/>
<property name="changed"/>
<property name="changedBy" length="35"/>
<property name="created"/>
<property name="createdBy" length="35"/>
<property name="inSharedUse" precision="1" scale="0"/>
<property name="paymentType" precision="1" scale="0"/>
<property name="ubResponsiblePerson_id" length="35"/>
<property name="partnerToBeInvoicedAddress" length="35"/>
<property name="purchaseOrderNumber" length="35"/>
<property name="invoiceType_tid" length="35"/>
<property name="invoicedUntil"/>
<property name="previousInvoicedUntil"/>
<property name="firstInvoicingDate"/>
<property name="invoicingSuspended" precision="1" scale="0"/>
<property name="pricingType_tid" length="35"/>
<property name="siteLocation_tid" length="35"/>
<property name="propertyRentPercent" precision="5" scale="2"/>
<property name="retrospective_running_date" />

<one-to-one name="agreementHeader"
class="lt.webmedia.unibase.conan.domain.AgreementHeader"
entity-name="AgreementHeader"
property-ref="siteAgreement"
fetch="join"
cascade="all"/>

<!-- added "delete-orphan" > -->
<set name="agreementLines" inverse="true" fetch="join" cascade="all,delete-orphan" >
<key column="siteAgreement_id"/>
<one-to-many class="lt.webmedia.unibase.conan.domain.AgreementLine" entity-name="AgreementLine" />
</set>

</class>
</hibernate-mapping>

这是 SiteAgreementHistory.hmb.xml

    <hibernate-mapping package="lt.webmedia.unibase.conan.domain">
<class name="SiteAgreement" entity-name="SiteAgreementHistory" table="SiteAgreementHistory">

<id name="id" column="siteagreement_id">
<!-- <generator class="lt.webmedia.unibase.conan.domain.LegacyIdGenerator">
<param name="prefix">cn</param>
</generator> -->
</id>

<property name="currency_tid" length="35"/>
<property name="freeText" length="2000"/>
<property name="partnerToBeInvoiced" length="35"/>
<property name="replacedAgreement_id" length="35"/>
<property name="mainBaseStationSite_id" length="35"/>
<property name="transactiontag" precision="38" scale="0"/>
<property name="agreementNumberOld" length="50"/>
<property name="paymentPeriod"/>
<property name="paymentRuleText" length="250"/>
<property name="offer_id" length="35"/>
<property name="manuallyPriced" precision="1" scale="0"/>
<property name="changed"/>
<property name="changedBy" length="35"/>
<property name="created"/>
<property name="createdBy" length="35"/>
<property name="inSharedUse" precision="1" scale="0"/>
<property name="paymentType" precision="1" scale="0"/>
<property name="ubResponsiblePerson_id" length="35"/>
<property name="partnerToBeInvoicedAddress" length="35"/>
<property name="purchaseOrderNumber" length="35"/>
<property name="invoiceType_tid" length="35"/>
<property name="invoicedUntil"/>
<property name="previousInvoicedUntil"/>
<property name="firstInvoicingDate"/>
<property name="invoicingSuspended" precision="1" scale="0"/>
<property name="pricingType_tid" length="35"/>
<property name="siteLocation_tid" length="35"/>
<property name="propertyRentPercent" precision="5" scale="2"/>
<property name="retrospective_running_date" />

<one-to-one name="agreementHeader"
class="lt.webmedia.unibase.conan.domain.AgreementHeader"
entity-name="AgreementHeaderHistory"
property-ref="siteAgreement"
cascade="all"
fetch="join" />

<set name="agreementLines" inverse="true" fetch="join" cascade="all">
<key column="siteAgreement_id"/>
<one-to-many class="lt.webmedia.unibase.conan.domain.AgreementLine"
entity-name="AgreementLineHistory" />
</set>

</class>
</hibernate-mapping>

最佳答案

我建议你有这样的东西:

<class name="package.Agreement" entity-name="entity1" table="agreements">
....
</class>


<class name="package.Agreement" entity-name="entity2" table="agreement_history">
....
</class>

请尝试下一步:

SQLQuery query = session.createSQLQuery(someSqlQuery);
query.addEntity("entity1"); //value of attribute 'entity-name' (case-sensitive)
query.setParameter("id", new String(id));
List result = query.list();

关于java - 当一个类映射到两个不同的表时,在 Hibernate 3.3.2 中创建 sql 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37726007/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com