开发者

Mapping java.long to oracle.Number(14)

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.

  • would Types.BIGINT work?
  • Or shall i use something else?

P.S: I can开发者_开发知识库't afford to change the datatype within java class and within DB.


From this link it says that java.sql.Types.BIGINT should be used for long in Java to Number in SQL (Oracle).

Attaching screenshot of the table in case the link ever dies.

Mapping java.long to oracle.Number(14)


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


I always use wrapper type, because wrapper types can be express null values.

In this case I will use Long wrapper type.


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).

I found a combination of addScalar and setResultTransformer worked wonders.

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.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜