hibernate与sqlserver

  • 问题:hibernate连sqlserver时向数据库中插入数据,不能插入,提示以下错误:当 DENTITY_INSERT    设置为 OFF 时,不 能向表 'member' 中的标识列插入显式值
  • 原因是:我的数据库中的主键id设置的是自动增长类型的,与hibernate支持的不一样(在这儿我描述的可能不太准确)
  • 解决方案:在hibernate-mapping中的id 的子配置项genetor 的class设置成native,它的意思是使用数据库自己的生成方式(依赖于数据库方言),具体设置如下:
      <id name="mid" column="id">
       <generator class="native"/>
      </id>
  • 完整错误代码如下:

 Hibernate: insert into member (name, pwd, email, phone, grade, id) values (?, ?, ?, ?, ?, ?)
1218 [main] WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: 544, SQLState: 23000
1218 [main] ERROR org.hibernate.util.JDBCExceptionReporter - [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]当 IDENTITY_INSERT 设置为 OFF 时,不能向表 'member' 中的标识列插入显式值。
1218 [main] ERROR org.hibernate.event.def.AbstractFlushingEventListener - Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: could not insert: [com.hibernate.bean.MemberBean]
 at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:94)
 at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
 at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2295)
 at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2688)
 at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:79)
 at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
 at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
 at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167)
 at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
 at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
 at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)
 at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:365)
 at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
 at com.hibernate.bean.TestHib.insertUser(TestHib.java:35)
 at com.hibernate.bean.TestHib.main(TestHib.java:76)
Caused by: java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]当 IDENTITY_INSERT 设置为 OFF 时,不能向表 'member' 中的标识列插入显式值。
 at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
 at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
 at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processErrorToken(Unknown Source)
 at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReplyToken(Unknown Source)
 at com.microsoft.jdbc.sqlserver.tds.TDSRPCRequest.processReplyToken(Unknown Source)
 at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReply(Unknown Source)
 at com.microsoft.jdbc.sqlserver.SQLServerImplStatement.getNextResultType(Unknown Source)
 at com.microsoft.jdbc.base.BaseStatement.commonTransitionToState(Unknown Source)
 at com.microsoft.jdbc.base.BaseStatement.postImplExecute(Unknown Source)
 at com.microsoft.jdbc.base.BasePreparedStatement.postImplExecute(Unknown Source)
 at com.microsoft.jdbc.base.BaseStatement.commonExecute(Unknown Source)
 at com.microsoft.jdbc.base.BaseStatement.executeUpdateInternal(Unknown Source)
 at com.microsoft.jdbc.base.BasePreparedStatement.executeUpdate(Unknown Source)
 at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:46)
 at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2275)
 ... 12 more
Exception in thread "main" org.hibernate.exception.ConstraintViolationException: could not insert: [com.hibernate.bean.MemberBean]
 at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:94)
 at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
 at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2295)
 at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2688)
 at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:79)
 at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
 at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
 at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167)
 at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
 at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
 at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)
 at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:365)
 at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
 at com.hibernate.bean.TestHib.insertUser(TestHib.java:35)
 at com.hibernate.bean.TestHib.main(TestHib.java:76)
Caused by: java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]当 IDENTITY_INSERT 设置为 OFF 时,不能向表 'member' 中的标识列插入显式值。
 at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
 at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
 at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processErrorToken(Unknown Source)
 at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReplyToken(Unknown Source)
 at com.microsoft.jdbc.sqlserver.tds.TDSRPCRequest.processReplyToken(Unknown Source)
 at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReply(Unknown Source)
 at com.microsoft.jdbc.sqlserver.SQLServerImplStatement.getNextResultType(Unknown Source)
 at com.microsoft.jdbc.base.BaseStatement.commonTransitionToState(Unknown Source)
 at com.microsoft.jdbc.base.BaseStatement.postImplExecute(Unknown Source)
 at com.microsoft.jdbc.base.BasePreparedStatement.postImplExecute(Unknown Source)
 at com.microsoft.jdbc.base.BaseStatement.commonExecute(Unknown Source)
 at com.microsoft.jdbc.base.BaseStatement.executeUpdateInternal(Unknown Source)
 at com.microsoft.jdbc.base.BasePreparedStatement.executeUpdate(Unknown Source)
 at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:46)
 at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2275)
 ... 12 more

 

Hibernate和SQL Server上使用Atomikos进行事务管理的整合可以按照以下步骤进行: 1. 添加Atomikos和SQL Server的相关依赖项到你的项目中。你可以在Maven或Gradle中添加以下依赖项: ```xml <!-- Atomikos --> <dependency> <groupId>com.atomikos</groupId> <artifactId>atomikos-transactions-jta</artifactId> <version>4.0.6</version> </dependency> <dependency> <groupId>com.atomikos</groupId> <artifactId>atomikos-transactions-jdbc</artifactId> <version>4.0.6</version> </dependency> <!-- SQL Server --> <dependency> <groupId>com.microsoft.sqlserver</groupId> <artifactId>mssql-jdbc</artifactId> <version>8.2.2.jre8</version> </dependency> ``` 2. 配置Atomikos的事务管理器。在Spring Boot中,可以使用`JtaTransactionManager`来配置Atomikos。 ```java @Configuration public class TransactionConfig { @Bean public UserTransactionManager userTransactionManager() throws SystemException { UserTransactionManager userTransactionManager = new UserTransactionManager(); userTransactionManager.setForceShutdown(true); return userTransactionManager; } @Bean(initMethod = "init", destroyMethod = "close") public UserTransactionImp userTransaction() throws SystemException { UserTransactionImp userTransactionImp = new UserTransactionImp(); userTransactionImp.setTransactionTimeout(300); return userTransactionImp; } @Bean public JtaTransactionManager transactionManager(UserTransactionManager userTransactionManager, UserTransactionImp userTransaction) { JtaTransactionManager transactionManager = new JtaTransactionManager(); transactionManager.setUserTransaction(userTransaction); transactionManager.setTransactionManager(userTransactionManager); return transactionManager; } } ``` 3. 配置SQL Server的数据源。可以使用Spring Boot的自动配置来配置SQL Server的数据源。 ```properties spring.datasource.url=jdbc:sqlserver://localhost:1433;databaseName=mydatabase spring.datasource.username=your-username spring.datasource.password=your-password spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver spring.jpa.hibernate.ddl-auto=update ``` 4. 使用Hibernate进行数据库访问。在你的实体类上使用Hibernate的注解来映射数据库表。 ```java @Entity @Table(name = "your_table") public class YourEntity { // Entity fields and annotations } ``` 5. 在需要进行事务管理的方法或类上使用`@Transactional`注解。这将确保方法内的数据库操作在Atomikos管理的事务中执行。 ```java @Service @Transactional public class YourService { // Service methods } ``` 通过按照以上步骤配置Atomikos和SQL Server的整合,你可以实现对Hibernate和SQL Server的分布式事务管理。Atomikos将协调和管理涉及多个数据库的事务,确保事务的一致性和原子性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值