Hibernate 连接 Mysql save()问题

xiaomaha 2008-08-07 03:31:17
spring_ioc.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="UserDAO" class="com.po.UserDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>

</beans>

hibernate.cfg.xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>

<session-factory>
<property name="connection.username">root</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/test
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="myeclipse.connection.profile">mysql</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<mapping resource="com/po/User.hbm.xml" />

</session-factory>

</hibernate-configuration>

PO

package com.po;

/**
* User generated by MyEclipse Persistence Tools
*/

public class User implements java.io.Serializable {

// Fields

private Integer userId;

private String userName;

private String userSex;

private Integer userAge;

// Constructors

/** default constructor */
public User() {
}

/** minimal constructor */
public User(String userName) {
this.userName = userName;
}

/** full constructor */
public User(String userName, String userSex, Integer userAge) {
this.userName = userName;
this.userSex = userSex;
this.userAge = userAge;
}

// Property accessors

public Integer getUserId() {
return this.userId;
}

public void setUserId(Integer userId) {
this.userId = userId;
}

public String getUserName() {
return this.userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public String getUserSex() {
return this.userSex;
}

public void setUserSex(String userSex) {
this.userSex = userSex;
}

public Integer getUserAge() {
return this.userAge;
}

public void setUserAge(Integer userAge) {
this.userAge = userAge;
}

}

DAO通过Hibernate反转过来的

package com.po;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
* Data access object (DAO) for domain model class User.
*
* @see com.po.User
* @author MyEclipse Persistence Tools
*/

public class UserDAO extends HibernateDaoSupport {
private static final Log log = LogFactory.getLog(UserDAO.class);

protected void initDao() {
// do nothing
}

public void save(User transientInstance) {
log.debug("saving User instance");
try {
getHibernateTemplate().save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}

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

public User findById(java.lang.Integer id) {
log.debug("getting User instance with id: " + id);
try {
User instance = (User) getHibernateTemplate()
.get("com.po.User", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}

public List findByExample(User instance) {
log.debug("finding User 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 User instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from User 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 findAll() {
log.debug("finding all User instances");
try {
String queryString = "from User";
return getHibernateTemplate().find(queryString);
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
}

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

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

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

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



Test

package com.util;

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

import com.po.User;
import com.po.UserDAO;

public class Test {

/**
* @param args
*/
public static void main(String[] args) {

ApplicationContext app = new ClassPathXmlApplicationContext("spring_ioc.xml");
UserDAO dao = (UserDAO)app.getBean("UserDAO");
User user = new User();
user.setUserName("xiaomaha");
user.setUserSex("男");
user.setUserAge(24);
dao.save(user);
}

}



我认为我配置没错`用的DAO是通过Hibernate自动生成的看了下代码用的是getHibernateTemplate 所以不需要事物`它的DAO绝对没问题

我运行的时候也不出异常!为什么数据库里没东西!!!!

我Mysql 资料表类型为InnoDB 所以支持事物!

找了半天没找到问题于是我在hibernate.cfg.xml配置文件中加了一句
<property name="connection.autocommit">true</property>

数据库里面就有信息了``但是自动编号变成了6`!!刚好使用了5次`前面的数据去哪儿了?
请帮我解决下这个问题`我不想用
<property name="connection.autocommit">true</property>


期盼高手解答我的疑问!!!
...全文
198 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
IceCraft 2008-08-07
  • 打赏
  • 举报
回复
你的Dao上边增加一层Service层。
由Service层调用Dao。
在spring中配置TransactionProxyFactoryBean,让它作为Service的代理,并给它加上事务配置。
AwL_1124 2008-08-07
  • 打赏
  • 举报
回复
以前我也出现过类似的问题
我也是手动设置提交事务
要不然没有提交不上去·
<property name="connection.autocommit">true</property>

这个倒是没有用过
学习了·
w_j_w2008 2008-08-07
  • 打赏
  • 举报
回复
自己采用事物提交方式试试,应该可以解决问题
huangyangweiyue 2008-08-07
  • 打赏
  • 举报
回复
不用自动提交,就得手动将事物提交下~~
mayuanfei 2008-08-07
  • 打赏
  • 举报
回复
是因为事务没提交 或 是回滚了的结果
原因是你集成了spring 用myeclipse生产的代码

你在hibernate.xml配置文件里加上
<property name="connection.autocommit">true </property> 是可以的

另外自己写dao 不用myeclipse生成代码更好
huangyangweiyue 2008-08-07
  • 打赏
  • 举报
回复


public void save(User transientInstance) {

try {
Session session = this.getSession();
Transcation tran = session.BeginTranscation();
session.save(transientInstance);
tran.commit();
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}



lz这样写save试试
xiaomaha 2008-08-07
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 zidasine 的回复:]
没有配置事务啊
[/Quote]
getHibernateTemplate 不是自带事物吗? 难道我以前理解错误?
我用oracle没任何问题``同样的配置`同样的没配置事物``

?????????????????????????????????????????????????????????????
zidasine 2008-08-07
  • 打赏
  • 举报
回复
没有配置事务啊
xiaomaha 2008-08-07
  • 打赏
  • 举报
回复
什么session我没用啊`??
awusoft 2008-08-07
  • 打赏
  • 举报
回复
把session中的数据清空一下.

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧