How to prevent Hibernate from creating duplicates in many to one mapping?

Up vote 0 down vote favorite share g+ share fb share tw.

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 City.hbm. Xml 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 cities = criteria.list(); // However, this generates warnings about unsafe casting. If you use this then // set @SuppressWarnings("unchecked") List 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; java hibernate link|improve this question edited Aug 6 '11 at 2:42 asked Aug 3 '11 at 3:37bugy956 100% accept rate.

– spinning_plate Aug 3 '11 at 16:27 I create a city object and set the description say: city. SetDescription("Paris") and then person. SetCity(city).

If the city with description="Paris" is used in two separate Person "save" sessions, it is saved twice (unless unique is set for the description column - then I get an exception on the second save). I was hoping that I could construct a relationship that would build the City table with unique entries through the saving of Person objects. – bugy Aug 3 '11 at 22:54 You need to look up the city object in your database to get the ID - this is how it will make it unique.

Another option is to make the ID field the string. – spinning_plate Aug 3 '11 at 23:15.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions