2013-04-20 4 views
0

Я использую Primefaces 3.5 + зимует 4.2.0Обновления объект в спящем режиме

Я использую cell editing table в primefaces и хочу обновить таблицу продукта в моей БД, когда я нажимаю на одном поля и изменить значение таблицы , Тем не менее, я нашел только способ обновления для простых атрибутов для спящего режима, как это:

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(); 

Как обновить весь объект, в спящем режиме?

ответ

0

Вы можете попробовать это:

DataaccessClass:

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; 
} 

hibernate.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; 
} 
} 
+0

Вы можете увидеть это для [Hibernate Annotations] (http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/) – navand

+0

есть также возможный способ использования простого 'обновления 'заявление? – maximus

+1

вы хотите что-то вроде этого: транзакция транзакции = null; Строка sqlQuery = "UPDATE YourTable SET yourColumn1 =" + yourValue1 + ", yourColumn2 = '" + yourValue2 + "' WHERE someCondition"; SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); Session session = sessionFactory.getCurrentSession(); transaction = session.beginTransaction(); SQLQuery query = session.createSQLQuery (sqlQuery); result = query.executeUpdate(); transaction.commit(); – navand

Смежные вопросы