[英]Hibernate query executes but nothing is added
I'm learning hibernate and when I run the following program, I get this message : Hibernate: insert into contact (firstname, lastname, email, id) values (?, ?, ?, ?)
but when I check the table, nothing seems to be inserted. 我正在学习休眠,当我运行以下程序时,我收到以下消息:
Hibernate: insert into contact (firstname, lastname, email, id) values (?, ?, ?, ?)
但是当我检查表时,什么也没有似乎已插入。 What's the problem here ? 这是什么问题? Clearly the statement value field is null but why?
显然,语句值字段为null,但为什么呢?
I have the following POJO: 我有以下POJO:
public class Contact {
private String firstName;
private String lastName;
private String email;
private long id;
/**
* @return Email
*/
public String getEmail() {
return email;
}
/**
* @return First Name
*/
public String getFirstName() {
return firstName;
}
/**
* @return Last name
*/
public String getLastName() {
return lastName;
}
/**
* @param string Sets the Email
*/
public void setEmail(String string) {
email = string;
}
/**
* @param string Sets the First Name
*/
public void setFirstName(String string) {
firstName = string;
}
/**
* @param string sets the Last Name
*/
public void setLastName(String string) {
lastName = string;
}
/**
* @return ID Returns ID
*/
public long getId() {
return id;
}
/**
* @param l Sets the ID
*/
public void setId(long l) {
id = l;
}
}
and below are my hibernate config and hbm files: 下面是我的休眠配置和hbm文件:
<?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/aneesh</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123</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="contact.hbm.xml"/>
</session-factory>
</hibernate-configuration>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Contact" table="contact">
<id name="id" column="id" >
<generator class="assigned"/>
</id>
<property name="firstName" >
<column name="firstname" />
</property>
<property name="lastName">
<column name="lastname"/>
</property>
<property name="email" >
<column name="email"/>
</property>
</class>
</hibernate-mapping>
and here is my code to insert : 这是我要插入的代码:
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.Session;
public class HSession {
public static void main(String[] args) {
Session session = null;
try{
SessionFactory sessionGet = new Configuration().configure().buildSessionFactory();
session = sessionGet.openSession();
Contact contact = new Contact();
contact.setFirstName("xyz");
contact.setLastName("zyx");
contact.setEmail("x@xmail.com");
contact.setId(20);
session.save(contact);
}finally{
// Actual contact insertion will happen at this step
try {
session.flush();
session.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Before you are saving the object you have to get the transaction and after saving the object you have to commit the transaction. 在保存对象之前,必须先获取事务,在保存对象之后,必须提交事务。
Transaction tx = session.beginTransaction();
session.save(contact);
tx.commit();
Use transaction
in save. 在保存中使用
transaction
。
session.getTransaction().begin();
session.save(contact);
session.getTransaction().commit();
You forgot the begin and commit the transaction. 您忘记了开始并提交事务。
session = sessionGet.openSession();
**Transaction tx= session.beginTransaction();**
Contact contact = new Contact();
contact.setFirstName("xyz");
contact.setLastName("zyx");
contact.setEmail("x@xmail.com");
contact.setId(20);
session.save(contact);
**tx.commit();**
you should do a commit for the insert to be reflected in database try 您应该进行一次提交才能使插入内容反映在数据库try中
session.getTransaction().commit();
after your 在你之后
session.save(contact);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.