0

I have 2 tables namely subscriber and contact. Tables look something like this:

subscriber -> id, contact_id //contact_id is a foreign key
contact -> id, firstName, lastName, email, contactType

My Contact.hbm.xml file looks like this:

<hibernate-mapping>
    <class name="com.DBNAME.model.Contact" table="contact" >

        <id name="id" type="int">
            <column name="id" />
            <generator class="identity" />
        </id>

        <property name="contactType" type="int">
            <column name="contactType" sql-type="TINYINT"></column>
        </property>
        <property name="firstName" type="string">
            <column name="firstName"></column>
        </property>
        <property name="lastName" type="string">
            <column name="lastName"></column>
        </property>
    </class>
</hibernate-mapping>

And my Subscriber.hbm.xml file looks like this :

<hibernate-mapping>
    <class name="com.DBNAME.model.Subscriber" table="subscriber" >

        <id name="id" type="int">
            <column name="id" />
            <generator class="identity" />
        </id>

        <many-to-one name="contact" class="com.DBNAME.model.Contact" column="contact_id" unique="true" fetch="join"/>
    </class>

</hibernate-mapping>

Now I want to retrieve a simple Subscriber object in which contact gets mapped automatically. So what I do in Java code is :

/**
     * get Subscribers
     */
    @SuppressWarnings("unchecked")
    private void getSubscribersWithContactDetails() {
        Session session = HibernateUtils.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        try {
            setSubscribers((List<Subscriber>)session.createQuery("from Subscriber").list());
        } catch (HibernateException e) {
            session.getTransaction().rollback();
        } finally {
            session.getTransaction().commit();
        }
        }

/**
     * @param subscribers the subscribers to set
     */
    public void setSubscribers(List<Subscriber> subscribers) {
        this.subscribers = subscribers;
    }

My data classes looks like the following :

    public class Contact implements Serializable {

        private static final long serialVersionUID = 1L;

        private int id;
        private int contactType;
        private String firstName;
        private String lastName;
    // Getters Setters and constructors
    }


public class Subscriber implements Serializable {
    private static final long serialVersionUID = 1L;

    private int         id;
    private Contact     contact; //Foreign Key from Contact -> id
    private int         contactId;
//Constructors, Getters and Setters
}

And my query generated by Hibernate looks like this :

select subscriber0_.id as id1_, subscriber0_.contact_id as contact2_1_ from subscriber subscriber0_

I am not getting contact details from contacts table. How will I be able to do that?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Ahmed
  • 2,966
  • 7
  • 42
  • 69
  • contact object is null and all the details inside contact are set to Null. – Ahmed Apr 13 '12 at 19:43
  • use 'lazy=false' in your .hbm file. It is true in your case because of which you are getting null entries. Once you use this option, it will keep the data in subscriber. – instanceOfObject Apr 13 '12 at 19:54

1 Answers1

1

Try to use this:

 <many-to-one name="contact" 
     class="com.DBNAME.model.Contact" column="contact_id" 
     unique="true" lazy="false"/>

I.e. lazy="false" and no fetch attribute.

chrki
  • 6,143
  • 6
  • 35
  • 55
Eugene Retunsky
  • 13,009
  • 4
  • 52
  • 55
  • org.hibernate.PropertyAccessException: Null value was assigned to a property of primitive type setter of com.smackdab.model.Contact.shipAddressId – Ahmed Apr 13 '12 at 19:59
  • I have this one shipAddressId as well in Contact table. And it empty at the moment. :s – Ahmed Apr 13 '12 at 20:00
  • 1
    If you have `Null value was assigned to a property of primitive type` it means that most probably `contactType` or `contactId` column contains null values. Try to change its type to Integer. – Eugene Retunsky Apr 13 '12 at 20:02
  • Love you for the quick answer! Brotherly love! :D ... But how did you figure out the lazy=false thing? I didnt get it. And btw for now I changed the fields from int to string so it can accept all the fields as null! ;) – Ahmed Apr 13 '12 at 20:16
  • You can also change it to `Integer`. Just a lot of experience with Hibernate :) – Eugene Retunsky Apr 13 '12 at 20:19
  • Practice + Stackoverflow + Internet Search. Just trying different approaches for different projects. After a certain amount you know what works, what is awkward, and what should be avoided. – Eugene Retunsky Apr 13 '12 at 21:30