JPA, SQL Server Lob lazy load - sql-server

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.

Related

zero value returned by Hibernate save() method (with Sql Server database)

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...)

How to select specified columns and its values from SQL Server stored procedure result and convert them into JSON?

I am new to SQL Server. Here are my requirements:
Requirements:
I need to select specified column header and its values from SQL Server stored procedure result
The selected column header and its values are serialize into a JSON string object in SQL Server 2012
What I did:
I need to monitor the table data changes in SQL Server Management Studio 2012. If any insert, delete and updates occur in the old table data, I need to send instant messages from SQL Server to a WCF service.
To send instant messages, first of all, I need to select specified columns from the result set of a stored procedure, and serialize them as a JSON string.
I am followed Change Data Capture feature in database and its table for tracking table data changes.
For the above, I created a database and table with Change Data Capture feature enabled. Then I inserted values in table data.
At last, using the stored procedure, I view the table data changes as result.
My demo query:
DECLARE #from_lsn binary(10), #to_lsn binary(10)
SET #from_lsn = sys.fn_cdc_get_min_lsn('dbo_one')
SET #to_lsn = sys.fn_cdc_get_max_lsn()
SELECT
CT.__$start_lsn, CT.__$operation,
CASE CT.__$operation
WHEN 1 THEN 'Delete'
WHEN 2 THEN 'Insert'
WHEN 3 THEN 'Update - Pre'
WHEN 4 THEN 'Update - Post'
END AS Operation,
CT.*,
LSN.tran_begin_time, LSN.tran_end_time, LSN.tran_id
FROM
cdc.fn_cdc_get_all_changes_dbo_one (#from_lsn, #to_lsn, N'all update old') AS CT
INNER JOIN
cdc.lsn_time_mapping AS LSN ON CT.__$start_lsn = LSN.start_lsn
From the above result, I need to select the type of operation, changed column header and its values.
After selecting, I need to serialize them all as a JSON string, then pass the JSON object from SQL Server 2012 to a WCF service via a POST method.
How can I achieve this? Could anyone help me solve this?
Actually it's simple. First you need to create an object like this:
[DataContract]
public class Output
{
[DataMember]
public string Name { get; set; }
[DataMember]
public string ID { get; set; }
.
.
.
//all other parameters
}
Then put your stored procedure output into it. That's all. Then just return it with WCF service.

Insert unique records from one server to another

Yesterday I learned how to use ADO.NET to take a DataTable and bulk insert all records into a SQL server database. That works great for one part of my project. Now what I need to do is take the records from the DataTable and insert only the records that do not already exist in the SQL server. Im using VB2010 and the code looks something like:
'get the data from our Teradata server and fill up a DataTable
Dim dtCheck As New DataTable("TableCheck")
dtCheck = GetDataTableFromTeradataServer
'connect to our SQL server
Dim connSqlSvr As New System.Data.SqlClient.SqlConnection
connSqlSvr.ConnectionString = "Data Source=DestSqlServer;Initial Catalog=DestDb;Connect Timeout=15"
connSqlSvr.Open()
'how do I take records from the DataTable and insert only those records that do not
'already exist in the SQL server?
'close and dispose the SQL server database connection
connSqlSvr.Close()
connSqlSvr.Dispose()
The SQL server destination table [DestDb].[dbo].[Events] has fields
[CityCode],[CarNum],[VIN],[Fleet],[CarMileage],[EventItm],[EventDate]
and the DataTable in memory has fields
City,CarNo,VIN,Fleet,Mileage,EventDesc,EventDate
For my purposes a unique record is made up of [CityCode],[CarNum],[VIN],[EventItm], and [EventDate]
Typically i will run this routine once a day and will get back about 200 records from the source. Been looking for a couple hours but just not sure how to approach this.
You could try loading data in a staging table in the target server. Then do the unique insert from there.

Two day discrepancy with JPA, EclipseLink and SQL Server 2008 R2

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.

JPA: Set default column value to sequence in database

I am using Postgres as database and JPA 2.0 as provider.
I had been using #GeneratedValue(strategy = GenerationType.IDENTITY) which generates its own sequence for that column and also executes this query ( or similar )
ALTER TABLE "comment" ALTER COLUMN id SET DEFAULT nextval('comment_id_seq'::regclass);
Now I am using
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "GEN_COMMENT")
#SequenceGenerator(name = "GEN_COMMENT", sequenceName = "SEQ_COMMENT")
which also generates it's own sequence but doesn't execute that query.
My point is that when I had been using IDENTITY then database could automatically insert the column value from sequence which is helpful when there are other accesses into database except via application.
So my question is if there is any way how to force JPA to set default value of that column to sequence without redefinition of #Column(columnDefinition="...")?
Thanks a lot.
This is database specific, the Postgres has defined type serial as generation type IDENTITY which automatically creates this database structure.

Resources