Cannot set options after executing query; 5. An Example of association query; 6. Org.hibernate.QueryException: addEntity or addScalar must be called on a sql query before executing the query; 7. Executing spatial queries; 8. MDK Executing a function or an interrupt handler in RAM; 9. Javax.persistence.TransactionRequiredException: no transaction is in progress; 10. Nested exception is javax.persistence.TransactionRequiredException: Executing an update/delete query It may be because you do not have @Transaction annotation in your repository class (interface). I tried adding @Transaction annotation to calling service or calling controller, but it did not solve the exception.
Ranch Hand
posted 7 years agoHi Guys
I have a bit of a problem. In my code, I execute an insert query using entityManager.createNativeQuery(query).executeUpdate() to insert 1000 records at a time into the database. On my development machine, it works perfectly. In the live environment, it throws an exception;
Any idea what could be causing it? Perhaps a setting or something somewhere? We use Hibernate, Glassfish 2.1 and MySQL on both my development machine and the live server. I'm stumped, and any help will be much appreciated.
Many thanks,
Riaan
I have a bit of a problem. In my code, I execute an insert query using entityManager.createNativeQuery(query).executeUpdate() to insert 1000 records at a time into the database. On my development machine, it works perfectly. In the live environment, it throws an exception;
Any idea what could be causing it? Perhaps a setting or something somewhere? We use Hibernate, Glassfish 2.1 and MySQL on both my development machine and the live server. I'm stumped, and any help will be much appreciated.
Many thanks,
Riaan
'The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.' - George Bernard Shaw
Ranch Hand
posted 7 years agoFor future reference, we have tracked down the issue.
We had a not-null field in the live database which didn't have have a value in the insert query. It's an old field which was no longer used, and though I had already removed it from my database, it was still in the live database.
We had a not-null field in the live database which didn't have have a value in the insert query. It's an old field which was no longer used, and though I had already removed it from my database, it was still in the live database.
'The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.' - George Bernard Shaw
Interface used to control query execution.
- Since:
- JPA 1.0
- See Also:
TypedQuery
Parameter
Learn how to define and use JPA/JPQL queries in Chapter 4 of the ObjectDB/JPA manual.
Execute an update or delete statement.
- Returns:
- the number of entities updated or deleted
- Throws:
IllegalStateException
- if called for a Java Persistence query language SELECT statement or for a criteria queryTransactionRequiredException
- if there is no transactionQueryTimeoutException
- if the statement execution exceeds the query timeout value set and only the statement is rolled backPersistenceException
- if the query execution exceeds the query timeout value set and the transaction is rolled back
- Since:
- JPA 1.0
The position of the first result the query object was set to retrieve.
The position of the first result the query object was set to retrieve. Returns 0 if
setFirstResult
was not applied to the query object. - Returns:
- position of the first result
- Since:
- JPA 2.0
ObjectDB Notes: Learn how to define and use JPA/JPQL queries in Chapter 4 of the ObjectDB/JPA manual.
Get the flush mode in effect for the query execution.
Get the flush mode in effect for the query execution. If a flush mode has not been set for the query object, returns the flush mode in effect for the entity manager.
- Returns:
- flush mode
- Since:
- JPA 2.0
Get the properties and hints and associated values that are in effect for the query instance.
Get the properties and hints and associated values that are in effect for the query instance.
- Returns:
- query properties and hints
- Since:
- JPA 2.0
Get the current lock mode for the query.
- Returns:
- lock mode
- Throws:
IllegalStateException
- if the query is found not to be a Java Persistence query language SELECT query or a Criteria API query
- Since:
- JPA 2.0
The maximum number of results the query object was set to retrieve.
The maximum number of results the query object was set to retrieve. Returns
Integer.MAX_VALUE
if setMaxResults
was not applied to the query object. - Returns:
- maximum number of results
- Since:
- JPA 2.0
Get the parameter object corresponding to the declared positional parameter with the given position.
Get the parameter object corresponding to the declared positional parameter with the given position. This method is not required to be supported for native queries.
- Parameters:
position
- position
- Returns:
- parameter object
- Throws:
IllegalArgumentException
- if the parameter with the specified position does not existIllegalStateException
- if invoked on a native query when the implementation does not support this use
- Since:
- JPA 2.0
Get the parameter object corresponding to the declared parameter of the given name.
Get the parameter object corresponding to the declared parameter of the given name. This method is not required to be supported for native queries.
- Parameters:
name
- parameter name
- Returns:
- parameter object
- Throws:
IllegalArgumentException
- if the parameter of the specified name does not existIllegalStateException
- if invoked on a native query when the implementation does not support this use
- Since:
- JPA 2.0
Parameter<T>getParameter(int position, Class<T> type)
Get the parameter object corresponding to the declared positional parameter with the given position and type.
Get the parameter object corresponding to the declared positional parameter with the given position and type. This method is not required to be supported by the provider.
- Parameters:
position
- positiontype
- type
- Returns:
- parameter object
- Throws:
IllegalArgumentException
- if the parameter with the specified position does not exist or is not assignable to the typeIllegalStateException
- if invoked on a native query or Java Persistence query language query when the implementation does not support this use
- Since:
- JPA 2.0
Parameter<T>getParameter(String name, Class<T> type)
Get the parameter object corresponding to the declared parameter of the given name and type.
Get the parameter object corresponding to the declared parameter of the given name and type. This method is required to be supported for criteria queries only.
- Parameters:
name
- parameter nametype
- type
- Returns:
- parameter object
- Throws:
IllegalArgumentException
- if the parameter of the specified name does not exist or is not assignable to the typeIllegalStateException
- if invoked on a native query or Java Persistence query language query when the implementation does not support this use
- Since:
- JPA 2.0
Return the value bound to the positional parameter.
Return the value bound to the positional parameter.
- Parameters:
position
- position
- Returns:
- parameter value
- Throws:
IllegalStateException
- if the parameter has not been been boundIllegalArgumentException
- if the parameter with the specified position does not exist
- Since:
- JPA 2.0
Return the value bound to the named parameter.
- Parameters:
name
- parameter name
- Returns:
- parameter value
- Throws:
IllegalStateException
- if the parameter has not been been boundIllegalArgumentException
- if the parameter of the specified name does not exist
- Since:
- JPA 2.0
Return the value bound to the parameter.
- Parameters:
param
- parameter object
- Returns:
- parameter value
- Throws:
IllegalArgumentException
- if the parameter is not a parameter of the queryIllegalStateException
- if the parameter has not been been bound
- Since:
- JPA 2.0
Get the parameter objects corresponding to the declared parameters of the query.
Get the parameter objects corresponding to the declared parameters of the query. Returns empty set if the query has no parameters. This method is not required to be supported for native queries.
- Returns:
- set of the parameter objects
- Throws:
IllegalStateException
- if invoked on a native query when the implementation does not support this use
- Since:
- JPA 2.0
Execute a SELECT query and return the query results as an untyped List.
Execute a SELECT query and return the query results as an untyped List.
- Returns:
- a list of the results
- Throws:
IllegalStateException
- if called for a Java Persistence query language UPDATE or DELETE statementQueryTimeoutException
- if the query execution exceeds the query timeout value set and only the statement is rolled backTransactionRequiredException
- if a lock mode has been set and there is no transactionPessimisticLockException
- if pessimistic locking fails and the transaction is rolled backLockTimeoutException
- if pessimistic locking fails and only the statement is rolled backPersistenceException
- if the query execution exceeds the query timeout value set and the transaction is rolled back
- Since:
- JPA 1.0
Execute a SELECT query that returns a single untyped result.
Execute a SELECT query that returns a single untyped result.
- Returns:
- the result
- Throws:
NoResultException
- if there is no resultNonUniqueResultException
- if more than one resultIllegalStateException
- if called for a Java Persistence query language UPDATE or DELETE statementQueryTimeoutException
- if the query execution exceeds the query timeout value set and only the statement is rolled backTransactionRequiredException
- if a lock mode has been set and there is no transactionPessimisticLockException
- if pessimistic locking fails and the transaction is rolled backLockTimeoutException
- if pessimistic locking fails and only the statement is rolled backPersistenceException
- if the query execution exceeds the query timeout value set and the transaction is rolled back
- Since:
- JPA 1.0
Return a boolean indicating whether a value has been bound to the parameter.
Return a boolean indicating whether a value has been bound to the parameter.
- Parameters:
param
- parameter object
- Returns:
- boolean indicating whether parameter has been bound
- Since:
- JPA 2.0
Set the position of the first result to retrieve.
Set the position of the first result to retrieve.
- Parameters:
startPosition
- position of the first result, numbered from 0
- Returns:
- the same query instance
- Throws:
IllegalArgumentException
- if the argument is negative
- Since:
- JPA 1.0
Set the flush mode type to be used for the query execution.
Set the flush mode type to be used for the query execution. The flush mode type applies to the query regardless of the flush mode type in use for the entity manager.
- Parameters:
flushMode
- flush mode
- Returns:
- the same query instance
- Since:
- JPA 1.0
Set a query property or hint.
Set a query property or hint. The hints elements may be used to specify query properties and hints. Properties defined by this specification must be observed by the provider. Vendor-specific hints that are not recognized by a provider must be silently ignored. Portable applications should not rely on the standard timeout hint. Depending on the database in use and the locking mechanisms used by the provider, this hint may or may not be observed.
- Parameters:
hintName
- name of the property or hintvalue
- value for the property or hint
- Returns:
- the same query instance
- Throws:
IllegalArgumentException
- if the second argument is not valid for the implementation
- Since:
- JPA 1.0
Set the lock mode type to be used for the query execution.
Set the lock mode type to be used for the query execution.
- Parameters:
lockMode
- lock mode
- Returns:
- the same query instance
- Throws:
IllegalStateException
- if the query is found not to be a Java Persistence query language SELECT query or a Criteria API query
- Since:
- JPA 2.0
Set the maximum number of results to retrieve.
- Parameters:
maxResult
- maximum number of results to retrieve
- Returns:
- the same query instance
- Throws:
IllegalArgumentException
- if the argument is negative
- Since:
- JPA 1.0
Bind an argument to a positional parameter.
- Parameters:
position
- positionvalue
- parameter value
- Returns:
- the same query instance
- Throws:
IllegalArgumentException
- if position does not correspond to a positional parameter of the query or if the argument is of incorrect type
- Since:
- JPA 1.0
Bind the value of a
Parameter
object. - Parameters:
param
- parameter objectvalue
- parameter value
- Returns:
- the same query instance
- Throws:
IllegalArgumentException
- if the parameter does not correspond to a parameter of the query
- Since:
- JPA 2.0
Bind an argument to a named parameter.
- Parameters:
name
- parameter namevalue
- parameter value
- Returns:
- the same query instance
- Throws:
IllegalArgumentException
- if the parameter name does not correspond to a parameter of the query or if the argument is of incorrect type
- Since:
- JPA 1.0
QuerysetParameter(int position, Date value, TemporalType temporalType)
Bind an instance of
java.util.Date
to a positional parameter. Bind an instance of
java.util.Date
to a positional parameter. - Parameters:
position
- positionvalue
- parameter valuetemporalType
- temporal type
- Returns:
- the same query instance
- Throws:
IllegalArgumentException
- if position does not correspond to a positional parameter of the query or if the value argument is of incorrect type
- Since:
- JPA 1.0
QuerysetParameter(String name, Date value, TemporalType temporalType)
Bind an instance of
java.util.Date
to a named parameter. Bind an instance of
java.util.Date
to a named parameter. - Parameters:
name
- parameter namevalue
- parameter valuetemporalType
- temporal type
- Returns:
- the same query instance
- Throws:
IllegalArgumentException
- if the parameter name does not correspond to a parameter of the query or if the value argument is of incorrect type
- Since:
- JPA 1.0
QuerysetParameter(int position, Calendar value, TemporalType temporalType)
Bind an instance of
java.util.Calendar
to a positional parameter. Bind an instance of
java.util.Calendar
to a positional parameter. - Parameters:
position
- positionvalue
- parameter valuetemporalType
- temporal type
- Returns:
- the same query instance
- Throws:
IllegalArgumentException
- if position does not correspond to a positional parameter of the query or if the value argument is of incorrect type
- Since:
- JPA 1.0
QuerysetParameter( Parameter<Date> param, Date value, TemporalType temporalType)
Bind an instance of
java.util.Date
to a Parameter
object. Bind an instance of
java.util.Date
to a Parameter
object. - Parameters:
param
- parameter objectvalue
- parameter valuetemporalType
- temporal type
- Returns:
- the same query instance
- Throws:
IllegalArgumentException
- if the parameter does not correspond to a parameter of the query
- Since:
- JPA 2.0
QuerysetParameter(String name, Calendar value, TemporalType temporalType)
Bind an instance of
java.util.Calendar
to a named parameter. Bind an instance of
java.util.Calendar
to a named parameter. - Parameters:
name
- parameter namevalue
- parameter valuetemporalType
- temporal type
- Returns:
- the same query instance
- Throws:
IllegalArgumentException
- if the parameter name does not correspond to a parameter of the query or if the value argument is of incorrect type
- Since:
- JPA 1.0
QuerysetParameter( Parameter<Calendar> param, Calendar value, TemporalType temporalType)
Bind an instance of
java.util.Calendar
to a Parameter
object. Bind an instance of
java.util.Calendar
to a Parameter
object. - Parameters:
param
- parameter objectvalue
- parameter valuetemporalType
- temporal type
- Returns:
- the same query instance
- Throws:
IllegalArgumentException
- if the parameter does not correspond to a parameter of the query
- Since:
- JPA 2.0
Return an object of the specified type to allow access to the provider-specific API.
Return an object of the specified type to allow access to the provider-specific API. If the provider's query implementation does not support the specified class, the
PersistenceException
is thrown. - Parameters:
cls
- the class of the object to be returned. This is normally either the underlying query implementation class or an interface that it implements.
- Returns:
- an instance of the specified class
- Throws:
PersistenceException
- if the provider does not support the call
- Since:
- JPA 2.0