You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Marcel Ruff <mr...@marcelruff.info> on 2008/08/07 11:23:52 UTC

Re: How to apply creation timestamp with -> UNRESOLVED

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


Re: How to apply creation timestamp with -> UNRESOLVED

Posted by Pinaki Poddar <pp...@apache.org>.
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.


Re: How to apply creation timestamp with -> UNRESOLVED

Posted by Jeremy Bauer <te...@gmail.com>.
Marcel,

If you are doing the find within the same em as you did the insert
(and haven't issued a clear) the find operation is getting the entity
from the cache instead of making a trip to the database.  You can
force a trip to the DB by doing a refresh after the find.

      CompanyProperty cp = em.find(CompanyProperty.class, key);
      em.refresh(cp);
      log.info(cp.getCreationTs());

-Jeremy