发帖    主题    评论    推荐    标签    作者    订阅    查搜    注册   登陆   关注
 
面向对象 设计模式 领域驱动设计 企业架构 框架 开发教程 微服务 大数据 扩展性 并发编程 事件驱动 分布式 SOA

spring 联合hibernate不能保存数据?

2008-06-12 17:28
赞助商链接

spring 联合hibernate不能保存数据???????????? 程序没报错
我用的数据库是mysql5.0; 高手们,帮我指点一下啊,谢谢了啊
这是我的测试代码:
package springdao;

import org.hibernate.Transaction;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Springdaotest {

/**
* @param args
*/
public static void main(String[] args) {
ApplicationContext ctx=
new ClassPathXmlApplicationContext("applicationContext.xml");
BbsuserDAO dao=(BbsuserDAO)ctx.getBean("BbsuserDAO");
Bbsuser user=new Bbsuser();
user.setUsername("lmbmb");
user.setPassword("12323");
System.out.println(user.getUsername());



dao.save(user);
System.out.println(user.getPassword());

}

}

这是Bbsuser.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="springdao.Bbsuser" table="bbsuser" catalog="test">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="increment" />
</id>
<property name="username" type="java.lang.String">
<column name="username" length="10" />
</property>
<property name="password" type="java.lang.String">
<column name="password" length="10" />
</property>
<property name="age" type="java.lang.String">
<column name="age" length="10" />
</property>
</class>
</hibernate-mapping>

这是applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>

<bean id="man" name="man_test" class="springtest.Man">
<property name="message">
<value>你好!!aaaa</value>
</property>
</bean>
<bean id="BbsuserDAO" class="springdao.BbsuserDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>


</beans>


这是BbsuserDAO.java
package springdao;


public class BbsuserDAO extends HibernateDaoSupport {
private static final Log log = LogFactory.getLog(BbsuserDAO.class);
// property constants
public static final String USERNAME = "username";
public static final String PASSWORD = "password";
public static final String AGE = "age";

protected void initDao() {
// do nothing
}

public void save(Bbsuser transientInstance) {
log.debug("saving Bbsuser instance");
try {

getHibernateTemplate().save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}

public void delete(Bbsuser persistentInstance) {
log.debug("deleting Bbsuser instance");
try {
getHibernateTemplate().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}


public static BbsuserDAO getFromApplicationContext(ApplicationContext ctx) {
return (BbsuserDAO) ctx.getBean("BbsuserDAO");
}
}


高手们,帮我指点一下啊!!!!!!!!!

2008-06-13 17:36

这是一个老问题,需要配置Spring的事务才能用Hibernate

赞助商链接

赞助商链接

返回顶部

移动版 关于本站 使用帮助 联系管理员 最佳分辨率1366x768
OpenSource JIVEJDON Powered by JdonFramework Code © 2002-20 jdon.com