Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] Temporal annotation acts differently than mapping?


I added this persistence property and had no errors reported but I'm still
not getting time populated
in the object returned by EclipseLink.

Since there are no annotations at all on the test class, I believe only the
mapping from orm.xml 
could be used by EclipseLink

Here's the orm.xml:

<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm";
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm
http://java.sun.com/xml/ns/persistence/orm_1_0.xsd"; version="1.0">
	<entity class="domain.JpaMappingTest">
		<table name="TEST">
		</table>
		<attributes>
			<id name="id">
			</id>
			<basic name="description">
			</basic>
			<basic name="dateVal">
				<temporal>DATE</temporal>
			</basic>
			<basic name="timestampVal">
				<temporal>TIMESTAMP</temporal>
			</basic>
			<basic name="dateVal2">
				<column insertable="false" updatable="false" name="DATEVAL"/>
				<temporal>TIMESTAMP</temporal>
			</basic>
		</attributes>
	</entity>
</entity-mappings>

Here's the table:

CREATE TABLE TEST (
		ID NUMBER(22 , 0) NOT NULL,
		DESCRIPTION VARCHAR2(100) NOT NULL,
		DATEVAL DATE NOT NULL,
		TIMESTAMPVAL TIMESTAMP(11) NOT NULL
	);

Here's the class:

public class JpaMappingTest
{
    private Integer id;

    private String description;

    private Date dateVal;

    private Date dateVal2;

    private Date timestampVal;

    public Integer getId()
    {
        return id;
    }

    public void setId(Integer id)
    {
        this.id = id;
    }

}

(remaining get/set not included)

and here's persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence";
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd";>
	<persistence-unit name="EclipseLinkSpike"
transaction-type="RESOURCE_LOCAL">
		<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
		<class>domain.Status</class>
		<class>domain.Test2</class>
		<class>domain.JpaAnnotationsTest</class>
		<class>domain.JpaMappingTest</class>
		<properties>
			<property name="eclipselink.jdbc.url"
value="jdbc:oracle:thin:@localhost:1521:xe"/>
			<property name="eclipselink.jdbc.driver"
value="oracle.jdbc.OracleDriver"/>
			<property name="eclipselink.logging.level" value="FINEST"/>
			<property name="eclipselink.target-database" value="Oracle" />
			<property name="eclipselink.jdbc.read-connections.min" value="1"/>
			<property name="eclipselink.jdbc.write-connections.min" value="1"/>
			<property name="eclipselink.orm.validate.schema" value="true"/>
		</properties>
	</persistence-unit>
</persistence>

Here's the code to get the object:

    public JpaMappingTest getJpaMappingTest(Integer id)
    {
        JpaMappingTest test = null;
        EntityManager mgr = getEntityManager();
        test = mgr.find(JpaMappingTest.class, id);
        return test;
    }

Here's my test to check for expected behavior:

    @Test
    public void testJPAMapping() throws Exception
    {
        DAO dao = new DAO();
        JpaMappingTest obj = dao.getJpaMappingTest(1);
        assertNotNull("JpaMappingTest object should be found", obj);
        assertEquals("ID", 1, obj.getId());
        System.out.println("JpaAnnotationsTest object attributes: " +
BeanUtils.describe(obj));
        assertFalse("Oracle DATE col mapped as Temporal DATE should have no
time", DateUtil.hasTime(obj.getDateVal()));
        // temporal mapping seems to act differently than temporal
annotation!
        assertTrue("Oracle DATE col mapped as Temporal TIMESTAMP should have
time", DateUtil.hasTime(obj.getDateVal2()));
        assertTrue("Oracle TIMESTAMP col mapped as Temporal TIMESTAMP should
have time", DateUtil.hasTime(obj.getTimestampVal()));
    }

Here's the test failure:

java.lang.AssertionError: Oracle DATE col mapped as Temporal TIMESTAMP
should have time
	at org.junit.Assert.fail(Assert.java:71)
	at org.junit.Assert.assertTrue(Assert.java:34)
	at dataaccess.DateValueTest.testJPAMapping(DateValueTest.java:46)

Here's a few relevant lines from the logging:

The access type for the persistent class [class domain.JpaAnnotationsTest]
is set to [FIELD].
The alias name for the entity class [class domain.JpaAnnotationsTest] is
being defaulted to: JpaAnnotationsTest.
The column name for element [private java.lang.Integer
domain.JpaAnnotationsTest.id] is being defaulted to: ID.
The column name for element [private java.util.Date
domain.JpaAnnotationsTest.dateVal] is being defaulted to: DATEVAL.
The column name for element [private java.lang.String
domain.JpaAnnotationsTest.description] is being defaulted to: DESCRIPTION.
The column name for element [private java.util.Date
domain.JpaAnnotationsTest.timestampVal] is being defaulted to: TIMESTAMPVAL.

Here's the state of the object returned from EclipseLink:

JpaAnnotationsTest object attributes: {id=1, 
dateVal=Thu Apr 09 00:00:00 PDT 2009, 
description=Test One, 
dateVal2=Thu Apr 09 00:00:00 PDT 2009, 
timestampVal=Thu Apr 09 12:47:50 PDT 2009, 
class=class domain.JpaMappingTest}

Since dateVal2 is mapped with the TIMESTAMP temporal element, I'd expect the
time element to be populated so I'm wondering why it isn't.

A different class mapped to the same table via JPA annotations like this:
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "dateVal", insertable = false, updatable = false)
    private Date dateVal2;

comes back with the time element populated expected like this:

  dateVal2=Thu Apr 09 12:47:50 PDT 2009



Thanks for your help!
-- 
View this message in context: http://www.nabble.com/Temporal-annotation-acts-differently-than-mapping--tp23489987p23531911.html
Sent from the EclipseLink - Users mailing list archive at Nabble.com.



Back to the top