-->
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.  [ 6 posts ] 
Author Message
 Post subject: Handling duplicates in "nested" objects?
PostPosted: Fri Sep 05, 2003 12:13 pm 
Newbie

Joined: Fri Sep 05, 2003 12:01 pm
Posts: 5
I have a many-to-one relationship defined between 2 classes (MusicFile and Category). The idea is that each MusicFile object can have an associated Category. That means that 2 different MusicFile objects can have the same Category.

I have the relationship defined as follows:
Code:
<hibernate-mapping>
    <class name="music.beans.MusicFile" table="musicfile">
        <id name="id" column="id" type="long">
            <generator class="identity"/>
        </id>
        <many-to-one name="category" column="cat_name" class="music.beans.Category" cascade="all"/>
        <property name="album"/>
        <property name="artist"/>
        <property name="serverURL"/>
        <property name="title"/>
    </class>

    <class name="music.beans.Category" table="category">
        <id name="name" column="name" type="string">
            <generator class="assigned"/>
        </id>
    </class>
</hibernate-mapping>


Now, I try to add the following:
MusicFile1 (id=1, name="file1", category="cat1")
MusicFile2 (id=2, name="file2", category="cat1")

Since I have cascade="all" specified the Category object is added when I do a session.save() on the MusicFile. It works for MusicFile1, but MusicFile2 throws an SQLException because "cat1" already exists.

My question is - how do I get around this with Hibernate? Or do I have to turn off cascading and add the Categories before I add the MusicFiles? I thought that there might be some way to say for Category that if it already existed then don't add it.

Any help is appreciated.


Top
 Profile  
 
 Post subject: no expert but...
PostPosted: Fri Sep 05, 2003 12:31 pm 
Beginner
Beginner

Joined: Thu Sep 04, 2003 2:50 pm
Posts: 45
Location: US: New York NY
are you doing a setCategory(instanceOfCat) or are you doing setCategoryId(catId)

maybe if you do the former, it will not try to create a new object. I'm too new to be sure, but i figured i'd throw that out there.


Top
 Profile  
 
 Post subject: more code
PostPosted: Fri Sep 05, 2003 2:10 pm 
Newbie

Joined: Fri Sep 05, 2003 12:01 pm
Posts: 5
Here is the code snippet from my simple test program. To directly answer your question I am setting via setCategory( CategoryInstance).

Code:
    SessionFactory sessionFactory = config.buildSessionFactory();
    Session session = sessionFactory.openSession();
    Transaction t = session.beginTransaction();
    System.out.println("MANY-TO-ONE MAPPING");

    // MusicFile and Category
    Category cat = new Category();
    cat.setName( "MyFavorites" );

    MusicFile musicFile = new MusicFile();
    musicFile.setCategory( cat );
   
    session.save(musicFile);
    t.commit();
    session.close();


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 05, 2003 2:36 pm 
Beginner
Beginner

Joined: Thu Sep 04, 2003 2:50 pm
Posts: 45
Location: US: New York NY
i think you want to do this:

// MusicFile and Category
Category cat = new Category();
sess.load(cat,"MyFavorites");

MusicFile musicFile = new MusicFile();
musicFile.setCategory( cat );


Top
 Profile  
 
 Post subject: don't understand
PostPosted: Fri Sep 05, 2003 6:54 pm 
Newbie

Joined: Fri Sep 05, 2003 12:01 pm
Posts: 5
Not sure why I would want to do that? The category I am adding may or may not exist. Shouldn't the hibernate code have a way to handle this? Especially since I want to put this into an automated loop and read files in from the filesystem and use this code.

Additionally, even if I do the session.load(cat) won't hibernate still try and add it?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 05, 2003 7:36 pm 
Beginner
Beginner

Joined: Thu Sep 04, 2003 2:50 pm
Posts: 45
Location: US: New York NY
I don't want to sound rude here, but as far as I can tell, hibernate will not do anything magical for you. Its the glue between your object and the database. It seems to have been designed to be fast, and not so much to figure out your specific business logic. (DISCLAIMER.. I'm pretty new to hibernate, so I'm no expert)

It seems to me that you will have to do your own lookup to figure out if the category already exists.

If it were my code, i would do this (roughly) ...

java.util.List allcat = sess.find("select cat from Category cat");

while (true) {

Category c = new Category();
c.setName(arrayofvars[i]);
int where = allcat.indexOf(c);
if (where >=0)
{ c = allcat.get(where);}
else
{ sess.save(c);}

MusicFile musicFile = new MusicFile();
musicFile.setCategory( cat );

sess.save(musicFile);

}
sess.flush();
tx.commit();

maybe hibernate team has a better method... not sure... hope it helps


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

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.