-->
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: Mapping foreign keys
PostPosted: Mon Dec 06, 2010 12:03 pm 
Beginner
Beginner

Joined: Mon Nov 15, 2010 10:39 am
Posts: 27
Hi.

I have a set of tables, where some correspond to uml classes, and some to associations. The complete database structure contains foreign keys to link the the tables together on a database level. The tables are created through an import.sql script at startup, into an empty db. My question is: How do I map this in Hibernate?

------------------ FK ----------------------- FK ------------------
| ClassTable1| <-------|AssociationTable|------->| ClassTable2 |
------------------ ----------------------- ------------------

I first tried a property mapping in the associations tables, to each of the classes, but that did not work (no surprise).

Then i tried mapping it using many-to-one in the association table, something like this:

<many-to-one name="classtable" column="class1Id" class="Class1"/>

This should make me able to traverse from an association to each of the classes in it? (But not the other way)

Problem is when i run this, and try to run the import script, i get an error message saying that some table already exists. Does this mean that a mapping like this will create a table if one does not exist? Do I need to change the CREATE TABLE statements in the import.sql script to ALTER TABLE statements instead? Or do i even need to run the import.sql script to create the tables, if all the information is in the mappings?

- Tobb

Edit: Found that this last problem was due to the hibernate.hbm2ddl.auto being set to create. But this is a prerequisite for being able to input the import.sql script at startup? So if it is create/create-drop, i get an error because of double definition of tables when import.sql is run, if it is something else it doesn't read import.sql at all?


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.