Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I set XACT_ABORT using JPA/Hibernate?
    primarykey
    data
    text
    <p>I am using a hibernate entity that maps to a view in MS SQLServer. The view consists of data selected from a table on a linked server. I am able to successfully query this view using JPQL. However, when I try to update the view using entityManager.merge(myEntity), I get the following error:</p> <blockquote> <p>com.microsoft.sqlserver.jdbc.SQLServerException: Unable to start a nested transaction for OLE DB provider "MSDAORA" for linked server "DBNAME". A nested transaction was required because the XACT_ABORT option was set to OFF. at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:216) at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1515) at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:404) at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:350) at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:5696) at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1715) at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:180) at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:155) at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeUpdate(SQLServerPreparedStatement.java:314)...</p> </blockquote> <p>I need to set XACT_ABORT to ON so that SQLServer can rollback the entire transaction if an error occurs. I was able to work around the issue using a native query, but it's not really ideal.</p> <pre><code> String queryString = """set xact_abort on; update table_name set column1 = :column1, column2 = :column2 where id = :id""" Query query = entityManager.createNativeQuery(queryString) query.setParameter("column1", column1) query.setParameter("column2", column2) query.setParameter("id", id) query.executeUpdate() </code></pre> <p><strong>Is there any way I can configure hibernate/JPA/JPAVendorAdaptor to execute the "set xact_abort on" for every merge/persist so I don't have to use a native query?</strong></p> <p>Here is my hibernate configuration:</p> <pre><code>@Resource DataSource dataSource @Bean JpaVendorAdapter vendorAdapter(){ return new HibernateJpaVendorAdapter(database:Database.SQL_SERVER) } @Bean AbstractEntityManagerFactoryBean entityManagerFactory(){ LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(dataSource:dataSource, jpaVendorAdapter:vendorAdapter()) em.setPackagesToScan("com.mycompany.entity") return em } @Bean PlatformTransactionManager transactionManager() { return new JpaTransactionManager(entityManagerFactory:entityManagerFactory().getObject()) } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload