-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Daabase Generated property value not working
PostPosted: Sun Nov 27, 2016 12:06 pm 
Newbie

Joined: Sun Nov 27, 2016 11:44 am
Posts: 3
Hi, I'm using Hibernate 5.2.5 with a Postgresql 9.4 Database.

I'm trying to set the value for a timestamp field using de Database Function. I'm doing almost exactly the same of the documentation example, but it's not working, the field lastActivityTime is missing in the INSERT statement and according to the documentation it should appear and in the VALUES clause should have LOCALTIMESTAMP as value. I checked that the methods of the class where the generation is defined are all called at startup.

Can anybody tell me if i'm missing something please?

My Annotation is @LocalTimestamp

This is my Entity Class (SessionDto)

Code:
@Entity(name="session")
public class SessionDto {
   
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long sessId;
   
    @Temporal(TemporalType.TIMESTAMP)
    @Column(insertable=false)
    private Calendar creationTime;
   
    private Long accoId;
   
    @ManyToOne
    @JoinColumn(name = "accoId", insertable=false, updatable=false)
    private AccountDto account;
   
    private String ipAddr;
   
    private Short statId;
   
    @LocalTimestamp
    @Temporal(TemporalType.TIMESTAMP)
    private Calendar lastActivityTime;
   
    private byte[] sid;

}


This is the annotation definition:

Code:
@ValueGenerationType(generatedBy = LocalTimestampImpl.class)
@Retention(RetentionPolicy.RUNTIME)
public @interface LocalTimestamp {}


And this is the Class that implements the AnnotationValueGeneration interface:

Code:
public class LocalTimestampImpl implements AnnotationValueGeneration<LocalTimestamp> {
   
    @Override
    public void initialize(LocalTimestamp annotation, Class<?> propertyType) {
    }

    /**
     * Generate value on ALWAYS
     * @return when to generate the value
     */
    @Override
    public GenerationTiming getGenerationTiming() {
        return GenerationTiming.ALWAYS;
    }

    /**
     * Returns null because the value is generated by the database.
     * @return null
     */
    @Override
    public ValueGenerator<?> getValueGenerator() {
        return null;
    }

    /**
     * Returns true because the value is generated by the database.
     * @return true
     */
    @Override
    public boolean referenceColumnInSql() {
        return true;
    }

    /**
     * Returns the database-generated value
     * @return database-generated value
     */
    @Override
    public String getDatabaseGeneratedReferencedColumnValue() {
        return "LOCALTIMESTAMP";
    }   
}


this is the insert statement executed by hibernate:

insert
into
session
(acco_id, ip_addr, sid, stat_id)
values
(?, ?, ?, ?)

and according to the documentation, it should be:

insert
into
session
(acco_id, ip_addr, sid, last_activity_time, stat_id)
values
(?, ?, ?, LOCALTIMESTAMP, ?)


Thanks!


Top
 Profile  
 
 Post subject: Re: Daabase Generated property value not working
PostPosted: Sun Nov 27, 2016 1:39 pm 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
You can run the test from our documentation, it's on GitHub.

This way, during debug you can determine what's the difference that causes our test to works and yours to fail.


Top
 Profile  
 
 Post subject: Re: Database Generated property value not working
PostPosted: Mon Nov 28, 2016 11:13 am 
Newbie

Joined: Sun Nov 27, 2016 11:44 am
Posts: 3
Trying to undestand why this is not working, I found something strange to me.

Looking at the class PropertyFinder in the org.hibernate.cfg.annotations package I found this method:

Code:
   private ValueGeneration determineValueGenerationStrategy(XProperty property) {
      ValueGeneration valueGeneration = getValueGenerationFromAnnotations( property );

      if ( valueGeneration == null ) {
         return NoValueGeneration.INSTANCE;
      }

      final GenerationTiming when = valueGeneration.getGenerationTiming();

      if ( valueGeneration.getValueGenerator() == null ) {
         insertable = false;
         if ( when == GenerationTiming.ALWAYS ) {
            updatable = false;
         }
      }

      return valueGeneration;
   }


In simple words, it says that when the valueGenerator is null (the case for the database generated values) it always sets "insertable" to "false" and also "updatable" to "false" when timing is Always. Probably, this is the reason because the field is not present in the INSERT statement, and it looks like a bug or a misunderstanding.

Any comments?
Regards
Pablo


Top
 Profile  
 
 Post subject: Re: Daabase Generated property value not working
PostPosted: Mon Nov 28, 2016 2:12 pm 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
I ran our test from the documentation module and it's working just fine:

Code:
insert
    into
        Event
        ("timestamp", id)
    values
        (current_timestamp, ?)


Top
 Profile  
 
 Post subject: Re: Daabase Generated property value not working
PostPosted: Mon Nov 28, 2016 3:19 pm 
Newbie

Joined: Sun Nov 27, 2016 11:44 am
Posts: 3
I have the test code, but I don't actually know how to run it :-)


Top
 Profile  
 
 Post subject: Re: Daabase Generated property value not working
PostPosted: Tue Nov 29, 2016 12:38 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
You can run it from your IDE.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.