我有一个计算出的业务对象的属性。计算涉及一些登录用户的详细信息,因此不能表示为简单的 SQL 查询。
我在表示 Hibernate 映射 XML 文件中的字段时遇到问题,因为 Hibernate 继续尝试从数据库中检索字段,尽管没有该名称的列。
最佳答案
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.
我可能遗漏了一些明显的东西,但是......那你为什么要映射这个字段?但以防万一,这可能是 <formula>
的一个很好的用例。属性。
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 :)
我怀疑是这样的,但我想得到确认。我认为您在这里没有那么多选择:要么使用 <formula>
(无论是一个“愚蠢”的公式只是为了欺骗 hibernate )还是return a non managed entity .
关于java - 阻止 Hibernate 尝试从数据库中获取计算字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3509031/