简体   繁体   English

懒惰的一对一使用代理无法正常工作

[英]Lazy One-to-One using Proxy not working

I've got a one-to-one relation between a dealer and a seller which should be lazy using a proxy. 我在经销商和卖家之间有一对一的关系,使用代理应该是懒惰的。 For the side on which the foreign key is defined (the seller, references the dealer), this works fine. 对于定义外键的一方(卖方,参考经销商),这很好。 But it doesn't work from the other side - the seller is always loaded eagerly. 但它从另一方面起作用 - 卖方总是热切地装满。 I set constrained="true" as described in "Some explanations on lazy loading" , but that didn't help. 我设置constrained="true" ,如“关于延迟加载的一些解释”中所述 ,但这没有帮助。

Following is the used mapping: 以下是使用的映射:

<class name="Role" table="PER_PERSROLE" abstract="true">
    <id column="OID" type="long">
        <generator class="native" />
    </id>
    <discriminator column="SUBTYPE" type="string" />
</class>

<subclass name="Dealer" extends="Role" discriminator-value="DEAL">
    <property name="gpNr" column="GP_NR" type="string" />
    <one-to-one name="seller" property-ref="dealer" lazy="proxy" constrained="true"
        outer-join="false" />
</subclass>

<subclass name="Seller" extends="Role" discriminator-value="SELL">
    <many-to-one name="dealer" column="SELLER_DEALEROID" lazy="proxy"
        outer-join="false" />
</subclass>

Is it a problem that both classes reside in one table? 这两个类都存在于一个表中是一个问题吗? I see that strictly speaking the relation isn't constrained on the database (it can't using this model), but the domain model always needs both entities and the application ensures this. 我看到严格来说,关系不受数据库约束(它不能使用此模型),但域模型总是需要两个实体,应用程序确保这一点。

I think that the page you linked to explained it best, though I am not sure why it recommends setting constrained="true" . 我认为您链接的页面解释得最好,但我不确定为什么建议设置constrained="true" If you think about it on the database level, Hibernate can't tell if the given property (the Dealer's seller) should be null or not without hitting the database (it needs to do a SELECT ... WHERE OID=:sellerOrDealerId to see if any rows are returned). 如果你在数据库级别考虑它,Hibernate无法判断给定的属性(经销商的卖家)是否应该为null而不是没有命中数据库(它需要做一个SELECT ... WHERE OID=:sellerOrDealerId看到如果返回任何行)。 And while it's hitting the database, it might as well fetch the rest of the row. 虽然它正在击中数据库,但它也可以获取行的其余部分。 From the other side of the association (Seller's dealer), there are no such problems since it has already fetched the row (and thus the SELLER_DEALEROID column). 从协会的另一方(卖方的经销商),没有这样的问题,因为它已经取出了行(因此SELLER_DEALEROID列)。

I did encounter something similar to this once and was able to solve it by making the association non-optional (err, non-nullable) 我曾经遇到类似于此的东西,并且能够通过使关联非可选(错误,不可空)来解决它

I'm guessing you are loading session.get(id)? 我猜你正在加载session.get(id)? Have you tried load the instance using an HQL query? 您是否尝试使用HQL查询加载实例? That will allow you to specify in the query which relationships to eager or lazy load. 这将允许您在查询中指定哪些关系与急切或延迟加载。

HTH HTH

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM