无法在hibernate中更新数据库
我是hibernate新手,我正在使用hibernate 3.0和MySQL,我想在数据库中进行简单的数据库操作,如插入更新和删除。我获得了插入和删除的成功结果,但无法更新特定字段。 我有一个名为Employee的持久类,它具有Fname、Lname、Id和带有getter和setter的mail 这是我的主要方法:-无法在hibernate中更新数据库,hibernate,Hibernate,我是hibernate新手,我正在使用hibernate 3.0和MySQL,我想在数据库中进行简单的数据库操作,如插入更新和删除。我获得了插入和删除的成功结果,但无法更新特定字段。 我有一个名为Employee的持久类,它具有Fname、Lname、Id和带有getter和setter的mail 这是我的主要方法:- public static void main(String[] args) { Session session = null; Random r = new R
public static void main(String[] args) {
Session session = null;
Random r = new Random();
try {
/*
* This step will read hibernate.cfg.xml
*
* and prepare hibernate for use
*/
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session = sessionFactory.openSession();
Transaction trx = session.beginTransaction();
Contact contact = new Contact();
contact = (Contact) session.get(Contact.class, new Long(1));
// Create new instance of Contact and set
// values in it by reading them from form object
System.out.println("Inserting Record");
contact.setId(r.nextLong() % 100);
contact.setFirstName("123anand");
contact.setLastName("nandurbarkar");
contact.setEmail("anand_it1990@yahoo.com");
trx.commit();
session.update(contact);
这是我的conf文件:-
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/hibernatetutorial</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.pool_size">1</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>
com.mysql.jdbc.Driver
jdbc:mysql://localhost/hibernatetutorial
根
根
1.
真的
org.hibernate.dialogue.mysqldialogue
更新
如果您知道如何解决此问题,请提供帮助。
提前感谢…我认为您需要在提交会话之前进行更新 因此,交换最后两行应该可以让它工作:-
session.update(contact);
trx.commit();
不能更改实体的id。如果要查看其他属性的更新工作方式,请尝试以下操作:
public static void main(String[] args) {
Session session = null;
Random r = new Random();
try {
/*
* This step will read hibernate.cfg.xml
*
* and prepare hibernate for use
*/
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session = sessionFactory.openSession();
Transaction trx = session.beginTransaction();
Contact contact = new Contact();
// Create new instance of Contact and set
// values in it by reading them from form object
System.out.println("Inserting Record");
contact.setId(r.nextLong() % 100);
contact.setFirstName("123anand");
contact.setLastName("nandurbarkar");
contact.setEmail("anand_it1990@yahoo.com");
// this makes contact persistent. No SQL commands yet
session.persist(contact);
// sync the session. Here you get a SQL INSERT
session.flush();
// change some properties
contact.setFirstName("ugo");
// sync again.Here you get a SQL UPDATE
session.flush()
// Now, let's say you want to update an existing instance/record
// with id = 123
// retrieve the instance
Contact c = session.get(Contact.class, new Long(123));
c.setFirstName("new name");
// sync the session with the db
session.flush(); // <- here you get SQL UPDATE
trx.commit();
publicstaticvoidmain(字符串[]args){
会话=空;
随机r=新随机();
试一试{
/*
*此步骤将读取hibernate.cfg.xml
*
*并准备hibernate以供使用
*/
SessionFactory SessionFactory=新配置().configure().buildSessionFactory();
session=sessionFactory.openSession();
事务trx=session.beginTransaction();
触点=新触点();
//创建联系人的新实例并设置
//通过从form对象中读取值来获取其中的值
System.out.println(“插入记录”);
contact.setId(r.nextLong()%100);
联系人:setFirstName(“123anand”);
联系人:setLastName(“nandurbarkar”);
contact.setEmail(“anand_it1990@yahoo.com");
//这会使联系人持久化。还没有SQL命令
会话。持续(联系);
//同步会话。这里有一个SQL插入
session.flush();
//更改某些属性
联系人:setFirstName(“ugo”);
//再次同步。这里有一个SQL更新
session.flush()
//现在,假设您要更新现有实例/记录
//id=123
//检索实例
Contact c=session.get(Contact.class,new Long(123));
c、 setFirstName(“新名称”);
//将会话与数据库同步
session.flush();//你同意这一点。但是如果我想在某个特定记录上更新怎么办?假设Id=1,那么联系人名称应该更改为“abc”。那么我如何实现这一点呢?为此,我不应该使用:-contact=(contact)session.get(contact.class,new Long(1));是的,您应该使用session.get()。为了举例说明这一点,我在代码中添加了几行代码。其他选项是使用HQL查询检索实例。这是Hibernate的基本用法,您将在优秀的参考文档中找到更多的用例和示例。