I'm using Spring Boot with Spring Data JPA to map an Entity to a table in a SQL Server database for which I've created an #Embeddable composite key. There's a column I'd like to use as part of the key and according to SQuirreL its type name is datetime and the class name is java.sql.Timestamp. The key class looks like this:
#Embeddable
public class MyEntityIdentifier implements Serializable {
#Column(name = "LastUpdateDateTime")
private Timestamp lastUpdateDateTime;
...but the lastUpdateDateTime property always resolves to null without error. I've checked and there are no null fields for this column. I've also tried resolving to java.util.Date without success. Is there another type I should be using or something I'm doing wrong?
Hibernate will internally convert to a native Java type (i.e. java.util.Date as opposed to java.sql.Timestamp) by adding the #Temporal annotation.
#Column(name = "LastUpdateDateTime")
#Temporal(TemporalType.DATE)
private Date lastUpdateDateTime;
Related
I have an Entity CustomEntity with a data member updateDate of type OffsetDateTime.
I have defined a Repository for this Entity which has a simple method to retrieve list of records matching updateDate as
List<CustomEntity> findByUpdateDate(OffsetDateTime updateDate);
Now, when this method is called from Controller/Service bean, I can see no matching record is retrieved; however, when I execute the generated SQL in the DB, I can see matching rows available.
I can retrieve the records based on other data members of the entity; its just an issue with OffsetDateTime and LocalDateTime
I got to understand that java.time package support was not in JPA 2.1; however I am using JPA 2.3.1. Do I need to use Converters (as suggested for JPA 2.1?
Any help is much appreciable.
EDIT :-
Below is the code for Entity
#Entity
#Table(name = "SAMPLE_TABLE")
public class CustomEntity {
#Id
#GeneratedValue
private Long id;
#Column
private OffsetDateTime updateDate;
//Getters & Setters
}
I am using Microsoft SQL Server and the generated SQL query (hibernate generated) looks something like below
select sample0_.id as id1_10, sample0_.updateDate as update2_10 from sample_table sample0_ where sample0_.updateDate=?
binding parameter [1] as [TIMESTAMP] - [2021-07-27T17:22:34.597Z]
I have something like this for defining the hibernate type mapping in the entity class:
#Entity
#Table(name = "TEST_TABLE")
public class Test {
#Type( type = "jsonb" )
#Column(name = "CONTENT_FILES")
private List<ContentFile> contentFiles;
}
which map an entity field to a custom defined hibernate type jsonb for supporting PostgreSQL DB.
I would like to change the mapping to another hibernate custom type json for supporting MSSQL DB.
Can I support both mappings in the same entity class?
I tried to use the #Profile annotation but it doesn't work.
#Profile("pgsql")
#Type( type = "jsonb" )
#Profile("mssql")
#Type( type = "json" )
#Column(name = "CONTENT_FILES")
private List<ContentFile> contentFiles;
json and jsonb both data types are almost identical according to the PostgreSQL documentation.Therefor you don't have to maintain two different data types to keep json in MSSQL and in PostgreSQL .
Please refer to below link of
PostgreSQL documentation.
I have an entity that has a filed of type java.util.Date (or Timestamp, doesn't matter for the case).
public class StatusReason {
#Column("SRN_ID")
private Long id;
#Column("SRN_CREATOR")
private String creator;
#Column("SRN_REASON")
private String reason;
#Column("SRN_STATUS")
private String status;
#Column("SRN_TIMESTAMP")
private Date timestamp;
//getters, setters, etc...
}
The database is Oracle and the corresponding column is of type TIMESTAMP(6) WITH TIME ZONE
When I call any of the default findById or findAll methods of the repository I get:
ConverterNotFoundException: No converter found capable of converting from type [oracle.sql.TIMESTAMPTZ] to type [java.util.Date].
I can create a custom RowMapper for the type and it will work fine.
I was just wondering if it's possible to register a custom converter (in my case from oracle.sql.TIMESTAMPTZ to java.util.Date) so can still benefit from the default mapping and use the converter through the whole app.
You can register custom conversions by inheriting your configuration from JdbcConfiguration (Spring Data JDBC v1.x) or AbstractJdbcConfiguration (v2.x). Then overwrite the method jdbcCustomConversions().
JdbcCustomConversions takes a list of Converter as an argument.
I have a PostgreSQL database schema. And I am trying to generate JPA entities from that schema in IntelliJ with the integrated persistence tool. Everything works fine, except the mapping of timestamps.
The persistence tool is trying to map the PostgreSQL TIMESTAMP data type to java.lang.Object or java.io.Serializable. I can't change the mapping to LocalDateTime, String or anything else.
Is there any way to set the correct mapping types?
UPDATE:
I get the following exception:
SchemaManagementException: Schema-validation: wrong column type encountered in column [timestamp] in table [ProcessEvent]; found [timestamptz (Types#TIMESTAMP)], but expecting [bytea (Types#VARBINARY)]
Have you used the proper annotation for the attribute?
#Entity
public class Employee {
...
#Basic
#Temporal(DATE)
private Calendar startDate;
...
}
Please see: https://en.wikibooks.org/wiki/Java_Persistence/Basic_Attributes#Temporal.2C_Dates.2C_Times.2C_Timestamps_and_Calendars
I have been configuring Spring auditing for my entity classes. Using annotations, I have something like this:
#CreatedDate
#NotNull
private Date createdDate
#CreatedBy
#NotNull
private User createdBy
The createdBy field is being set correctly, however persisting the object fails with a null createdDate. I am guessing that this may be related to type conversion for Eclipselink?
#Temporal annotation is available since the release of JPA 1.0. #Temporal solves the one of the major issue of converting the date and time values from Java object to compatible database type and retrieving back to the application.
#Column(name = "XDATE")
#Temporal(TemporalType.DATETIME)
private Date xDate; //java.util.Date
I hope this will resolve your problem. For more info please refer this link