Question

Here is the background: I have an annotated @Embeddable Java class that has a GregorianCalendar field. I am attempting to use hibernate3:hbm2ddl to generate a schema via the hibernate3 Maven plugin so that I can persist another object into which this is embedded, but it runs into an error regarding the use of @Temporal.

Here is the embeddable class:

@Embeddable
public class OperationalStatus implements Serializable {
        .
        .
        .
    /**
     * Recorded date/time that the status value is valid
     */
    private GregorianCalendar time;

    /**
     * No-argument constructor.
     */
    public OperationalStatus() {}
        .
        .
        .
    /**
     * @return the time
     */
    @Temporal(TemporalType.TIMESTAMP) 
    public GregorianCalendar getTime() {
        return time;
    }

    /**
     * @param time the time to set
     */
    public void setTime(GregorianCalendar time) {
        this.time = time;
    }
}

And here is the error readout:

[ERROR] Failed to execute goal org.codehaus.mojo:hibernate3-maven-plugin:2.2:hbm 2ddl (default-cli) on project STRIPES_V2: Execution default-cli of goal org.code haus.mojo:hibernate3-maven-plugin:2.2:hbm2ddl failed: @Temporal should only be s et on a java.util.Date or java.util.Calendar property: stripes.datamodel. util.OperationalStatus.time

Here are some excerpts from the pom:

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>hibernate3-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
    <components>
        <component>
            <name>hbm2ddl</name>
            <implementation>annotationconfiguration</implementation>
        </component>
    </components>
    <componentProperties>
        <drop>false</drop>
        <configurationfile>src/main/resources/hibernate.cfg.xml</configurationfile>
        <outputfilename>schema.sql</outputfilename>
    </componentProperties>
</configuration>
<dependencies>
    <dependency>
        <groupId>postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.2-1000.jdbc4</version>
    </dependency>
</dependencies>
</plugin>
.
.
.
<dependency>
    <groupId>org.hibernate</groupId>
    <version>4.1.7.Final</version>
    <artifactId>hibernate-core</artifactId>
</dependency>

What am I missing? GregorianCalendar is a concrete extension of Calendar, so what's wrong?

Was it helpful?

Solution

Nothing in the spec constrains the provider to be capable of vending up any particular arbitrary implementation of Calendar you want, so it would be a compatibility issue if they allowed you to require it to give a particular one. (Not that anyone in their right mind would make a new subclass of Calendar, but the possibility is there. A JPA implementation that only knew how to return its own custom subclass of Calendar wouldn't really be wrong by anything in the spec, you can't require it to know how to use GregorianCalendar instead.)

Or on the flip side, if I were to create org.affe.MyAwesomeCalendar, it is not reasonable to expect a JPA provider to be able to make instances of it for me!

11.1.47 Temporal Annotation

The Temporal annotation must be specified for persistent fields or properties of type java.util.Date and java.util.Calendar. It may only be specified for fields or properties of these types.

Hibernate will not let you choose java.sql.Date etc directly either. Basically they interpret it's being left up to the persistence provider to determine what implementation of Calendar is used.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top