• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Is a field with ID mandatory for Hibernate mapping

 
Ranch Hand
Posts: 45
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.



 
Harsha Ka
Ranch Hand
Posts: 45
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic