阻止 Hibernate 尝试从数据库获取计算字段

发布于 2024-09-15 04:49:27 字数 139 浏览 4 评论 0原文

我有一个已计算的业务对象的属性。该计算涉及一些登录用户的详细信息,因此不能表示为简单的 SQL 查询。

我在表示 Hibernate 映射 XML 文件中的字段时遇到问题,因为 Hibernate 继续尝试从数据库中检索该字段,尽管没有该名称的列。

I have a property of a business object which is calculated. The calculation involves some of the logged in user's details, and so can't be represented as a simple SQL query.

I'm having trouble representing the field in the Hibernate mapping XML file, because Hibernate continues to try and retrieve the field from the database, although there is no column by that name.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瞳孔里扚悲伤 2024-09-22 04:49:27

我在表示 Hibernate 映射 XML 文件中的字段时遇到问题,因为 Hibernate 继续尝试从数据库中检索该字段,尽管没有该名称的列。

我可能遗漏了一些明显的东西但是......那么你为什么要映射这个字段呢?但以防万一,这可能是 属性。


该字段位于 Java 类中,当我编写查询时,我可以提供自定义 SQL 来填充它,例如: SELECT t.*, (SELECT x FROM y) AS field FROM table t WHERE... Hibernate然后允许我使用 SQLQuery.addEntity(Z.class) 并适当地创建对象。据我了解,如果我将该字段完全排除在映射之外,我将永远无法通过查询填充该属性。我已经尝试过:)

我怀疑类似的事情,但我想得到确认。我认为你在这里没有那么多选择:要么使用 (无论是一个“愚蠢”的公式只是为了欺骗休眠)还是 返回非托管实体

I'm having trouble representing the field in the Hibernate mapping XML file, because Hibernate continues to try and retrieve the field from the database, although there is no column by that name.

I'm probably missing something obvious but... why are you mapping this field then? But just in case, this might be a good use case for a <formula> attribute.


The field is in the Java class, and when I code a query I can provide the custom SQL to fill it, e.g: SELECT t.*, (SELECT x FROM y) AS field FROM table t WHERE... Hibernate allows me to then use SQLQuery.addEntity(Z.class) and creates the object appropriately. As far as I understand, if I leave the field out of the mapping altogether, I can never fill that attribute through my query. I have tried :)

I was suspecting something like this but I wanted to get confirmation. I don't think you have that many options here: either use a <formula> (be it a "dumb" formula just to trick hibernate) or return a non managed entity.

养猫人 2024-09-22 04:49:27

听起来你希望这个属性是“暂时的”

<transient name="foo" />

It sounds like you want this property to be "transient"

<transient name="foo" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文