Java 没有使用休眠插入数据,但没有给出错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4431059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Data not inserted using hibernate, but gives no error?
提问by Kris
I'm trying to get started with Hibernate, but can't insert data, for some reason. It seems to be working properly as no error is given, but when I check the database the table is empty. I don't think it the connection to the database itself that fail, because when I change the table name to a non-excisting one in the mapping xml, Hibernate creates this on the fly (But as I said, no data is inserted). Does anyone know what the problem could be?
我正在尝试开始使用 Hibernate,但由于某种原因无法插入数据。它似乎工作正常,因为没有给出错误,但是当我检查数据库时,表是空的。我不认为它与数据库本身的连接失败了,因为当我在映射 xml 中将表名更改为不存在的表名时,Hibernate 会动态创建它(但正如我所说,没有插入数据) . 有谁知道是什么问题?
Here's my code:
这是我的代码:
hibernate.cfg.xml:
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-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/intex</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- Mapping files -->
<mapping resource="intex.hbm.xml"/>
</session-factory>
</hibernate-configuration>
My mapping xml:
我的映射 xml:
<hibernate-mapping>
<class name="com.intex.uni.courses.Course" table="course">
<id name="id" column="id" >
<generator class="increment"/>
</id>
<property name="name" column="name" />
<property name="description" column="description" />
<property name="url" column="url" />
<property name="code" column="code" />
</class>
</hibernate-mapping>
And my test client:
我的测试客户端:
public class HibernateTest {
public static void main(String[] args) {
Session session = null;
try {
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session = sessionFactory.openSession();
Course course = new Course();
course.setDescription("Description");
course.setName("NAME");
course.setUrl("http://www.url.com");
session.save(course);
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
session.flush();
session.close();
}
}
}
I really hope someone will be able to help me! Thanks in advance :)
我真的希望有人能够帮助我!提前致谢 :)
采纳答案by Venkatesan Elumalai
use this code and test in main class.
使用此代码并在主类中进行测试。
Session session = null;
Transaction txn = null;
try {
SessionFactory sessionFactory =
new Configuration().configure().buildSessionFactory();
session = sessionFactory.openSession();
txn = session.beginTransaction();
Course course = new Course();
course.setDescription("Description");
course.setName("NAME");
course.setUrl("http://www.url.com");
session.save(course);
txn.commit();
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
if (!txn.wasCommitted()) {
txn.rollback();
}
session.flush();
session.close();
}
回答by user151019
I suspect that the system is not committing the transaction that includes the needed insert. I always set transactions explicitly e.g. Hibernate Getting Started Tutorial Example 2.5
我怀疑系统没有提交包含所需插入的事务。我总是显式设置事务,例如Hibernate Getting Started Tutorial Example 2.5
An alternative is that you should be able to set Hibernate's commit mode in you code so that transactions are implicit - look at FlushMode
另一种方法是您应该能够在代码中设置 Hibernate 的提交模式,以便事务是隐式的 - 看看FlushMode
回答by Saravanakumar
Try the following code:
试试下面的代码:
public class HibernateTest {
public static void main(String[] args) {
Session session = null;
Transaction txn = null;
try {
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session = sessionFactory.openSession();
txn = session.beginTransaction();
Course course = new Course();
course.setDescription("Description");
course.setName("NAME");
course.setUrl("http://www.url.com");
session.save(course);
txn.commit();
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
session.flush();
session.close();
}
}
}
回答by pawan
Please start transaction before save and commit transaction after save.
请在保存前启动事务并在保存后提交事务。
回答by PURNIMA KUMARI
Check mapping file if any generator is defined. comment the generator and check. This solution worked for me.
如果定义了任何生成器,请检查映射文件。注释生成器并检查。这个解决方案对我有用。