使用spring整合hibernate,简单的实现添加,代码如下
package domain; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="tb_person") public class Person { @Id @Column(name="person_id") @GeneratedValue(strategy=GenerationType.IDENTITY) private Integer id; private String name; private String pass; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPass() { return pass; } public void setPass(String pass) { this.pass = pass; } public Person(String name, String pass) { this.name = name; this.pass = pass; } public Person() {} }
spring的beans.xml配置
<?xml version="1.0" encoding="GBK"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" p:configLocation="classpath:hibernate.cfg.xml"/> <bean id="hi" class="org.springframework.orm.hibernate4.HibernateTemplate" p:sessionFactory-ref="sessionFactory"/> </beans>
hibernate.cfg.xml配置
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> <!-- 驱动 --> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <!-- 数据库URL --> <property name="hibernate.connection.url">jdbc:mysql:///try_db</property> <!-- 用户名 --> <property name="hibernate.connection.username">root</property> <!-- 密码 --> <property name="hibernate.connection.password">root</property> <property name="hbm2ddl.auto">update</property> <property name="show_sql">true</property> <property name="format_sql">true</property> <mapping class="domain.Person"/> </session-factory> </hibernate-configuration>
使用下面的代码保存数据库时
public class Main { public static void main(String[] args) { ApplicationContext acx = new ClassPathXmlApplicationContext("beans.xml"); HibernateTemplate ht = acx.getBean("hi", HibernateTemplate.class); ht.saveOrUpdate(new Person("孙悟空", "123456")); } }
出现下面的错误
Exception in thread "main" org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove ""readOnly"" marker from transaction definition.
这是说只读吗?这个错误怎么样解决?
解决方案:10分
后面提示说:
Turn your Session into FlushMode.COMMIT/AUTO or remove “”readOnly”” marker from transaction definition.
你再看看这个:
show variables like “”autocommit%””;
Turn your Session into FlushMode.COMMIT/AUTO or remove “”readOnly”” marker from transaction definition.
你再看看这个:
show variables like “”autocommit%””;
解决方案:30分
这个还真不太了解。