| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2069 人关注过本帖
标题:spring 联合hibernate不能保存数据???
只看楼主 加入收藏
e00tang
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2006-12-25
收藏
 问题点数:0 回复次数:1 
spring 联合hibernate不能保存数据???
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.
<!--
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.
<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;

import java.util.List;
import org.
import org.
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
* A data access object (DAO) providing persistence and search support for
* Bbsuser entities. Transaction control of the save(), update() and delete()
* operations can directly support Spring container-managed transactions or they
* can be augmented to handle user-managed Spring transactions. Each of these
* methods provides additional information for how to configure it for the
* desired type of transaction control.
*
* @see springdao.Bbsuser
* @author MyEclipse Persistence Tools
*/

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 Bbsuser findById(java.lang.Integer id) {
log.debug("getting Bbsuser instance with id: " + id);
try {
Bbsuser instance = (Bbsuser) getHibernateTemplate().get(
"springdao.Bbsuser", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}

public List findByExample(Bbsuser instance) {
log.debug("finding Bbsuser instance by example");
try {
List results = getHibernateTemplate().findByExample(instance);
log.debug("find by example successful, result size: "
+ results.size());
return results;
} catch (RuntimeException re) {
log.error("find by example failed", re);
throw re;
}
}

public List findByProperty(String propertyName, Object value) {
log.debug("finding Bbsuser instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from Bbsuser as model where model."
+ propertyName + "= ?";
return getHibernateTemplate().find(queryString, value);
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
}

public List findByUsername(Object username) {
return findByProperty(USERNAME, username);
}

public List findByPassword(Object password) {
return findByProperty(PASSWORD, password);
}

public List findByAge(Object age) {
return findByProperty(AGE, age);
}

public List findAll() {
log.debug("finding all Bbsuser instances");
try {
String queryString = "from Bbsuser";
return getHibernateTemplate().find(queryString);
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
}

public Bbsuser merge(Bbsuser detachedInstance) {
log.debug("merging Bbsuser instance");
try {
Bbsuser result = (Bbsuser) getHibernateTemplate().merge(
detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
}

public void attachDirty(Bbsuser instance) {
log.debug("attaching dirty Bbsuser instance");
try {
getHibernateTemplate().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}

public void attachClean(Bbsuser instance) {
log.debug("attaching clean Bbsuser instance");
try {
getHibernateTemplate().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}

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


高手们,帮我指点一下啊!!!!!!!!!
搜索更多相关主题的帖子: spring hibernate 数据库 import args 
2008-06-12 17:28
guoxhvip
Rank: 8Rank: 8
来 自:聖西羅南看臺
等 级:贵宾
威 望:44
帖 子:4052
专家分:135
注 册:2006-10-8
收藏
得分:0 
缺少事务提交

愛生活 && 愛編程
2008-06-13 00:02
快速回复:spring 联合hibernate不能保存数据???
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.099686 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved