Originally posted by pascal betz:
have you tried ?
pascal
Originally posted by ben oliver:
I posted a thread a few days ago. My classes look like --
1. will tables of "LEVEL_2" and "LEVEL_3" be updated as well ??
2. if the answer is yes, does that mean that for a new row in Table LEVEL_1, database creates a new "ID" and then "pass" this ID to "LEVEL_2" table , and then also creates a new ID for "LEVEL_2" table, and then pass that "LEVEL_2"'s ID on to "LEVEL_3", and also creates a new ID for "LEVEL_3" ?? I doubt Hibernate is so smart..
Originally posted by Peer Reynders:
And your doubt is based on? Hibernate can handle it. The success hinges on whether you define the mappings properly (mainly the cascade attribute on the various mapping elements; but the are other attributes related to cascading behavior). See for example:
5.1.10. many-to-one
5.1.11. one-to-one
5.1.22. any
6.2. Collection mappings
10.11. Transitive persistence
Again - get Hibernate In Action (Hibernate In Action (ThoutReader + PDF ebook)) and read it. It discusses all these issues in detail. Hibernate doesn't cost anything so you might as well invest some time and money into the book.
Originally posted by ben oliver:
And it doesn't matter if the primary or foreign key connecting the tables is a system generated unique ID or some natural key (natural business field of table), right ?
Originally posted by Peer Reynders:
There is a strong recommendation and preference towards the use of synthetic/surrogate keys. In many situations there may be a reason for the business domain (natural) key to change during the lifetime of the record/object - that does not happen for surrogate keys. The primary key of a record can never change during the lifetime of the record.
Originally posted by Peer Reynders:
First you have to set up the appropriate sequences in the Oracle database schema.
Usually you create a sequence for each table.
Then in the mapping you have to indicate the name of the sequence to Hibernate.
5.1.4.4. Identity columns and sequences
Before inserting a new record Hibernate will query the next value in the sequence
SELECT person_id_sequence.nextval FROM DUAL
and then use it as the new key.
If you do not specify a version or timestamp for concurrency control (which you really should) you need to indicate the "unsaved-value" attribute in the "id" element so that hibernate can tell whether the object is a new one. The value that you specify must be the value that you set in the class initialization or class constructor.
5.1.4. id
5.1.7. version
5.1.8. timestamp
Mapped classes must declare the primary key column of the database table. Most classes will also have a JavaBeans-style property holding the unique identifier of an instance. The <id> element defines the mapping from that property to the primary key column.
If I just try this concrete scenario and find it doesn't work, it does NOT mean it can't work --- Maybe it is because I am a newbie and I don't know the right way to do it.
Originally posted by ben oliver:
1. Assuming I use oracle, can I use class = "native" instead of "sequence" ?
Originally posted by ben oliver:
2. By the way, I assume your answer to my last question was -- yes, Hibernate will just generate the unique id for me, and it's behind the scene (e.g. developers don't need to explicitly programatically deal with the unique id), is that right ?
Before inserting a new record hibernate will query the next value in the sequence
SELECT person_id_sequence.nextval FROM DUAL
and then use it as the new key.