Ací es mostren les diferències entre la revisió seleccionada i la versió actual de la pàgina.
| Ambdós costats versió prèvia Revisió prèvia Següent revisió | Revisió prèvia | ||
|
hibernate [2022/11/24 07:39] albert_palacios_jimenez [Configuració de Hibernate, arxiu "hibernate.cfg.xml"] |
hibernate [2022/11/24 08:24] (actual) albert_palacios_jimenez [Configuració de Hibernate, arxiu "hibernate.properties"] |
||
|---|---|---|---|
| Línia 30: | Línia 30: | ||
| - URI de la base de dades | - URI de la base de dades | ||
| - | La configuració de " | + | == La configuració de " |
| (Cal canviar DB_USER i DB_PASSWORD pels valors corresponents a la connexió ...) | (Cal canviar DB_USER i DB_PASSWORD pels valors corresponents a la connexió ...) | ||
| Línia 44: | Línia 44: | ||
| </ | </ | ||
| - | La configuració de " | + | == La configuració de " |
| (Amb SQLite s’especifica l’arxiu a utilitzar, en aquest cas " | (Amb SQLite s’especifica l’arxiu a utilitzar, en aquest cas " | ||
| Línia 146: | Línia 146: | ||
| - Delete | - Delete | ||
| + | Exemple d' | ||
| + | |||
| + | <code java> | ||
| + | import java.io.Serializable; | ||
| + | import java.util.Collection; | ||
| + | import java.util.List; | ||
| + | import java.util.Set; | ||
| + | |||
| + | import org.hibernate.HibernateException; | ||
| + | import org.hibernate.SQLQuery; | ||
| + | import org.hibernate.Session; | ||
| + | import org.hibernate.Transaction; | ||
| + | import org.hibernate.boot.registry.StandardServiceRegistry; | ||
| + | import org.hibernate.boot.registry.StandardServiceRegistryBuilder; | ||
| + | import org.hibernate.SessionFactory; | ||
| + | import org.hibernate.cfg.Configuration; | ||
| + | |||
| + | public class Manager { | ||
| + | |||
| + | private static SessionFactory factory; | ||
| + | | ||
| + | public static void createSessionFactory() { | ||
| + | |||
| + | try { | ||
| + | Configuration configuration = new Configuration(); | ||
| + | configuration.configure(); | ||
| + | StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings( | ||
| + | configuration.getProperties()).build(); | ||
| + | factory = configuration.buildSessionFactory(serviceRegistry); | ||
| + | } catch (Throwable ex) { | ||
| + | System.err.println(" | ||
| + | throw new ExceptionInInitializerError(ex); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | public static void close () { | ||
| + | factory.close(); | ||
| + | } | ||
| + | | ||
| + | public static Employee addEmployee(String firstName, String lastName, int salary){ | ||
| + | Session session = factory.openSession(); | ||
| + | Transaction tx = null; | ||
| + | Employee result = null; | ||
| + | try { | ||
| + | tx = session.beginTransaction(); | ||
| + | result = new Employee(firstName, | ||
| + | session.save(result); | ||
| + | tx.commit(); | ||
| + | } catch (HibernateException e) { | ||
| + | if (tx!=null) tx.rollback(); | ||
| + | e.printStackTrace(); | ||
| + | result = null; | ||
| + | } finally { | ||
| + | session.close(); | ||
| + | } | ||
| + | return result; | ||
| + | } | ||
| + | |||
| + | public static Contact addContact(String lname, String lmail){ | ||
| + | Session session = factory.openSession(); | ||
| + | Transaction tx = null; | ||
| + | Contact result = null; | ||
| + | try { | ||
| + | tx = session.beginTransaction(); | ||
| + | result = new Contact(lname, | ||
| + | session.save(result); | ||
| + | tx.commit(); | ||
| + | } catch (HibernateException e) { | ||
| + | if (tx!=null) tx.rollback(); | ||
| + | e.printStackTrace(); | ||
| + | result = null; | ||
| + | } finally { | ||
| + | session.close(); | ||
| + | } | ||
| + | return result; | ||
| + | } | ||
| + | |||
| + | public static <T> T getById(Class<? | ||
| + | Session session = factory.openSession(); | ||
| + | Transaction tx = null; | ||
| + | T obj = null; | ||
| + | try { | ||
| + | tx = session.beginTransaction(); | ||
| + | obj = clazz.cast(session.get(clazz, | ||
| + | tx.commit(); | ||
| + | } catch (HibernateException e) { | ||
| + | if (tx!=null) tx.rollback(); | ||
| + | e.printStackTrace(); | ||
| + | } finally { | ||
| + | session.close(); | ||
| + | } | ||
| + | return obj; | ||
| + | } | ||
| + | | ||
| + | public static void updateContact(long contactId, String name, String email, Set< | ||
| + | Session session = factory.openSession(); | ||
| + | Transaction tx = null; | ||
| + | try { | ||
| + | tx = session.beginTransaction(); | ||
| + | Contact obj = (Contact) session.get(Contact.class, | ||
| + | obj.setName(name); | ||
| + | obj.setEmail(email); | ||
| + | obj.setEmployees(employees); | ||
| + | session.update(obj); | ||
| + | tx.commit(); | ||
| + | } catch (HibernateException e) { | ||
| + | if (tx!=null) tx.rollback(); | ||
| + | e.printStackTrace(); | ||
| + | } finally { | ||
| + | session.close(); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | public static void updateEmployee(long employeeId, String firstName, String lastName, int salary){ | ||
| + | Session session = factory.openSession(); | ||
| + | Transaction tx = null; | ||
| + | try { | ||
| + | tx = session.beginTransaction(); | ||
| + | Employee obj = (Employee) session.get(Employee.class, | ||
| + | obj.setFirstName(firstName); | ||
| + | obj.setLastName(lastName); | ||
| + | obj.setSalary(salary); | ||
| + | session.update(obj); | ||
| + | tx.commit(); | ||
| + | } catch (HibernateException e) { | ||
| + | if (tx!=null) tx.rollback(); | ||
| + | e.printStackTrace(); | ||
| + | } finally { | ||
| + | session.close(); | ||
| + | } | ||
| + | } | ||
| + | | ||
| + | public static <T> void delete(Class<? | ||
| + | Session session = factory.openSession(); | ||
| + | Transaction tx = null; | ||
| + | try { | ||
| + | tx = session.beginTransaction(); | ||
| + | T obj = clazz.cast(session.get(clazz, | ||
| + | session.delete(obj); | ||
| + | tx.commit(); | ||
| + | } catch (HibernateException e) { | ||
| + | if (tx!=null) tx.rollback(); | ||
| + | e.printStackTrace(); | ||
| + | } finally { | ||
| + | session.close(); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | public static <T> Collection<?> | ||
| + | return listCollection(clazz, | ||
| + | } | ||
| + | |||
| + | public static <T> Collection<?> | ||
| + | Session session = factory.openSession(); | ||
| + | Transaction tx = null; | ||
| + | Collection<?> | ||
| + | try { | ||
| + | tx = session.beginTransaction(); | ||
| + | if (where.length() == 0) { | ||
| + | result = session.createQuery(" | ||
| + | } else { | ||
| + | result = session.createQuery(" | ||
| + | } | ||
| + | tx.commit(); | ||
| + | } catch (HibernateException e) { | ||
| + | if (tx!=null) tx.rollback(); | ||
| + | e.printStackTrace(); | ||
| + | } finally { | ||
| + | session.close(); | ||
| + | } | ||
| + | return result; | ||
| + | } | ||
| + | |||
| + | public static <T> String collectionToString(Class<? | ||
| + | String txt = ""; | ||
| + | for(Object obj: collection) { | ||
| + | T cObj = clazz.cast(obj); | ||
| + | txt += " | ||
| + | } | ||
| + | if (txt.substring(0, | ||
| + | txt = txt.substring(1); | ||
| + | } | ||
| + | return txt; | ||
| + | } | ||
| + | |||
| + | public static void queryUpdate (String queryString) { | ||
| + | Session session = factory.openSession(); | ||
| + | Transaction tx = null; | ||
| + | try { | ||
| + | tx = session.beginTransaction(); | ||
| + | SQLQuery query = session.createSQLQuery(queryString); | ||
| + | query.executeUpdate(); | ||
| + | tx.commit(); | ||
| + | } catch (HibernateException e) { | ||
| + | if (tx!=null) tx.rollback(); | ||
| + | e.printStackTrace(); | ||
| + | } finally { | ||
| + | session.close(); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | public static List< | ||
| + | Session session = factory.openSession(); | ||
| + | Transaction tx = null; | ||
| + | List< | ||
| + | try { | ||
| + | tx = session.beginTransaction(); | ||
| + | SQLQuery query = session.createSQLQuery(queryString); | ||
| + | @SuppressWarnings(" | ||
| + | List< | ||
| + | result = rows; | ||
| + | tx.commit(); | ||
| + | } catch (HibernateException e) { | ||
| + | if (tx!=null) tx.rollback(); | ||
| + | e.printStackTrace(); | ||
| + | } finally { | ||
| + | session.close(); | ||
| + | } | ||
| + | return result; | ||
| + | } | ||
| + | |||
| + | public static String tableToString (List< | ||
| + | String txt = ""; | ||
| + | for (Object[] row : rows) { | ||
| + | for (Object cell : row) { | ||
| + | txt += cell.toString() + ", "; | ||
| + | } | ||
| + | if (txt.length() >= 2 && txt.substring(txt.length() - 2).compareTo(", | ||
| + | txt = txt.substring(0, | ||
| + | } | ||
| + | txt += " | ||
| + | } | ||
| + | if (txt.length() >= 2) { | ||
| + | txt = txt.substring(0, | ||
| + | } | ||
| + | return txt; | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| ==== Hibernate, transactions ==== | ==== Hibernate, transactions ==== | ||
| Línia 420: | Línia 659: | ||
| } | } | ||
| </ | </ | ||
| + | |||
| + | ==== Tipus de relacions N:N ManyToMany ==== | ||
| + | |||
| + | Són aquelles que creen una relació entre múltiples elements de múltiples d’altres taules. Necessiten una taula extra per mantenir la relació. Per exemple: | ||
| + | |||
| + | - Una supermercat té N treballadors i alguns d' | ||
| + | |||
| + | - Múltiples comandes es relacionen amb múltiples clients | ||
| ==== Exemple N:N ManyToMany amb " | ==== Exemple N:N ManyToMany amb " | ||