我正在将数据库 SQL Server 迁移到 MYSQL。我已经设置了数据库,但是当我将其配置到 xml 配置文件并执行项目时,我收到了错误。
java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed
还有一件事是,hibernate 生成的所有 select 查询都生成成功,但是当 hibernate 执行插入查询时,会抛出相同的异常。
简单来说,它无法修改mysql中设置的数据库
这是我的 applicationcontext.xml 代码
<beans>
<!-- Hibernate -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test_live_30"/>
<property name="username" value="root" />
<property name="password" value="" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="useTransactionAwareDataSource" value="true" />
<property name="mappingResources">
<list>
<value>src/dao/hbms/Company.hbm.xml</value>
<value>src/dao/hbms/ProductClaimsLoading.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="connection.pool_size">1</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.generate_statistics">true</prop>
<prop key="hibernate.cglib.use_reflection_optimizer">false</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
最佳答案
我知道这篇文章已经过时了,但这条线索仍然可以帮助其他人。
我在使用 spring + hibernate + spring-data-repository 时遇到了类似的问题,其中我的 AbstractRepository 扩展了 SimpleJPARepository 类,我不得不在我的自定义方法中提到 @Transactional 注释,我在谷歌或其他地方找不到解决方案/线索。不知怎的,我发现新方法缺少这个 @Transactional 注释,一旦指定,就开始像魅力一样工作......!谢谢
关于java.sql.SQLException : Connection is read-only. hibernate中不允许导致数据修改的查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34605103/