aspose file tools*
The moose likes Object Relational Mapping and the fly likes Is a field with ID mandatory for Hibernate mapping Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Is a field with ID mandatory for Hibernate mapping" Watch "Is a field with ID mandatory for Hibernate mapping" New topic
Author

Is a field with ID mandatory for Hibernate mapping

Harsha Ka
Ranch Hand

Joined: Mar 14, 2010
Posts: 45

I am trying out simple examples with Hibernate and wrote a Hibernate mapping xml with two fields name and age. Part of the xml looked like this.



I was not able to run this as the schema validation was failing with the below exception.


Error parsing XML: XML InputStream(8) The content of element type "class" must match "(meta*,subselect?,cache?,synchronize*,comment?,tuplizer*,(id|composite-id),discriminator?,natural-id?,(version|timestamp)?,(property|many-to-one|one-to-one|component|dynamic-component|properties|any|map|set|list|bag|idbag|array|primitive-array)*,((join*,subclass*)|joined-subclass*|union-subclass*),loader?,sql-insert?,sql-update?,sql-delete?,filter*,resultset*,(query|sql-query)*)".
Initial SessionFactory creation failed.org.hibernate.InvalidMappingException: Could not parse mapping document from resource

I finally figured out that I needed to have an id which needs identifies every unique row. Below is the xml which worked



So was wondering if "id" is mandatory for every table to use it in Hibernate.

Thanks in advance for your help..

SCJP 1.6,Preparing (Tryin to prepare) for scwcd
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1648
    
    7

Yes, hibernate requires an Id. Sometimes if you are dealing with a legacy database that for whatever reason does not have a key, you can define the key in Hibernate to be a composite key of all the columns for example, as this will be guaranteed to be unique. Hibernate does not have a concept of what the 'real' database keys are so you can do things like this in a pinch. 99% of the time a table should have a key though and that should be mapped accordingly in Hibernate.

Also unless you are really partial to XML I would find some more modern examples as they tend to be annotation based. The framework world is moving away from XML configuration. I personally find the annotation based configuration much easier to write and read. Others may have differing opinions, but at least try both. Some sub-projects like Hibernate Envers do not even have XML support and there has not been enough demand to justify anyone to implement it.




[How To Ask Questions][Read before you PM me]
Harsha Ka
Ranch Hand

Joined: Mar 14, 2010
Posts: 45

Bill Gorder wrote:
Also unless you are really partial to XML I would find some more modern examples as they tend to be annotation based. The framework world is moving away from XML configuration. I personally find the annotation based configuration much easier to write and read. Others may have differing opinions, but at least try both. Some sub-projects like Hibernate Envers do not even have XML support and there has not been enough demand to justify anyone to implement it.


Many thanks. I do plan to try out both xml and annotation based examples.
 
Consider Paul's rocket mass heater.
 
subject: Is a field with ID mandatory for Hibernate mapping