Ian
Ian

Reputation: 466

Using hibernate 4 with hbm.xml and xsd

after much googling now confused and frustrated.

I am upgrading an application from Hibernate 3 to 4. This works well using dtd 3.0 but now need to use the 4.0 xsd and this is where every thing goes base over apex!

The app uses hbm.xml files to configure each entity, no annotations anywhere.

It would be very useful to find an example of an hbm file but even tutorials for hibernate 4 just use the 3.0 dtd!

I am using the following

<hibernate-mapping
    xmlns="http://www.hibernate.org/xsd/hibernate-mapping"
    xsi:schemaLocation="http://www.hibernate.org/xsd/hibernate-mapping hibernate-mapping-4.0.xsd"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    package="org.hibernate.metamodel.binding">

Using this I get an LONG list of errors the root cause being

Caused by: org.xml.sax.SAXParseException; lineNumber: 6; columnNumber: 51; cvc-elt.1: Cannot find the declaration of element 'hibernate-mapping'.

My reading of multiple Google pages suggest that I still need a doctype which is confusing and I think I am just misunderstanding and when I do add a doctype Eclipse complains about the definition of the hibernate-mapping element. Ignoring that error (just incase problem with eclipse config) get the same as a run time error:

Attribute "xmlns" must be declared for element type "hibernate-mapping"

I have been unable to find a tutorial or an example of an hbm.xml file that uses the hibernate 4 xsd.

Can anyone please put me out of my misery ?

Upvotes: 4

Views: 3672

Answers (2)

charego
charego

Reputation: 137

Try this:

<?xml version="1.0" encoding="UTF-8"?>

<hibernate-mapping package="your.package.name"
    xmlns="http://www.hibernate.org/xsd/hibernate-mapping"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.hibernate.org/xsd/hibernate-mapping
        http://www.hibernate.org/xsd/hibernate-mapping/hibernate-mapping-4.0.xsd">

    <!-- your mappings --->

</hibernate-mapping>

Note this points to an external address (http://www.hibernate.org).

Ideally we would use the local XSD files that Hibernate packages in hibernate-core.jar.

I don't know whether Hibernate intercepts these requests and returns the local copy like Spring does.

Upvotes: 2

koss
koss

Reputation: 874

Message about "xmlns" attrribute is a bit confusing, although all you have to do is to remove all the attributes from the hibernate-mapping element and leave only package attribute. At least, it worked for me.

Upvotes: 0

Related Questions