[英]Unable to create a table automatically and insert records using spring and hibernate integration
我是Spring的新手,正在嘗試將Hibernate與Spring集成在一起。 我正在使用MySql數據庫,並嘗試使用saveOrUpdate()方法插入一條記錄。
因此,該程序會觸發一個用於創建表的sql查詢(應如此)。
但是問題是,即使它正在觸發“創建表”查詢,也沒有在數據庫中創建表。 另外,如果表是在數據庫中手動創建的,然后嘗試插入記錄,則它不會執行任何操作。
我嘗試使用save()和persist()方法而不是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 {private int id; 私有字符串名稱; 私人int工資; // getters和setters}
這是包含所有配置的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"/>
還為sessionFactory添加了setter,並為事務使用了注釋,並賦予了寫操作權限。
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); } }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.