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

  • SEARCH
  • Home
  • 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 8720141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:57:55+00:00 2026-06-13T06:57:55+00:00

I’m using Hibernate 4. I have this database with Category and CategoryItem tables: CREATE

  • 0

I’m using Hibernate 4. I have this database with Category and CategoryItem tables:

CREATE  TABLE `test`.`Category` (
  `Id` INT NOT NULL AUTO_INCREMENT ,
  `Description` VARCHAR(50) NOT NULL ,
  PRIMARY KEY (`Id`) )

CREATE  TABLE `test`.`CategoryItem` (
  `IdCategory` INT NOT NULL ,
  `Id` VARCHAR(10) NOT NULL ,
  `Description` VARCHAR(100) NOT NULL ,
  PRIMARY KEY (`Id`, `IdCategory`) ,
  CONSTRAINT `fk_table1_Category1`
    FOREIGN KEY (`IdCategory` )
    REFERENCES `test`.`Category` (`Id` ))

This are used to store different categories or catalogs and its values. For example:

Category table:

1, 'Genders'
2, 'Marital status'
3, 'Countries'

CategoryItem table:

1, 'FEM', 'Female'
1, 'MAL', 'Male'
2, 'SIN', 'Single'
2, 'MAR', 'Married'
3, 'US', 'United States'
3, 'UK', 'England'

These values are used all over the schema, but with logical reference to CategoryItem(Id) only, for example:

CREATE  TABLE `test`.`Person` (
  `Id` INT NOT NULL ,
  `Name` VARCHAR(30) NULL ,
  --this fields are referencing CategoryItem(Id)
  `IdGender` VARCHAR(10) NOT NULL ,
  `IdMaritalStatus` VARCHAR(10) NOT NULL ,
  `IdCountryOrigen` VARCHAR(10) NOT NULL ,
  PRIMARY KEY (`Id`) )

Is it possible to map this tables with some technique in Hibernate? I’m not able to change this schema right now.

EDITED:

These are the classes and mappings so far:

public class Category implements Serializable {

    private int id;
    private String description;

    private Set<CategoryItem> items = new HashSet<CategoryItem>();

    public Category() {
    }

    //getters and setters omitted
}

public class CategoryItem implements Serializable {

    private CategoryItemId id;
    private Category category;
    private String description;

    public CategoryItem() {
    }
    //getters and setters omitted
}

public class CategoryItemId implements Serializable {

    private Category category;
    private String id;

    public CategoryItemId() {
    }
    //getters and setters omitted
}

public class Person implements Serializable {

    private int id;
    private String name;
    private CategoryItem gender;
    private CategoryItem maritalStatus;
    private CategoryItem countryOrigen;

    public PersonaNatural() {
    }
    //getters and setters omitted
}

  <class catalog="test" name="test.Category" table="Category">
    <id name="id" type="int">
      <column name="Id"/>
      <generator class="increment"/>
    </id>
    <property name="description" type="string">
      <column length="50" name="Description" not-null="true"/>
    </property>
    <set inverse="true" name="items">
      <key>
        <column name="IdCategory" not-null="true"/>
      </key>
      <one-to-many class="test.CategoryItem"/>
    </set>    
  </class>

  <class catalog="test" name="test.CategoryItem" table="CategoryItem">
    <composite-id class="test.CategoryItemId" name="id">
        <key-many-to-one name="category" class="test.Category">
            <column name="IdCategory" not-null="true"/>
        </key-many-to-one>
      <key-property name="id" type="string">
        <column name="Id"/>
      </key-property>
    </composite-id>
    <many-to-one name="category" class="test.Category" fetch="select" insert="false" update="false">
      <column name="IdCategory" not-null="true"/>
    </many-to-one>
    <property name="description" type="string">
      <column name="Description" length="100" not-null="true"/>
    </property>
  </class>

<class catalog="test" name="test.Person" table="Person">
    <id name="id" type="int">
      <column name="Id"/>
      <generator class="increment"/>
    </id>
    <property name="name" type="string">
      <column length="30" name="Name" not-null="false"/>
    </property>
    <many-to-one name="gender" class="test.CategoryItem" fetch="select">
        <column name="IdGender" not-null="true"/>
    </many-to-one>
    <many-to-one name="maritalStatus" class="test.CategoryItem" fetch="select">
        <column name="IdMaritalStatus" not-null="true"/>
    </many-to-one>
    <many-to-one name="countryOrigen" class="test.CategoryItem" fetch="select">
        <column name="IdCountryOrigen" not-null="true"/>
    </many-to-one>
  </class>

I’m trying with many-to-one but I’m getting this error: ‘must have same number of columns as the referenced primary key’. This makes sense because I’m pretending to map just CategoryId(Id) on Person.hbm.xml.

I’m gonna need to specific a where or discriminator value to complete the left join condition, because CategoryItem(Id) alone is not unique.

Maybe this is not even possible with Hibernate, but any help I will appreciate.
Thanks.

  • 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-13T06:57:57+00:00Added an answer on June 13, 2026 at 6:57 am

    This is how I solve the Person mapping:

    <class catalog="test" name="test.Person" table="Person">
        <id name="id" type="int">
          <column name="Id"/>
          <generator class="increment"/>
        </id>
        <property name="name" type="string">
          <column length="30" name="Name" not-null="false"/>
        </property>
        <many-to-one name="gender" class="test.CategoryItem" fetch="select">
            <formula>1</formula>
            <column name="IdGender" not-null="true"/>
        </many-to-one>
        <many-to-one name="maritalStatus" class="test.CategoryItem" fetch="select">
            <formula>2</formula>
            <column name="IdMaritalStatus" not-null="true"/>
        </many-to-one>
        <many-to-one name="countryOrigen" class="test.CategoryItem" fetch="select">
            <formula>3</formula>
            <column name="IdCountryOrigen" not-null="true"/>
        </many-to-one>
      </class>
    

    Adding the <formula>(IdCategory)</formula> instead of <column> element causes hibernate matches the composite Id of CategoryItem and resolve correctly the left join.

    Thanks.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have thousands of HTML files to process using Groovy/Java and I need to
I have a reasonable size flat file database of text documents mostly saved in
I have a view passing on information from a database: def serve_article(request, id): served_article
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all

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.