Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java EE 综合讨论区 » Hibernate  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 麻烦帮忙看一下,hibernate的save操作执行了,但是数据却没有被插入
asii





发贴: 2
积分: 0
于 2007-11-20 19:29 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
现在大部份使用都是Hibernate+spring,今天尝试了一下用纯Hibernate进行数据库操作:

public class Manager implements java.io.Serializable {

private Integer id;

private String name;

private String password;

private Integer powerId;

private Integer departmentId;

private Date createtime;

private Integer createId;

get*();
set*();//所有的geter和seter
}

public class test {
public static void main(String[] args) {

Configuration configuration = new Configuration();
configuration.configure( "/HibernateSpringTest/hibernate.cfg.xml ");
SessionFactory sessionFactory = configuration.buildSessionFactory();
Session session = sessionFactory.openSession();

// TODO Auto-generated method stub
Manager manager = new Manager();
manager.setName( "asii ");
manager.setPassword( "111111 ");
manager.setPowerId(1);
manager.setCreateId(1);
manager.setCreatetime(new Date());
manager.setDepartmentId(1);

session.save(manager);
// HibernateSessionFactory.getSession().save(manager);
// HibernateSessionFactory.closeSession();
System.out.print( "Success ");
}
}
执行结果是:
INFO [main] - Hibernate 3.1.3
INFO [main] - hibernate.properties not found
INFO [main] - using CGLIB reflection optimizer
INFO [main] - using JDK 1.4 java.sql.Timestamp handling
INFO [main] - configuring from resource: /HibernateSpringTest/hibernate.cfg.xml
INFO [main] - Configuration resource: /HibernateSpringTest/hibernate.cfg.xml
INFO [main] - Reading mappings from resource: hibernate/Manager.hbm.xml
INFO [main] - Mapping class: hibernate.Manager -> manager
INFO [main] - Configured SessionFactory: null
INFO [main] - Using Hibernate built-in connection pool (not for production use!)
INFO [main] - Hibernate connection pool size: 20
INFO [main] - autocommit mode: false
INFO [main] - using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost:3306/success
INFO [main] - connection properties: {user=root, password=****}
INFO [main] - RDBMS: MySQL, version: 5.0.18-nt
INFO [main] - JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-3.1.10 ( $Date: 2005/05/19 15:52:23 $, $Revision: 1.1.2.2 $ )
INFO [main] - Using dialect: org.hibernate.dialect.MySQLDialect
INFO [main] - Using default transaction strategy (direct JDBC transactions)
INFO [main] - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
INFO [main] - Automatic flush during beforeCompletion(): disabled
INFO [main] - Automatic session close at end of transaction: disabled
INFO [main] - JDBC batch size: 15
INFO [main] - JDBC batch updates for versioned data: disabled
INFO [main] - Scrollable result sets: enabled
INFO [main] - JDBC3 getGeneratedKeys(): enabled
INFO [main] - Connection release mode: auto
INFO [main] - Maximum outer join fetch depth: 2
INFO [main] - Default batch fetch size: 1
INFO [main] - Generate SQL with comments: disabled
INFO [main] - Order SQL updates by primary key: disabled
INFO [main] - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
INFO [main] - Using ASTQueryTranslatorFactory
INFO [main] - Query language substitutions: {}
INFO [main] - Second-level cache: enabled
INFO [main] - Query cache: disabled
INFO [main] - Cache provider: org.hibernate.cache.EhCacheProvider
INFO [main] - Optimize cache for minimal puts: disabled
INFO [main] - Structured second-level cache entries: disabled
INFO [main] - Echoing all SQL to stdout
INFO [main] - Statistics: disabled
INFO [main] - Deleted entity synthetic identifier rollback: disabled
INFO [main] - Default entity-mode: pojo
INFO [main] - building session factory
WARN [main] - No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/D:/Program%20Files/MyEclipse%205.5.1%20GA/myeclipse/eclipse/plugins/com.genuitec.org.hibernate.eclipse_5.5.0/myeclipse-data/3.0/lib/ehcache-1.1.jar!/ehcache-failsafe.xml
INFO [main] - Not binding factory to JNDI, no JNDI name configured
Hibernate: insert into success.manager (name, password, powerId, departmentId, createtime, createId) values (?, ?, ?, ?, ?, ?)
Success

如上结果,显示了
Hibernate: insert into success.manager (name, password, powerId, departmentId, createtime, createId) values (?, ?, ?, ?, ?, ?)

而且也打印出了“Success”但是到数据库里去看,就是死活没有数据,被气死。大家麻烦看看,问题是出在哪里?



作者 Re:麻烦帮忙看一下,hibernate的save操作执行了,但是数据却没有被插入 [Re:asii]
bigseal





发贴: 4
积分: 0
于 2007-11-22 12:31 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list

Session sess = factory.openSession();
Transaction tx = null;
try {
tx = sess.beginTransaction();

// do some work
...

tx.commit();
}
catch (RuntimeException e) {
if (tx != null) tx.rollback();
throw e; // or display error message
}
finally {
sess.close();
}

在事物中保存就可以了

Although the Hibernate reference documentation states that the use of transactions is optional, I found that (at least through Version 2.1.1, with the HSQLDB database) any changes I made outside the context of a Hibernate transaction would disappear when the application ended. Analyzing the SQL emitted by Hibernate revealed that even though I didn't request a transaction, auto-commit was being turned off, so my changes were getting rolled back when the application ended. So for now it's always necessary to use explicit transactions in your code, a good habit in any case.

摘自 《OReilly_-_Hibernate_A_Developers_Notebook_2004》


bigseal edited on 2007-11-22 12:34

作者 Re:麻烦帮忙看一下,hibernate的save操作执行了,但是数据却没有被插入 [Re:asii]
shishishi





发贴: 1
积分: 0
于 2007-12-03 14:34 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
没有提交呗!


作者 Re:麻烦帮忙看一下,hibernate的save操作执行了,但是数据却没有被插入 [Re:asii]
xuan198451





发贴: 1
积分: 0
于 2008-10-03 20:50 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
刚开始没有启动事务

事务未提交



作者 Re:麻烦帮忙看一下,hibernate的save操作执行了,但是数据却没有被插入 [Re:asii]
nihaolaogao





发贴: 8
积分: 0
于 2008-11-18 19:39 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
Configuration configuration = new Configuration();
这个默认读的是hiberante.properties.
想要读/hibernate.cfg.xml 它.
应该这样写
Configuration configuration = new Configuration().config();




flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923