Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

database - How to represent Oracle Interval in Java

I'm using Java 7 with hibernate 4.

Want to use oracle Interval data type (http://psoug.org/definition/INTERVAL.htm) to represent a interval of a certain number of days.

Wondering what Java Type to use to map this Oracle Interval Object.

I would like to use standard Java objects and not any oracle.sql.* objects as mentioned in this document http://docs.oracle.com/cd/B28359_01/java.111/b31224/datacc.htm.

Here's the table I'm playing with:


CREATE TABLE "MyTest" (
    "ID" NUMBER(14,0) NOT NULL
    "DELIVERY_PERIOD" INTERVAL DAY (3) TO SECOND (6), 
    CONSTRAINT "MYTEST_PK" PRIMARY KEY ("ID"));

Edit

I've since tried with

@Temporal(TemporalType.TIME)
private java.util.Date deliveryPeriod;

Getting the error:

Caused by: java.sql.SQLException: Invalid column type: getTime not implemented for class oracle.jdbc.driver.T4CIntervaldsAccessor

Edit 2

http://docs.oracle.com/cd/B12037_01/java.101/b10983/datamap.htm

I know mapping it to Java String would work, but I would like to get it is some sort of date object so I don't have to parse it myself.

http://objectmix.com/jdbc-java/41781-oracle10g-oracle-sql-interval-type.html

I would also like to avoid using oracle specific data types such as oracle.sql.INTERVALS

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Check this SO answer:

  1. You can define an Interval Hibernate UserType
  2. Then your Entities will simply use Integer:

    @TypeDef(name="interval", typeClass = Interval.class)
    
    @Type(type = "interval")    
    private Integer interval;
    
  3. The Internal UserType is the Java Integer to SQL INTERVAL adapter.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...