-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Problem with My First Hibernate Application
PostPosted: Mon Feb 06, 2006 11:45 am 
Newbie

Joined: Mon Feb 06, 2006 10:22 am
Posts: 3
Location: Ireland
I made the following from the hibernate reference doc. while there were no exceptions, i expected that when i check the Tables created, i will find 4 tables from calling show tables from MySQL but only PERSON table and EVENT tables were created. I expected PERSON_EVENT and PERSON_EMAIL_ADDR. So why are my having two tables?

Hibernate Servlet:
Code:
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        addEvent("Swimming",new Date());
        addPerson("Jow", "Babe" , "24");
       
       addPersonToEvent(new Long(1),new Long(1),out);
        addEmailToPerson(Long.valueOf("1"), "deji@yahoo.com");
        out.write("Person Added");
   
       
        out.close();
    }
    private void addPerson(String fname, String lname, String age){
        Session session = HibernateUtil.currentSession();
        session = HibernateUtil.currentSession();
        session.beginTransaction();
       
        Person aPerson = new Person();
       
       
        aPerson.setFirstname(fname);
        aPerson.setLastname(lname);
        aPerson.setAge(age);
       
        session.save(aPerson);
        session.getTransaction().commit();
    }
    private void addEvent(String title, Date date){
        Session session = HibernateUtil.currentSession();
        session.beginTransaction();
        Event theEvent = new Event();
       
        theEvent.setTitle(title);
        theEvent.setDate(date);
       
        session.save(theEvent);
        session.getTransaction().commit();
    }
    private List listEvents() {
        Session session = HibernateUtil.currentSession();
        session.beginTransaction();
        List result = session.createQuery("from Event").list();
        session.getTransaction().commit();
        return result;
    }
    private void addPersonToEvent(Long personId, Long eventId, PrintWriter out) {
        Session session = HibernateUtil.currentSession();
        session.beginTransaction();
        Person aPerson = (Person) session.load(Person.class, personId);
        Event anEvent = (Event) session.load(Event.class, eventId);
       
        Set s = new HashSet();
        s.add(anEvent);
        aPerson.setEvents(s);
        session.save(aPerson);
        session.getTransaction().commit();
    }
    private void addEmailToPerson(Long personId, String emailAddress) {
        Session session = HibernateUtil.currentSession();
        session.beginTransaction();
        Person aPerson = (Person) session.load(Person.class, personId);
     
        aPerson.getEmailAddresses().add(emailAddress);
        session.getTransaction().commit();
    }

Hibernate version:
HIbenate 3.1
Mapping documents:
Code:
<hibernate-mapping>
    <class name="test.hibernate.Event" table="EVENTS">
        <id name="id" column="EVENT_ID">
            <generator class="native"/>
        </id>
        <property name="date" type="timestamp" column="EVENT_DATE"/>
        <property name="title"/>
        <set name="participants" table="PERSON_EVENT" inverse="true">
            <key column="EVENT_ID"/>
            <many-to-many column="PERSON_ID" class="test.hibernate.Person"/>
        </set>
    </class>
</hibernate-mapping>


<hibernate-mapping>
    <class name="test.hibernate.Person" table="PERSON">
        <id name="id" column="PERSON_ID">
            <generator class="native"/>
        </id>
        <property name="age"/>
        <property name="firstname"/>
        <property name="lastname"/>
       
        <set name="events" table="PERSON_EVENT">
            <key column="PERSON_ID"/>
            <many-to-many column="EVENT_ID" class="test.hibernate.Event"/>
        </set>
       
        <set name="emailAddresses" table="PERSON_EMAIL_ADDR">
            <key column="PERSON_ID"/>
            <element type="string" column="EMAIL_ADDR"/>
        </set>
    </class>
</hibernate-mapping>


<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:3306/eventdb</property>
        <property name="connection.username">root</property>
        <property name="connection.password">request</property>
        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">15</property>
        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLMyISAMDialect</property>
        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>
        <!-- Disable the second-level cache -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>
        <!-- Drop and re-create the database schema on startup
        <property name="hbm2ddl.auto">create</property>-->
        <mapping resource="test/hibernate/Event.hbm.xml"/>
        <mapping resource="test/hibernate/Person.hbm.xml"/>
       

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



Code between sessionFactory.openSession() and session.close():

Code:
    private static Log log = LogFactory.getLog(HibernateUtil.class);
   
    private static SessionFactory sessionFactory;
   
    private static SessionFactory getSessionFactory() {
        try {
            if (sessionFactory == null) {
                Configuration configuration = new Configuration();
                configuration.configure("hibernate.cfg.xml");
               
                sessionFactory =  configuration.buildSessionFactory();
            }
           
        } catch (Throwable ex) {
            log.error("Initial SessionFactory creation failed.", ex);
            throw new ExceptionInInitializerError(ex);
        }
        return  sessionFactory;
    }
   
    public static final ThreadLocal session = new ThreadLocal();
   
   
    public static Session currentSession() throws HibernateException {
        Session s = (Session) session.get();
        if (s == null) {
            s = getSessionFactory().openSession();
            session.set(s);
        }
        return s;
    }
   
   
    public static void closeSession() throws HibernateException {
        Session s = (Session) session.get();
        session.set(null);
        if (s != null)
            s.close();
    }
   

Full stack trace of any exception that occurs:

No Exceptions
Name and version of the database you are using:
MySQL 5.0.3 and JDBC Driver: mysql-connector-3.1.6
The generated SQL (show_sql=true):
Hibernate: insert into EVENTS (EVENT_DATE, title) values (?, ?)
Hibernate: insert into PERSON (age, firstname, lastname) values (?, ?, ?)
Hibernate: select person0_.PERSON_ID as PERSON1_2_0_, person0_.age as age2_0_, person0_.firstname as firstname2_0_, person0_.lastname as lastname2_0_ from PERSON person0_ where person0_.PERSON_ID=?

Debug level Hibernate log excerpt:

_________________
Life is a Risk, Take it or you are not living


Top
 Profile  
 
 Post subject: Re: Problem with My First Hibernate Application
PostPosted: Thu Feb 09, 2006 5:48 am 
Beginner
Beginner

Joined: Mon Jan 30, 2006 2:28 am
Posts: 47
Location: INDIA
Pls check, If u have 2 tables u should have 2 mapping files.

_________________
A.Edward Durai
"The things which are impossible with men are possible with God."


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.