When pulling dates from a view using JPA and EclipseLink I'm getting dates two days before the date in the SQL data. (ie 1965-01-01 becomes 1964-12-30 and 1998-12-31 becomes 1998-12-29)
I am mapping date fields in SQL Server 2008 R2 with the following annotations:
#Entity
#Table(name = "vw_Record")
#XmlRootElement
public class VwRecord implements Serializable {
#Column(name = "dateStart")
#Temporal(TemporalType.DATE)
private Date dateStart;
#Column(name = "dateEnd")
#Temporal(TemporalType.DATE)
private Date dateEnd;
The columns in SQL Server are defined as:
[dateStart] [date] NULL,
[dateEnd] [date] NULL
Any ideas why I am getting a consistent two day difference?
EclipseLink does not do any conversion, it is most likely occurring in JDBC.
You can trying executing a native SQL query through JPA and see what data it returns. Also try selecting the data through raw JDBC.
Related
I have A column in SQL Server defined as
body varchar(max)
And JPA entity with definition of this column
#Lob
#Column(name = "body")
private String body;
Is it possible to fetch this cloba lazyly? I dont want to load this clobs in memory every time when entity is fetched from DB. I've tried to add
#Basic(fetch=FetchType.LAZY)
But it didn't work.
I am facing an issue with Spring MVC and Hibernate 4.2.1.
With Oracle DB connection, when save() method is performed I obtain a Long value that describes the ID of the saved record. Using a Sql Server instance, otherwise, the save() returns always a Long with 0 value.
I've printed the query in the console and I've noticed that the ID field is missing (when the operation is performed on Oracle, the ID field is present).
Following, the snippet of the entity class with the highlight on ID column:
#Id
#GeneratedValue(strategy = GenerationType.AUTO, generator = "seq")
#SequenceGenerator(name="seq", sequenceName="SEQ_PEZZI")
#Basic(optional = false)
#Column(name = "id")
private Long id;
The field type in the database is bigint.
I've also tried to change the GenerationType with IDENTITY without success (the type SEQUENCE seems not supported in SQL Server...)
I am trying to run unit tests for my SQL Server query. Query is simply inserting date to the table. I tried two different formats but didn't work:
parameters.addValue(STUDY_DATE, getDate(studyEvent.getStudy().getStudyDate()));
Timestamp timestamp = new Timestamp(getDate(studyEvent.getStudy().getStudyDate()).getTimeInMillis());
parameters.addValue(STUDY_DATE, timestamp);
And this is getDate() method that returns Calendar object:
private Calendar getDate(long time) {
Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("America/New_York"));
calendar.setTimeInMillis(time);
return calendar;
}
I am not sure if the problem is that SQL Server's datetime format issue or hsqldb issue. Here is my hsqldb create table:
SET DATABASE SQL SYNTAX MSS TRUE;
DROP TABLE event_study IF EXISTS;
CREATE TABLE event_study
(
STUDY_ID INT,
STUDY_DATE DATE
)
Is my hsqldb setup wrong? or should I use different datetime format for SQL Server?
Error I am getting is:
data exception: invalid datetime format; nested exception is java.sql.SQLDataException: data exception: invalid datetime format
and SQL query that I am running is:
INSERT INTO event_study(study_id, study_date)
SELECT x.*
FROM (VALUES(:study_id, :study_date))
AS x(study_id, study_date)
WHERE NOT EXISTS (SELECT 1 FROM event_study s WHERE s.study_id = x.study_id)
As you are not using strings for dates, this is not actually a formatting issue, but a Java type issue. With your table definition, DATE does not have time information. You can create and use a java.sql.Date object for the parameter value. If you want a datetime column, which includes information on time of the day, then use TIMESTAMP in your table definition and a java.sql.Timestamp for the parameter value.
In either case, you cannot use a Calendar object as parameter value.
I have read several post around the problem but found not solution to the issue I'm facing with.
My entity model contains several date properties whose values I need to be set at SQL server level. Here's an example:
[Column(IsDbGenerated = true)]
public DateTime DateCreated { get; set; }
DateCreated is a 'date' type on SQL Server, and its default value is GETDATE().
[DateCreated] DATE DEFAULT (getdate()) NOT NULL,
As a matter of fact saving a new record (without passing any DateCreated value) results in '1/1/0001' (i.e. null datetime) value being inserted.
It looks like Linq overrides default server GETDATE() value, forcing a 'null' value to be written.
You must use DatabaseGenerationOption.Identity.
Here are two links that explain further:
Entity Framework Code First Data Annotations
How do I tell Entity Framework to allow SQL Server to provide a defined default value for a field?
I am testing an application on server with SQL Server 2005 and I am getting the following error (the application runs fine with SQL Server 2008 R2 and 2012 versions).
There is no store type corresponding to the conceptual side type 'Edm.Time(Nullable=True,DefaultValue=,Precision=)' of PrimitiveType 'Time'.`
The code inside the Controller that might responsible for the error is ('might' because when this code is invoked the database does not exist yet and Entity Framework is supposed to generate the database)
var tempRegistrations = db.Registrations.Where(t => t.DateExpires < DateTime.Now).ToList();
The code in my Model responsible for the entry in question is
public class Registrations
{
public int RegistrationsId { get; set; }
// ...
public DateTime? DateExpires { get; set; }
}
Both, SQL Server 2008 R2 and 2012 versions had datetime data type (nullable as well) in the generated table. I am quite sure SQL Server 2005 has datetime datatype. So what is wrong here?
Thanks.
Seems SQL 2005 doesn't support the Time datatype, which is what it's trying to map it to. Perhaps change it on the Edm side to use
datetime?
http://msdn.microsoft.com/en-us/library/bb896344.aspx
PrimitiveType 'Time'
Are you sure it's not trying to create a datatype of time? 2005 does not have this type.
I'm not sure if this is what you're using, but it looks like there is a difference between time and datetime.
http://msdn.microsoft.com/en-us/library/ee382832.aspx