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 6216703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:17:47+00:00 2026-05-24T07:17:47+00:00

I have two simple objects; Person and City. A City may have multiple Persons

  • 0

I have two simple objects; Person and City. A City may have multiple Persons but a Person only one City. I would like to be able to save a Person and have Hibernate automatically save the City – only if it is a new City – that is cities should be unique. With my mapping I find that every time a Person is saved, a new City is inserted in the database, regardless if it is already there. I am sure that this is fairly straight forward, however I am new to Hibernate and struggling with it. Note, if I save two Person with a single City in one session – all works. This is an issue across sessions. I have also created equals and hashcode methods in the City class as per below.

Person.hbm.xml

  <id name="id" type="long" column="id" >
   <generator class="native"/>
  </id>

  <property name="firstName">
   <column name="firstname" />
  </property>

 <many-to-one name="city" class="com.project.City" cascade="save-update">
  <column name="cityId" not-null="true" />
 </many-to-one>


City.hbm.xml

   <id name="id" type="long" column="id" >
   <generator class="native"/>
  </id>

  <property name="description" unique="true">
   <column name="description" />
  </property>


public class City implements java.io.Serializable {
    private Long id;
    private String description;

      // getters and setters

    public boolean equals(Object other) {
        if (this==other) return true;
        if ( !(other instanceof City) ) return false;
        final City that = (City) other;
        return this.description.equals( that.getDescription() );
    }   

    public int hashCode() {
        return description.hashCode();
    }    
}




    try {
      Transaction tx = session.beginTransaction();

      Criteria criteria = session.createCriteria(City.class);
      criteria.add(Restrictions.eq("description", city.getDescription()));
      criteria.setMaxResults(1);

      // Possible to use:
      // List<City> cities = criteria.list();
      // However, this generates warnings about unsafe casting.  If you use this then
      // set @SuppressWarnings("unchecked")
      List<City> cities = HibernateUtil.castList(City.class, criteria.list());

      if (cities.isEmpty())  {
          session.save(city);
        }
      else  {
          city = (City) cities.get(0);
        }
          session.flush();
          tx.commit();
            }
        catch (Exception e) {
          return null;
            }
      }
return city;
  • 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-05-24T07:17:48+00:00Added an answer on May 24, 2026 at 7:17 am
        if ( !(other instanceof Ward) ) return false;
        final Ward that = (Ward) other;
    

    Shouldn’t this be City instead of Ward?

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

Sidebar

Related Questions

Simple idea: I have two images that I want to merge, one is 500x500
I have two webapplication, one is a simple authenticationsite which can authenticate the logged
Let's say I have two strings like this: XABY XBAY A simple regex that
I'm still struggling with this concept. I have two different Person objects, very simply:
I have two simple models: Book, and Author Each Book has one Author, linked
I have two generic list objects, in which one contains ids and ordering, and
I have two simple models in my Django app. Here's what they look like:
I have a very simple object model with a base class Person and two
I have two simple tables in Django which looks like: class Session(models.Model): id =
I have two objects that are searching for one-another on the stage. They move

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.