我是新的春天,并试图休眠与Spring集成。我使用的MySQL数据库和尝试插入使用saveOrUpdate()方法的方法记录。
因此,该程序发射一个SQL查询用于创建表(因为它应该)。
但问题是,即使它被烧成“创建表”查询,该表没有被在数据库中创建。另外,如果是手动创建数据库表,然后尝试插入记录时,它什么都不做。
我已经使用save(),坚持()方法,而不是saveOrUpdate()方法试过,但我花了无处。
这是主类。 bean类(员工)的价值是为了插入一条记录被设置。
public static void main( String[] args )
{
ApplicationContext context=new
ClassPathXmlApplicationContext("sphb.xml");
EmployeeDAO edao=(EmployeeDAO) context.getBean("d");
Employee e=new Employee();
e.setId(1);
e.setName("sourav");
e.setSalary(100000);
edao.saveEmployee(e);
}
这是bean类: - 公共类Employee {私人诠释ID;私人字符串名称;私人诠释工资; // getter和setter}
这是一个包含所有配置XML文件。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver">
</property>
<property name="url" value="jdbc:mysql://localhost:3306/db"></property>
<property name="username" value="root"></property>
<property name="password" value="1234"></property>
</bean>
<bean id="mysessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mappingResources">
<list>
<value>mapping.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop
key
="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="template"
class="org.springframework.orm.hibernate5.HibernateTemplate">
<property name="sessionFactory" ref="mysessionFactory"></property>
<property name="checkWriteOperations" value="false"></property>
</bean>
<bean id="d" class="demo.sphbIntegrate.EmployeeDAO">
<property name="template" ref="template"></property>
</bean>
</beans>
这是DAO类: -
public class EmployeeDAO
{
HibernateTemplate template;
public void setTemplate(HibernateTemplate template) {
this.template = template;
}
public void saveEmployee(Employee e)
{
template.saveOrUpdate(e);
}
}
当不在数据库中手动创建表输出: -
log4j:WARN No appenders could be found for logger
(org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for
more info.
**Hibernate: create table employee (id integer not null, name varchar(255),
salary integer, primary key (id)) type=MyISAM
Hibernate: select employee_.id, employee_.name as name2_0_,
employee_.salary as salary3_0_ from employee employee_ where employee_.id=?
Exception in thread "main"
org.springframework.dao.InvalidDataAccessResourceUsageException: could not
extract ResultSet; SQL [n/a]; nested exception is
org.hibernate.exception.SQLGrammarException: could not extract ResultSet**
at org. springframework. orm. hibernate5. SessionFactoryUtils.
convertHibernateAccessException (SessionFactoryUtils.java:230)
at org. springframework. orm. hibernate5. HibernateTemplate. doExecute
(HibernateTemplate.java:387)
and 15 more...
当手动创建的输出表中: -
log4j:WARN No appenders could be found for logger
(org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for
more info.
**Hibernate: select employee_.id, employee_.name as name2_0_,
employee_.salary as salary3_0_ from employee employee_ where employee_.id=?**
就在执行该选择查询,而不是插入一个。
使其通过添加它使用注释管理事务工作
<bean id="txManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="mysessionFactory"></property>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
还增加setter方法SessionFactory的和使用的标注为交易和给出的写操作权限。
public class EmployeeDAO
{
HibernateTemplate template;
public void setTemplate(HibernateTemplate template) {
this.template = template;
}
public void setSessionFactory(SessionFactory sessionFactory)
{
this.template=new HibernateTemplate(sessionFactory);
}
@Transactional(readOnly=false)
public void saveEmployee(Employee e)
{
template.saveOrUpdate(e);
}
}