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!