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

标签 java hibernate orm hibernate-mapping

我有一个计算出的业务对象的属性。计算涉及一些登录用户的详细信息,因此不能表示为简单的 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/

相关文章:

java - 你如何通过代理下载文件

java - Kotlin:只有 getter 的私有(private)可变属性

java - HQL 查询连接表

java - 读取为 JSONObject 时对象属性小写

spring - Hibernate 中的 Java 8 LocalDate 错误地映射到 TIMESTAMP

java - hibernate_sequence 序列名的重复条目

java - Azure SDK 中的哪个 Java API 可以管理 PostgreSQL 服务器?

oracle - 使用 Hibernate 4.1 执行 native 查询

php - PhalconPHP数据库加入ORM

java - 如何自动删除连接表中的行以避免 ConstraintViolationException?