我是hibernate的新手,我正在使用带有MySQL的hibernate 3.0,我想使用数据库进行简单的数据库操作,如插入更新和删除。我获得了插入和删除的成功结果,但无法更新特殊领域。 我有一个名为Employee的持久化类,其中包含Fname,Lname,Id和带有getter和setter的邮件。
这是我的主要方法: -
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>
如果您知道如何解决此问题,请提供帮助。 在此先感谢...
答案 0 :(得分:2)
您无法更改实体的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();
答案 1 :(得分:0)
我认为您需要在提交会话之前进行更新
所以交换你的最后两行应该可以使它工作: -
session.update(contact);
trx.commit();