-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: cascaded persitance configuration
PostPosted: Fri Sep 04, 2009 9:15 am 
Newbie

Joined: Fri Sep 04, 2009 7:23 am
Posts: 1
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.