-->
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.  [ 1 post ] 
Author Message
 Post subject: polymorphism and many-to-one
PostPosted: Mon Sep 10, 2007 12:03 pm 
Newbie

Joined: Mon Sep 10, 2007 11:49 am
Posts: 1
Hibernate version:3.2.5.ga

Hi I'm trying to get a many-to-one mapping to work
with a simple class hierachy

Here's my little testcase:
Code:
public class EventTest extends TestCase {
   
   public void testCreateEvents() {
      Session session = HibernateUtil.getSessionFactory().getCurrentSession();
      session.beginTransaction();
      
      Event theEvent = new Event();
      theEvent.setTitle("EVENT1");
      theEvent.setDate(new Date());            
      session.save(theEvent);
      
      SubEvent subEvent = new SubEvent();
      subEvent.setTitle("SUB-EVENT");
      subEvent.setDate(new Date());
      subEvent.setSubData("subdata");
      session.save(subEvent);
      
      Person p = new Person();
      p.setAge(100);
      p.setFirstname("Matti");
      p.setLastname("Pehrs");
      p.setSelectedEvent(subEvent);
      session.save(p);
      
      session.getTransaction().commit();
   }
   
   public void testListEvents() {
      Session session = HibernateUtil.getSessionFactory().getCurrentSession();
      session.beginTransaction();

//      List evlist = session.createQuery("from Event").list();
//      for(Iterator it = evlist.iterator();it.hasNext();) {
//         Event ev = (Event)it.next();
//         System.out.println("event["+ev.getId()+"] sub="+(ev instanceof SubEvent));
//      }

      
      List list = session.createQuery("from Person").list();
      for(Iterator it = list.iterator();it.hasNext();) {
         Person p = (Person)it.next();
         Event ev = p.getSelectedEvent();
         System.out.println("person["+p.getId()+"] sub["+ev.getId()+"]="+(ev instanceof SubEvent));
      }
      
      session.getTransaction().commit();
   }
}


The above will not match the associated Event as an SubEvent (which is a SubEvent instance in the database)
If I enable the commented lines above then it will work!!!
Is there something I'm missing here in terms on configuration or limitations in Hibernate when it comes to polymorphic behaviour?

Here's the rest of the test code:
hibernate.cfg.xml:
Code:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

           <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost/esb</property>
        <property name="connection.username">root</property>
        <property name="connection.password">a123</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</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="se/seb/esb/hibernate/Event.hbm.xml"/>
        <mapping resource="se/seb/esb/hibernate/Person.hbm.xml"/>

    </session-factory>

</hibernate-configuration>


Event.hbm.xml:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

    <class name="se.seb.esb.hibernate.Event" table="EVENT">
        <id name="id" column="EVENT_ID">
            <generator class="native"/>
        </id>
       <discriminator column="EVENT_TYPE" type="string" />
       <property name="title"/>
       <property name="date"/> 
       
       <subclass name="se.seb.esb.hibernate.SubEvent"
          discriminator-value="SUB">
          <property name="subData"/>
       </subclass>
       
    </class>

</hibernate-mapping>


Person.hbm.xml:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

    <class name="se.seb.esb.hibernate.Person" table="PERSON">
        <id name="id" column="PERSON_ID">
            <generator class="native"/>
        </id>
        <property name="age"/>
        <property name="firstname"/>
        <property name="lastname"/>
        <many-to-one name="selectedEvent"/>

        <set name="events" table="PERSON_EVENT">
            <key column="PERSON_ID"/>
            <many-to-many column="EVENT_ID" class="se.seb.esb.hibernate.Event"/>
        </set>
       
        <set name="emailAddresses" table="PERSON_EMAIL_ADDR">
            <key column="PERSON_ID"/>
            <element type="string" column="EMAIL_ADDR"/>
        </set>

    </class>

</hibernate-mapping>


The Person, Event and SubEvent classes are just simple BeanClasses:
Code:
package se.seb.esb.hibernate;

import java.util.*;

public class Person {

    private Long id;
    private int age;
    private String firstname;
    private String lastname;
    private Event selectedEvent = null;

    public Person() {}


    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }


    private Set emailAddresses = new HashSet();

    public Set getEmailAddresses() {
        return emailAddresses;
    }

    public void setEmailAddresses(Set emailAddresses) {
        this.emailAddresses = emailAddresses;
    }


    private Set events = new HashSet();

    // Defensive, convenience methods
    protected Set getEvents() {
        return events;
    }

    protected void setEvents(Set events) {
        this.events = events;
    }

    public void addToEvent(Event event) {
        this.getEvents().add(event);
        event.getParticipants().add(this);
    }

    public void removeFromEvent(Event event) {
        this.getEvents().remove(event);
        event.getParticipants().remove(this);
    }

   /**
    * @return Returns the selectedEvent.
    */
   public Event getSelectedEvent() {
      return selectedEvent;
   }
   /**
    * @param selectedEvent The selectedEvent to set.
    */
   public void setSelectedEvent(Event selectedEvent) {
      this.selectedEvent = selectedEvent;
   }
}


package se.seb.esb.hibernate;

import java.util.*;

public class Event {
    private Long id;

    private String title;
    private Date date;

    public Event() {}

    public Long getId() {
        return id;
    }

    private void setId(Long id) {
        this.id = id;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
   

    private Set participants = new HashSet();

    public Set getParticipants() {
        return participants;
    }

    public void setParticipants(Set participants) {
        this.participants = participants;
    }

}


package se.seb.esb.hibernate;

public class SubEvent extends Event {
   
   String subData = null;
   

   /**
    * @return Returns the subData.
    */
   public String getSubData() {
      return subData;
   }
   /**
    * @param subData The subData to set.
    */
   public void setSubData(String subData) {
      this.subData = subData;
   }
}


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

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.