Welcome everybody.
I've visited to discuss a simple but crucial performance issue that I'm wondering about.
It is easy to bring the case of mine to a very simple example. Let's consider a mechanism that massively adds users to DBMS. The class diagram / DB schema looks as follows:
User n ------> 1 City
Adding a new User object could look like this:
Code:
City city = null;
Query query = session.createQuery("from City where name=?");
query.setString(0, cityName);
Iterator it = query.iterate();
if (!it.hasNext()) {
city = new City(cityName);
} else {
city = (City) it.next();
}
User user = new User();
user.setName(userName)
user.setCity(city);
session.persist(user);
I'm wondering if there's any way of doing the same thing without the necessity of acquiring the City object from the DB. Something like this:
Code:
City city = new City(cityName);
User user = new User();
user.setName(userName)
user.setCity(city);
session.persist(user);
But - as you surely know - simply doing it this way results with a restriction violation error if city name is set to unique and two users from "Rome" are added (we assume User table doesn't have such restrictions).
I was wondering if some configuration settings could make the second code snippet work so that Hibernate could detect duplicates and assigning previous identifier to the new transient object.
Thanks for your replies in advance!
Konrad