浏览 1833 次
|
锁定老贴子 主题:『出错』能查找却不能插入的怪问题
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
|---|---|
| 作者 | 正文 |
|
最后更新时间:2005-07-04
软件环境:
hibernate2.1.8 配置文件: Teller <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"> <hibernate-mapping> <class name="com.lm.jlogistics.domain.Teller" table="teller"> <id name="telkey" type="java.lang.String" column="telkey"> <generator class="uuid.hex"/> </id> <property name="telname" type="java.lang.String" column="telname"/> <property name="telpasswd" type="java.lang.String" column="telpasswd"/> <property name="telclass" type="java.lang.String" column="telclass"/> <property name="status" type="java.lang.String" column="status"/> </class> </hibernate-mapping> hibernate.cfg.xml <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd"> <hibernate-configuration> <session-factory> <property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.username">root</property> <property name="connection.password">root</property> <property name="connection.url">jdbc:mysql://localhost:3306/jlogistics</property> <property name="connection.pool.size">1</property> <property name="statement_cache.size">25</property> <property name="jdbc.fetch_size">50</property> <property name="jdbc.batch_size">30</property> <property name="show_sql">true</property> <!-- Mapping files --> <mapping resource="com/lm/jlogistics/domain/Teller.hbm.xml"/> </session-factory> </hibernate-configuration> 错误提示信息: 测试类 public class HibernateTest { public static void main(String[] args){ Teller t=new Teller(); t.setStatus("0"); t.setTelname("11"); t.setTelpasswd("22"); t.setTelclass("1"); try{ Configuration config=new Configuration().configure(); SessionFactory sf =config.buildSessionFactory(); Session s=sf.openSession(); s.save(t); Teller t2=(Teller)s.find("from Teller").get(0); System.out.println(t2.getTelname()); s.flush(); s.close(); sf.close(); }catch(HibernateException he){ System.out.println(he.toString()); } } } Hibernate: insert into teller (telname, telpasswd, telclass, status, telkey) values (?, ?, ?, ?, ?) Hibernate: select teller0_.telkey as telkey, teller0_.telname as telname, teller0_.telpasswd as telpasswd, teller0_.telclass as telclass, teller0_.status as status from teller teller0_ 2 但是teller表中却没有save的记录,没有任何异常抛出 你的分析: 不解 很不解,想了一下午了。 声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
|
| 返回顶楼 | |
|
最后更新时间:2005-07-04
第一个save(t)之后,应该使用s.flush()提交
或者使用事务 Transaction tx=s.beginTransaction(); ... s.save(t); tx.commit(); |
|
| 返回顶楼 | |
|
最后更新时间:2005-07-04
[code:1]Configuration config=new Configuration().configure();
SessionFactory sf =config.buildSessionFactory(); Session s=sf.openSession(); s.save(t); //Teller t2=(Teller)s.find("from Teller").get(0); //System.out.println(t2.getTelname()); s.flush(); s.close(); sf.close();[/code:1] 这样还是不能插入 但是这样[code:1]Configuration config=new Configuration().configure(); SessionFactory sf =config.buildSessionFactory(); Session s=sf.openSession(); Transaction tx=s.beginTransaction(); s.save(t); //Teller t2=(Teller)s.find("from Teller").get(0); //System.out.println(t2.getTelname()); s.flush(); tx.commit(); s.close(); sf.close();[/code:1] 就可以了 不明白为什么在这里 不使用事务 就不能插入呢?我记得只要flush就可以的啊,不解。 |
|
| 返回顶楼 | |
|
最后更新时间:2005-07-06
楼主试试要是不用flush可不可以?
我记得session的save方法会自动清理缓存的,那样就可以不用手动flush了。 |
|
| 返回顶楼 | |
|
最后更新时间:2005-07-18
session.save(Object) 是用户持久化临时对象的。
你的对象是临时对象的话,如果要保存,还需要借助 Transaction 类的 commit() 方法。 因为HIBERNATE 的SESSIONFACTORY 把CONNECTION 的 autoCommit 设置为 FALSE。 所以,你需要调用 commit() 提交你的事务。 |
|
| 返回顶楼 | |




