I have strange issue with hibernate. I'm using annotated class as entity. The problem is that sometimes hibernate return empty list and after few trials it's eventually returns the data (after rebooting app). Hibernate doesn't throw any exception, no SQL select is printout to the console (normally in my app prints sql select query)
My table contain about 9000 rows.
Related
I am trying to insert 1000 records using ‘’’Hibernate jdbc batch’’’ and sometime we get unique constraints for one of the records. Is there anyway I can force hibernate to return which row data caused constraints issue?
Whenever constraint issue occurs hibernate return error just ‘’’ db constraints error’’’
I know I can go back to database and check but looking for some feature of hibernate which logs or return culprit data only.
My backed is oracle.
My java application when using mybatis (3.4.4) to query oracle back end is not returning all the rows when using lazy load.
Essentially,
when I query my database using some SQL tool (like oracle Sql Developer) I get 4000 results
when I query using selectCursor which leads to lazy load conn.selectCusror(query) I only get 560 result!
when I query using selectList which will fetch all result at once conn.selectList(query) I am getting 4000 results (matches with database)
Note: A Cursor offers the same results as a List, except it fetches data lazily using an Iterator. Documentation
This is how I am counting the number of records
Cursor<Object> cur = conn.selectCursor(query) ; // query definition is written below
int count =0;
for(Object ob : cur){
count++;
}
System.out.println(count);
QUERY
<select id="query" fetchSize="20000" resultType="java.util.Map" >
select distinct code from my CODES_VIEW
</select>
Can someone please advise why selectCursor is not giving all the 4000 results
After spending days I found this solution:
My result set from the query contains a row (561) which have null values in its column. This shouldn't be a problem as I can have null values in the columns I fetched . In this case code column has value null at row 561.
When fetching records via selectCursor (lazy method of fetching data) mybatis believes it is end of record when all the columns are null in the fetched row.
MyBatis, by default, returns null when all the columns of a returned
row are NULL. When this setting is enabled, MyBatis returns an empty
instance instead. Note that it is also applied to nested results (i.e.
collectioin and association). Since: 3.4.2
https://mybatis.org/mybatis-3/configuration.html
However, you won't face this problem when you use selectList as all the records are fetched at once and cursor doesn't keep on checking on how does the next fetched records look like .
Hence the solution is to put the below setting in the mybatis config.xml
<setting name="returnInstanceForEmptyRow" value="true" />
On our production application we recently become weird error from DB2:
Caused by: com.ibm.websphere.ce.cm.StaleConnectionException: [jcc][t4][2055][11259][4.13.80] The database manager is not able to accept new requests, has terminated all requests in progress, or has terminated your particular request due to an error or a force interrupt. ERRORCODE=-4499, SQLSTATE=58009
This occurs when hibernate tries to select data from one big table(More than 6 milions records and 320 columns).
I observed that when ResultSet lower that 10 elements, hibernate selects successfully.
Our architecture:
Spring 4.0.3
Hibernate 4.3.5
DB2 v10 z/Os
Websphere 7.0.0.31(with JDBC V9.7FP5)
This select works when I tried to executed this in Data Studio or when app is started localy from Tomcat(connected to production Data Source). I suppose that Data Source on Websphere is not corectly configured, but I tried some modifications and without results. I also tried to update JDBC Driver but that not helped. Actually I become then ERRORCODE = -1244.
Ok, so now I'm looking for any help ;).
I can obviously provide additional information when needed.
Maybe someone fighted earlier with this problem?
Thanks in advance!
We have the same problem and finally solved by running REORG and RUNSTAT on the table(s). In our case, databse and tables were damaged and after running both mentioned operations, it resolved.
This occurs when hibernate tries to select data from one big table(More than 6 milions records and 320 columns)
6 million records with 320 columns seems huge to be read at once through hibernate. How you tried creating a database cursor and streaming few records at a time? In plain JDBC it is done as follows
Statement stmt = conn.createStatement(java.sql.ResultSet.TYPE_FORWARD_ONLY,
java.sql.ResultSet.CONCUR_READ_ONLY);
stmt.setFetchSize(50); //fetch only 50 records at a time
while with hibernate you would need the below code
Query query = session.createQuery(query);
query.setReadOnly(true);
query.setFetchSize(50);
ScrollableResults results = query.scroll(ScrollMode.FORWARD_ONLY);
// iterate over results
while (results.next()) {
Object row = results.get();
// process row then release reference
// you may need to flush() as well
}
results.close();
This allows you to stream over the result set, however Hibernate will still cache results in the Session, so you’ll need to call session.flush() every so often. If you are only reading data, you might consider using a StatelessSession, though you should read its documentation beforehand.
Analyze the database table locking impact when using this approach.
I am trying to write a DAO using Spring's HibernateTemplate that supports deletion from a table based on several conditions. For example, sample SQL:
Delete from Employee where Name='E01' AND Dept='D01' AND Address='ADR01';
I wrote the below code for this query:
session.createQuery("delete from Employee where name=? and dept=? and address=?")
.setParameter(0, name).setParameter(1, dept).setParameter(2, address)
.executeUpdate();
it works fine if all columns in where clause have some values. However, if I try to delete records which have "NULL" in their any column, then it does not work.
The generated SQL Query is always of the form:
Delete from Employee where Name=? AND Dept=? AND Address=?;
which of course cannot handle NULL comparison - SQL requires "IS NULL" for checking NULL and "=null" doesn't do the trick here. So when I pass dept as null in Java code, the generated SQL would be of the form:
Delete from Employee where Name='E01' AND Dept=null AND Address='ADR01';
This does not delete the records from DB which have NULL values in Dept column, as the correct condition would be "Dept IS NULL"; and "Dept=null" does not work! Is there anyway to compare NULL values in where clause without using a native query?
NOTE:
I do not want to use deleteAll(Collection) method of HibernateTemplate, as it requires fetching the records first and then deleting them - i.e. more than 1 SQL query. I want to achieve deletion using a single SQL query, without requiring to select first, or requiring native queries.
I am aware that Spring advices using SessionFactory now, but I am stuck with HibernateTemplate in existing code base.
A criteria is used to retrieve data from database. It generates SQL Query perfectly, which is tested on mySql separately and the records are loaded correctly;However, when using Criterial.list() it gives me an empty list.
I have checked my DB connections, and they are all correct. What would have caused this problem?
UPDATED
Here is my code:
accCr = DetachedCriteria.forClass(TSESpotInvestorAccount.class, "acc");
accCr.setResultTransformer(DetachedCriteria.DISTINCT_ROOT_ENTITY);
accCr.add(Restrictions.eq("exchangeDepositNo", filter.getBc()));
accCr.setProjection(Projections.id());
List accIds = getHibernateTemplate().findByCriteria(accCr);
Your code doesn't make much sense:
accCr.setResultTransformer(DetachedCriteria.DISTINCT_ROOT_ENTITY);
The above line says that the query is supposed to return entities (with, potentially, joined entities), and that Hibernate should return only distinct entities
accCr.setProjection(Projections.id());
The above line says that the query must only return one scalar column: the ID of the root entity.
If what you want is a list of IDs, then don't set the distinct root entity result transformer.