Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7817307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:11:25+00:00 2026-06-02T06:11:25+00:00

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

  • 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T06:11:27+00:00Added an answer on June 2, 2026 at 6:11 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have tables like this: tblUsers int UserID string UserName tblUsersInRoles int UserID int
I have 3 tables namely: Town; Village; Farmers...and I need to COUNT how many
My prob in brief: I have two tables namely category and product. table: category
I have a database with three tables in my database namely catering, room, room_booking
I have tables like these two test_table date student test 2012-05-31 Alice Math 2012-05-31
I have three tables namely test1, test2, test3 test1 data =========== id test_id q_id
newbie programmer here. I have 3 tables namely product, category, and subcategory. I configured
MY PLATFORM: PHP & mySQL WHAT I HAVE HERE: I have 4 tables, namely,
I have two tables, namely Price List (Table A) and Order Record (Table B)
Imagine I have a service that looks like this: public interface MyAccountService { boolean

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.