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: automatic relational insert ?
PostPosted: Sat Oct 29, 2005 5:43 am 
Beginner
Beginner

Joined: Thu Oct 20, 2005 12:35 pm
Posts: 21
I have a very basic question about the features of Hibernate.

Suppose we have 2 entities, Teacher and Courses. and a relation TeacheS, so it is obvious that a Teacher teacheS one or more Courses.

Clearly this is a many to one relationship.

This would produce 3 tables in my database:

Teacher, Teaches, Courses

SO, when I ad a teacher, and a Course is Hibernate capable of AUTOMATICLY making the proper inserts in the Teaches Table ? (When typing this I think it sounds weard, but i don't know, maybe the mapping files can do this). Or is it me who has to write some java code for it.

In te latter case, then why the hell are the <many to one ... > clauses used in the mapping files ? as some sort of constraints ??

Thx for clearing this up for me...


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 29, 2005 6:03 pm 
Beginner
Beginner

Joined: Thu Jan 22, 2004 8:22 pm
Posts: 48
Most if not all forms of of relationships are automaically updated by Hibernate. For the particular case you site I have more then one example in projects I've done and establishment and removal of the entry in the equivalent of the TeacheS table is automatic.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 30, 2005 1:55 pm 
Beginner
Beginner

Joined: Thu Oct 20, 2005 12:35 pm
Posts: 21
Well then how does this feature works ? What extra info do i need to add to my mapping files ? My apologies if this is an extreme noob question, but i don't seem to find the answer in the documentation.

Thx


Top
 Profile  
 
 Post subject: Use association mappings (set/map/bag) and maybe try cascade
PostPosted: Sun Oct 30, 2005 3:05 pm 
Beginner
Beginner

Joined: Sun Oct 09, 2005 3:21 pm
Posts: 40
Without seeing your mapping files one thing that mixed me up was having logically solid mapping definitions (one-to-many or many-to-many associations) but never actually updating the relation tables after a save or update call. Took a fifth/sixth look at the documentation to find info about the "cascade" attribute in for example the set element. By setting cascade equal to "save-update" then relationships built up by your code are saved with the POJO being acted upon.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 30, 2005 5:26 pm 
Beginner
Beginner

Joined: Thu Oct 20, 2005 12:35 pm
Posts: 21
I have a kind of "who was first chicken or egg" feeling about it. Suppose i set the mapping files good.

We remain in the example i gave previously.

I create a new Teacher object, and save it to the database. It can't be saved to the Teaches table yet, because it hasn't been assigned a Course to teach. It works the same way when creating a Course first.

Logically i guess my java class Teacher should have a attribute pointing to the Course he is Teaching. So when i Set that element, and then save my Teacher object, IS IT THEN WHEN THE TEACHES TABLE IS UPDATED ???

Another question (maybe better for Tools forum), but I am trying to do reverse engineering on my tables, can this process also recognize the relationships between the entities ? And can it create the according many to many associations automaticly ?

Thx


Top
 Profile  
 
 Post subject: Pretty much right on
PostPosted: Mon Oct 31, 2005 3:33 am 
Beginner
Beginner

Joined: Sun Oct 09, 2005 3:21 pm
Posts: 40
If the foreign key columns in Teacher/Course to the relational table is nullable then it is no problem saving the those classes independently. Exactly. Use the association setter to save a relationship object.

Try messing with SchemaExport to generate DLL. Also HibernateToolTask to create the POJO objects. This saving me time just to get up and running (i.e. test relationships and mapping logic).


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.