OpenJPA should be able to fetch the value for the auto-assigned field after
commit like any other field.

The following simple test demonstrates when an auto-assigned value is set on
the persistent entity when
a corresponding field is declared as

        @Column(name="ts", columnDefinition="TIMESTAMP DEFAULT 
CURRENT_TIMESTAMP")
        private Timestamp ts;

        /**
         * When does the value of an auto-assigned field gets set on the entity?
         */
        public void testAutoAssignedFieldValue() {
                EntityManager em = emf.createEntityManager();
                em.getTransaction().begin();
                AutoInc pc = new AutoInc();
                
                assertNull(pc.getTimestamp());
                em.persist(pc);
                assertNull(pc.getTimestamp());
                
                em.flush();
                assertNull(pc.getTimestamp());
                
                em.getTransaction().commit();
                assertNull(pc.getTimestamp());
                em.clear();
                assertNull(pc.getTimestamp());
                
                em.getTransaction().begin();
                pc = em.find(AutoInc.class, pc.getId());
                assertNotNull(pc);
                assertNotNull(pc.getTimestamp());
        }


Marcel Ruff wrote:
> 
> Hi,
> 
> i was to quickly happy:
> 
> The timestamp is add nicely to the DB: OK
> (check with normal sql)
> 
> But when i want to load it later into my bean
> it remains null in java:
>         CompanyProperty cp = em.find(CompanyProperty.class, key);
>         log.info(cp.getCreationTs());
> --> is null!
> 
> Why this?
> 
> Thanks
> Marcel
> 
> Marcel Ruff wrote:
>> Pinaki Poddar wrote:
>>> Hi,
>>>    With MySQL this is what happens:
>>>
>>>
>>> 1. Definition of a column with default timestamp value
>>>
>>>    @Column(name="ts", columnDefinition="TIMESTAMP DEFAULT
>>> CURRENT_TIMESTAMP")
>>>    private Timestamp ts;
>>>
>>> 2. OpenJPA issues SQL to create the table:
>>>
>>>     CREATE TABLE AutoInc (id BIGINT NOT NULL, ts TIMESTAMP DEFAULT
>>> CURRENT_TIMESTAMP, PRIMARY KEY (id))
>>>
>>> 3. OpenJPA issues SQL for insert when application does not set the 
>>> value of
>>> ts field:
>>>           INSERT INTO AutoInc (id, ts) VALUES (?, ?) [params=(long) 
>>> 100, (null)
>>> null]
>>>
>>>
>>> 4. This is what database looks like:
>>>
>>> mysql> select * from autoinc;
>>> +-----+---------------------+
>>> | id  | ts                  |
>>> +-----+---------------------+
>>> | 100 | 2008-08-06 12:47:29 |
>>> +-----+---------------------+
>>>   
>> Hi all,
>>
>> thanks for all support & solution.
>>
>> For postgres 8.3.x this works well:
>>
>>    @Column(name = "creationts", insertable=false, updatable=false,
>> columnDefinition="timestamp not null default current_timestamp")
>>
>>
>> regards
>> Marcel
>>
>>
> 
> 
> -- 
> Marcel Ruff
> http://www.xmlBlaster.org
> http://watchee.net
> Phone: +49 7551 309371
> 
> 
> 

-- 
View this message in context: 
http://n2.nabble.com/How-to-apply-creation-timestamp-with-tp673210p678768.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Reply via email to