0

I am working on a project in hibernate where there is both annotations and xml mapped entities.

Here is the setup, Entity/Class A is annotation based.

Class A

@Id
long id;

I need a collection in class A, that can only be mapped through XML configurations. We cannot mix annotations and xml for a given entity I know.

But can I just create a wrapper class, call it X, and create a corresponding XML mapping for X with the collection xml configuration that I would have put in A? So we'd have:

Class A

@Id
long id;

Object X;

And then also a X.hbm.xml file. Note this file will contain mapping to formulate a collection of a column from say Table B, that is mapped by A's primary key which is a foreign key in B. So I guess it will actually have to reference Table A and B.

Is this workaround going to work or not? Seems like a long shot...

1 Answer 1

0

You may try mapping files (I used them to redefine some properties of an "external" entity)

persistence.xml

<persistence-unit name="yourPersistenceUnit" transaction-type="RESOURCE_LOCAL">
  <provider>org.hibernate.ejb.HibernatePersistence</provider>
  <mapping-file>META-INF/orm-custom.xml</mapping-file>

orm-custom.xml

<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings
        xmlns="http://java.sun.com/xml/ns/persistence/orm"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm orm_2_0.xsd"
        version="2.0">

    <entity class="your.Entity">
        <attributes>
            <one-to-many name="attributes" fetch="LAZY"/>
            <one-to-many name="answers" fetch="LAZY"/>
        </attributes>
    </entity>
</entity-mappings>
4
  • Here is some documentation: docs.jboss.org/hibernate/annotations/3.5/reference/en/html/…
    – Multisync
    Oct 17, 2014 at 9:22
  • I'm a bit confused, so your solution allows us to override annotations through XML? SO I can have a class with all annotations called "your.Entity", and one field/attribute of this class can be overriden through this orm-custom.xml file?
    – James
    Oct 17, 2014 at 19:30
  • @James, You'd better refer to the documentation. Actually, I did what you asked. I had an entity in an external jar and this entity used EAGER for its lists. In order to overwrite this I used that orm-custom.xml
    – Multisync
    Oct 17, 2014 at 19:38
  • @James "The primary target for metadata in EJB3 is annotations, but the EJB3 specification provides a way to override or replace the annotation defined metadata through an XML deployment descriptor. In the current release only pure EJB3 annotations overriding are supported. If you wish to use Hibernate specific features in some entities, you'll have to either use annotations or fallback to hbm files. You can of course mix and match annotated entities and entities describes in hbm files."
    – Multisync
    Oct 17, 2014 at 19:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.