Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Illegal attempt to associate a collection with two open sessions
- public class Hibernate
- {
- protected static final SessionFactory sessionFactory;
- private Session session;
- static
- {
- try
- {
- // Create the SessionFactory from hibernate.cfg.xml
- sessionFactory = new Configuration().configure().buildSessionFactory();
- session
- }
- catch (Throwable ex)
- {
- // Make sure you log the exception, as it might be swallowed
- System.err.println("Initial SessionFactory creation failed." + ex);
- throw new ExceptionInInitializerError(ex);
- }
- }
- public void create(Object obj)
- {
- this.session = sessionFactory.openSession();
- session.getTransaction().begin();
- session.save(obj);
- session.getTransaction().commit();
- session.close();
- }
- public void refresh(Object obj)
- {
- this.session = sessionFactory.openSession();
- session.getTransaction().begin();
- session.refresh(obj);
- session.getTransaction().commit();
- session.close();
- }
- public void update(Object obj)
- {
- this.session = sessionFactory.openSession();
- session.getTransaction().begin();
- session.saveOrUpdate(obj);
- session.getTransaction().commit();
- session.close();
- }
- public void delete(Object obj)
- {
- this.session = sessionFactory.openSession();
- session.getTransaction().begin();
- session.delete(obj);
- session.flush();
- session.getTransaction().commit();
- session.close();
- }
- protected String protectString(String toProtect)
- {
- return (toProtect.replace("'", "''"));
- }
- }
- public class DAOPerson extends Hibernate
- {
- public void remove(Person p)
- {
- if (p instanceof Student)
- {
- Student s = (Student)p;
- Set<persistenceClass.Class> set = s.getClasses();
- Iterator<persistenceClass.Class> it = set.iterator();
- while (it.hasNext())
- {
- persistenceClass.Class r = it.next();
- r.getStudents().remove(s);
- }
- p.getBirthCountry();
- p.getCountry();
- this.delete(p);
- }
- else
- this.delete(p);
- }
- <class name="persistenceClass.Person" table="T_PERSON">
- <id name="Id" column="PERSON_ID">
- <generator class="native" />
- </id>
- <property name="FirstName" column="PERSON_FIRST_NAME" not-null="true" />
- <property name="LastName" column="PERSON_LAST_NAME" not-null="true" />
- <property name="Type" column="PERSON_TYPE" not-null="true" />
- <property name="BirthDate" column="PERSON_BIRTH_DATE" />
- <property name="BirthCity" column="PERSON_BIRTH_CITY" />
- <property name="PhoneNumber" column="PERSON_PHONE_NUMBER" />
- <property name="MobileNumber" column="PERSON_MOBILE_NUMBER" />
- <property name="Mail" column="PERSON_MAIL" />
- <property name="Address" column="PERSON_ADDRESS_ADDRESS" />
- <property name="ZipCode" column="PERSON_ADDRESS_ZIPCODE" />
- <property name="City" column="PERSON_ADDRESS_CITY" />
- <property name="Image" column="PERSON_IMAGE" type="image" />
- <many-to-one name="Country" column="PERSON_ADDRESS_COUNTRY" class="persistenceClass.Country" />
- <many-to-one name="BirthCountry" column="PERSON_BIRTH_COUNTRY" class="persistenceClass.Country" />
- <many-to-one name="Civility" column="PERSON_CIVILITY" class="persistenceClass.Civility" />
- <many-to-one name="Sex" column="PERSON_SEX" class="persistenceClass.Sex" />
- <joined-subclass name="persistenceClass.Student" table="T_STUDENT">
- <key column="PERSON_ID" />
- <set name="Classes" table="T_CLASS_STUDENT" inverse="true" >
- <key column="PERSON_ID" />
- <many-to-many class="persistenceClass.Class" column="CLASS_ID" />
- </set>
- </joined-subclass>
- <joined-subclass name="persistenceClass.Teacher" table="T_TEACHER">
- <key column="PERSON_ID" />
- </joined-subclass>
- </class>
- <hibernate-configuration>
- <session-factory>
- <!-- Database connection settings -->
- <property name="connection.driver_class">org.gjt.mm.mysql.Driver</property>
- <property name="connection.url">jdbc:mysql://localhost/projet</property>
- <property name="connection.username">root</property>
- <property name="connection.password"></property>
- <!-- JDBC connection pool (use the built-in) -->
- <property name="connection.pool_size">10</property>
- <!-- SQL dialect -->
- <property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
- <!-- Drop and re-create the database schema on start-up, also try with “update” to keep the previous values -->
- <property name="hbm2ddl.auto">update</property>
- <!-- Echo all executed SQL to stdout -->
- <property name="show_sql">true</property>
- <mapping resource="persistenceConfigurations/Person.hbm.xml"/>
- <mapping resource="persistenceConfigurations/Country.hbm.xml"/>
- <mapping resource="persistenceConfigurations/Civility.hbm.xml"/>
- <mapping resource="persistenceConfigurations/Sex.hbm.xml"/>
- <mapping resource="persistenceConfigurations/Formation.hbm.xml"/>
- <mapping resource="persistenceConfigurations/Year.hbm.xml"/>
- <mapping resource="persistenceConfigurations/Class.hbm.xml"/>
- <mapping resource="persistenceConfigurations/Subject.hbm.xml"/>
- <mapping resource="persistenceConfigurations/Room.hbm.xml"/>
- <mapping resource="persistenceConfigurations/Lesson.hbm.xml"/>
- </session-factory>
- </hibernate-configuration>
- public void remove(Person p)
- {
- if (p instanceof Student)
- {
- Student s = (Student)p;
- Set<persistenceClass.Class> set = s.getClasses();
- Iterator<persistenceClass.Class> it = set.iterator();
- while (it.hasNext())
- {
- persistenceClass.Class r = it.next();
- r.getStudents().remove(s);
- //now effectively removing the student from the class.
- //this will update the class and its persistent set of students.
- this.update(r);
- }
- }
- this.delete(p); //now remove the student (or whatever comes as argument)
- }
- //Example
- public void create(Object obj){
- this.session = sessionFactory.openSession();
- try{
- session.getTransaction().begin();
- session.save(obj);
- session.getTransaction().commit();
- } catch (Exception e) {
- e.printStackTrace();
- session.getTransaction().rollback();
- } finally {
- session.flush();
- session.close();
- //keep in mind that too many flushes might cause db memory shortage.
- //So if you are treating list of objects and so on iterate and save them flushing
- //only when your batch size is reached.
- }
- }
- @ManyToOne(cascade = CascadeType.ALL)
- @JoinColumn(name = “user_id”)
- public User getAuthor() {
- return author;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement