0

我正在使用 Primefaces 3.5 + 休眠 4.2.0

我正在使用 primefaces 中的单元格编辑表,并希望在单击一个字段并更改表值时更新我的​​数据库中的产品表。但是,我只找到了 hibernate 的简单属性的更新方法,如下所示:

EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();

String jpqlUpdate = "update Customer set name = :newName where name = :oldName"
int updatedEntities = entityManager.createQuery( jpqlUpdate )
                            .setParameter( "newName", newName )
                            .setParameter( "oldName", oldName )
                            .executeUpdate();
entityManager.getTransaction().commit();
entityManager.close();

如何在休眠状态下更新整个对象?

4

1 回答 1

0

你可以试试这个:

数据访问类:

public boolean update(YourClass yourObject) {
    Transaction transaction = null;
    boolean result = false;
    try {
        SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.getCurrentSession();
        transaction = session.beginTransaction();
        session.update(yourObject);
        transaction.commit();

        result = true;

    } catch (Exception ex) {
        ex.printStackTrace();
        if (transaction != null) {
            transaction.rollback();
        }
    }
    return result;
}
 public boolean update2(YourClass yourObject)  {
    Transaction transaction = null;
    int result = -1;
    try {
        String sqlQuery = "UPDATE YourTable SET yourColumn1=" + yourObject.value1
                + ", yourColumn2='" + yourObject.value2 + "' WHERE [some condition]=";
        SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.getCurrentSession();
        transaction = session.beginTransaction();
        SQLQuery query = session.createSQLQuery(sqlQuery);
        result = query.executeUpdate();
        transaction.commit();

    } catch (Exception ex) {
        ex.printStackTrace();
        if (transaction != null) {
            transaction.rollback();
        }
    }
    return result;
}

休眠.cfg.xml

<hibernate-configuration>
<session-factory name="session">
    <property name="hibernate.connection.driver_class">[db_driver]</property>
    <property name="hibernate.connection.url">jdbc:[db_type]://[db_ip]:[db_port]/YourDatabase</property>
    <property name="hibernate.connection.username">username</property>
    <property name="hibernate.connection.password">password</property>
    <property name="hibernate.dialect">[db_dialect]</property>

    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.format_sql">false</property>
    <property name="hibernate.current_session_context_class">thread</property>

    <mapping class="dataaccess.entity.YourClass"/>

</session-factory>
</hibernate-configuration>  

HibernateUtil.class

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

public class HibernateUtil {

private static final SessionFactory sessionFactory;

static {
    try {
        sessionFactory = new AnnotationConfiguration().configure("/hibernate.cfg.xml").buildSessionFactory();
    } catch (Throwable ex) {
        System.err.println("Initial SessionFactory creation failed." + ex);
        throw new ExceptionInInitializerError(ex);
    }
}

public static SessionFactory getSessionFactory() {
    return sessionFactory;
}
}
于 2013-04-20T15:57:47.813 回答