将 java.long 映射到 oracle.Number(14)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7638664/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mapping java.long to oracle.Number(14)
提问by x.509
I have db column whose datatype is Number (15) and i have the corresponding field in java classes as long. The question is how would i map it using java.sql.Types.
我有数据类型为 Number (15) 的 db 列,并且我在 java 类中有相应的字段long。问题是我将如何使用java.sql.Types映射它。
- would Types.BIGINT work?
- Or shall i use something else?
- Types.BIGINT 会起作用吗?
- 还是我应该用别的东西?
P.S: I can't afford to change the datatype within java class and within DB.
PS:我负担不起在 java 类和 DB 中更改数据类型。
回答by Mike
回答by robododge
A good place to find reliable size mappings between Java and Oracle Types is in the Hibernate ORM tool. Documented in the code here, Hibernate uses an Oracle NUMBER(19,0) to represent a java.sql.Types.BIGINT which should map to a long primitave
在 Hibernate ORM 工具中找到 Java 和 Oracle 类型之间可靠的大小映射的好地方。记载在这里的代码,休眠使用Oracle NUMBER(19,0)来表示java.sql.Types.BIGINT应映射到一个长primitave
回答by FGreg
I had a similar problem where I couldn't modify the Java Type or the Database Type. In my situation I needed to execute a native SQL query (to be able to utilize Oracle's Recursive query abilities) and map the result set to a non-managed entity (essentially a simple pojo class).
我有一个类似的问题,我无法修改 Java 类型或数据库类型。在我的情况下,我需要执行本机 SQL 查询(以便能够利用 Oracle 的递归查询功能)并将结果集映射到非托管实体(本质上是一个简单的 pojo 类)。
I found a combination of addScalarand setResultTransformerworked wonders.
我发现addScalar和setResultTransformer的组合创造了奇迹。
hibernateSes.createSQLQuery("SELECT \n"
+ " c.notify_state_id as \"notifyStateId\", \n"
+ " c.parent_id as \"parentId\",\n"
+ " c.source_table as \"sourceTbl\", \n"
+ " c.source_id as \"sourceId\", \n"
+ " c.msg_type as \"msgType\", \n"
+ " c.last_updt_dtm as \"lastUpdatedDateAndTime\"\n"
+ " FROM my_state c\n"
+ "LEFT JOIN my_state p ON p.notify_state_id = c.parent_id\n"
+ "START WITH c.notify_state_id = :stateId\n"
+ "CONNECT BY PRIOR c.notify_state_id = c.parent_id")
.addScalar("notifyStateId", Hibernate.LONG)
.addScalar("parentId", Hibernate.LONG)
.addScalar("sourceTbl",Hibernate.STRING)
.addScalar("sourceId",Hibernate.STRING)
.addScalar("msgType",Hibernate.STRING)
.addScalar("lastUpdatedDateAndTime", Hibernate.DATE)
.setParameter("stateId", notifyStateId)
.setResultTransformer(Transformers.aliasToBean(MyState.class))
.list();
Where notifyStateId
, parentId
, sourceTbl
, sourceId
, msgType
, and lastUpdatedDateAndTime
are all properties of MyState
.
其中notifyStateId
、parentId
、sourceTbl
、sourceId
、msgType
和lastUpdatedDateAndTime
都是 的属性MyState
。
Without the addScalar
's, I would get a java.lang.IllegalArgumentException: argument type mismatch
because Hibernate was turning Oracle's Number
type into a BigDecimal
but notifyStateId
and parentId
are Long
types on MyState
.
如果没有addScalar
's,我会得到 a,java.lang.IllegalArgumentException: argument type mismatch
因为 Hibernate 正在将 Oracle 的Number
类型转换为 a BigDecimal
butnotifyStateId
并且parentId
是Long
类型 on MyState
。
回答by Tarcísio Júnior
I always use wrapper type, because wrapper types can be express null values.
我总是使用包装器类型,因为包装器类型可以表示空值。
In this case I will use Long wrapper type.
在这种情况下,我将使用 Long 包装器类型。