|
|
AnnotatedSessionFactory and orm.xml mapping file
Trying to set up a small Hibernate Annotations example using AnnotatedSessionFactory. The example works fine when I'm using JPA annotations to configure the entities, but when I'm trying to introduce a orm file to compliment the mappings (override a @NamedQuery annotation) it fails.
For some reason I get a SAXParseException saying quot;Cannot find the element 'entity-mappings' from the xcerces parser.
Even with a empty mappings-file, with just the lt;entity-mappingsgt;-emelent I get the error.
Here's my config:
Code:
lt;bean id=quot;sessionFactoryquot; class=quot;org..orm.hibernate3.annotation.AnnotationSessionFactoryBeanquot;gt; lt;property name=quot;dataSourcequot; ref=quot;dataSourcequot;/gt; lt;property name=quot;schemaUpdatequot; value=quot;truequot;/gt; lt;property name=quot;mappingResourcesquot;gt; lt;listgt; lt;valuegt;mapping.xmllt;/valuegt; lt;/listgt; lt;/propertygt; lt;property name=quot;annotatedClassesquot;gt; lt;listgt; lt;valuegt;com.test.AnnotatedPersonlt;/valuegt; lt;/listgt; lt;/propertygt; lt;property name=quot;hibernatePropertiesquot;gt; lt;propsgt; lt;prop key=quot;hibernate.dialectquot;gt; org.hibernate.dialect.MySQL5Dialect lt;/propgt; lt;/propsgt; lt;/propertygt;
lt;/beangt;
My mapping file:
Code:
lt;?xml version=quot;1.0quot; encoding=quot;UTF-8quot;?gt;
lt;entity-mappings xmlns rm=quot;xml/ns/persistence/ormquot;
xmlns:xsi=quot;2001/XMLSchema-instancequot;
xsi:schemaLocation=quot;xml/ns/persistence/orm orm_1_0.xsdquot;gt;
lt;/entity-mappingsgt;
Is there something I'm doing wrong? How do you attach the EJB 3-style xml-mapping to the AnnotatedSessionFactory?
Take a look at the xsd or/and the JPA spec - the entity mappings has some mandatory elements (such as the persistence unit) which are mandatory.
Sure you're not confusing it with the persistence.xml? The orm.xml file don't have any mandatory fields except the entity-mappings, to my knowledge..?
Found out what was wrong I had copied the xml-header from a sun site, that had introduced orm highlighted in the code below:
Removing it solved my problem.Code:
lt;?xml version=quot;1.0quot; encoding=quot;UTF-8quot;?gt;
lt;entity-mappings xmlns rm=quot;xml/ns/persistence/ormquot;
xmlns:xsi=quot;2001/XMLSchema-instancequot;
xsi:schemaLocation=quot;xml/ns/persistence/orm orm_1_0.xsdquot;gt;
lt;/entity-mappingsgt;Right - if you wanted to leave the orm in place you should have used entity-mappings under its namespace: i.e lt;orm:entity-mappingsgt;.
And you were right - I confused persistence.xml with orm.xml.
I'm trying to introduce a orm file to compliment the mappings (override a @NamedQuery annotation)
Way to go!
I am not using AnnotatedSessionFactoryBean. What if you have not one but, say, twenty entities, are you going to stick all of them into annotatedClasses element? Instead, I am using LocalContainerEntityManagerFactoryBean (test environment) and falling back on automated discovery of all entities in the classpath (and a small hint from persistence.xml
Code:
lt;!DOCTYPE beans PUBLIC quot;-//SPRING//DTD BEAN//ENquot; quot;dtd/spring-beans-2.0.dtdquot;[lt;!ENTITY LocalContainerEntityManagerFactoryBean quot;org..orm.jpa.LocalContainerEntityManagerFactoryBeanquot;gt;lt;!ENTITY HibernateJpaVendorAdapter quot;org..orm.jpa.vendor.HibernateJpaVendorAdapterquot;gt;lt;!ENTITY persistenceXmlLocation quot;com/arno/net/test/jdbc/jpa/META-INF/persistence.xmlquot;gt; ]gt;
lt;beans default-init-method=quot;initquot;gt; lt;bean id=quot;entityManagerFactoryquot; class=quot;amp;LocalContainerEntityManagerFactoryBean;quot;gt; lt;propertyname=quot;dataSourcequot;ref=quot;dataSourcequot; /gt; lt;propertyname=quot;persistenceXmlLocationquot;value=quot;amp;persistenceXmlLocation;quot; /gt; lt;property name=quot;jpaVendorAdapterquot;gt;lt;bean class=quot;amp;HibernateJpaVendorAdapter;quot; /gt; lt;/propertygt; lt;/beangt;
....
lt;/beansgt;
persistence.xml
Code:
lt;?xml version=quot;1.0quot; encoding=quot;UTF-8quot;?gt;
lt;persistence xmlns=quot;xml/ns/persistencequot; xmlns:xsi=quot;2001/XMLSchema-instancequot; xsi:schemaLocation=quot;xml/ns/persistence xml/ns/persistence/persistence_1_0.xsdquot; version=quot;1.0quot;gt; lt;persistence-unit name=quot;postgres_testquot; transaction-type=quot;RESOURCE_LOCALquot;gt; lt;mapping-filegt;com/arno/net/test/jdbc/jpa/META-INF/orm.xml lt;/mapping-filegt; lt;propertiesgt;lt;property name=quot;hibernate.hbm2ddl.autoquot; value=quot;create-dropquot; /gt;lt;property name=quot;hibernate.dialectquot; value=quot;org.hibernate.dialect.PostgreSQLDialectquot; /gt;lt;property name=quot;hibernate.show_sqlquot; value=quot;truequot; /gt;lt;property name=quot;hibernate.format_sqlquot; value=quot;truequot; /gt;lt;property name=quot;hibernate.use_sql_commentsquot; value=quot;truequot; /gt;lt;!-- RepeatableRead --gt;lt;property name=quot;hibernate.connection.isolationquot; value=quot;4quot; /gt; lt;/propertiesgt; lt;/persistence-unitgt;
lt;/persistencegt;
orm.xml
Code:
lt;?xml version=quot;1.0quot;?gt;
lt;entity-mappings xmlns=quot;xml/ns/persistence/ormquot; xmlns:xsi=quot;2001/XMLSchema-instancequot; xsi:schemaLocation=quot;xml/ns/persistence/orm xml/ns/persistence/orm/orm_1_0.xsdquot; version=quot;1.0quot;gt; lt;sequence-generator name=quot;spring_sequencequot; sequence-name=quot;spring_test_sequencequot; /gt; lt;named-query name=quot;EmployeeDao:findEmployeeNamesquot;gt; lt;querygt;lt;![CDATA[select e.name.firstName, e.name.lastName, e.name.userName from Employee e order by e.name.lastName ]]gt; lt;/querygt; lt;/named-querygt; lt;named-query name=quot;EmployeeDao:findDepartmentPhonesByTypequot;gt; lt;querygt;lt;![CDATA[select p.numberfrom Employee e join e.phones p where upper(e.department.name)=upper(:departmentName) and upper(p.type)=upper(:phoneType) ]]gt; lt;/querygt; lt;/named-querygt;
See? No fancy orm namespace. Works just fine for me.
Hi
I have a very similar set up. My only problem is that if I drop orm.xml into the classpath, the @Entity annotations are ignored. Any idea why?
Is it a bad practice to mix annotations with orm.xml?
Thanks.
Originally Posted by mucsijHi
I have a very similar set up. My only problem is that if I drop orm.xml into the classpath, the @Entity annotations are ignored. Any idea why?
Is it a bad practice to mix annotations with orm.xml?
Thanks.
If it's a bad practice or not depends on how you use it to compliment the annotations I guess. My opinion is that it is a good practice to use orm to override table and schema names, but when you use it to override column mappings, or even worse named queries your data access layer becomes difficult to understand. You always have to check both source code for annotations and the orm.xml to know exactly what value a query has and which column a property is mapped to. When using orm.xml to compliment annotations, you should have rules that states which overrides are quot;allowedquot; to avoid these kinds of problems.
(Come to think of it, if the developer never really cares about the database, a column mapping override isn't that confusing, as the developer will never write any native sql to access data)
The entity annotations shouldn't be ignored when you have an orm in your classpath, at least not according to the Java Persistence API spec. Now, Hibernate Annotations isn't a JPA implementation (it's just a release that let's you do orm mapping with annotations, both jpa and hibernate specific), so I guess it doesn't have to be implemented according to the spec. Reading the docs however suggest it follows the spec when dealing with an orm.xml. Not sure why you experience these difficulties.
Does your orm.xml file contain a lt;xml-mapping-metadata-complete/gt; element? If it does this tells the EntityManager to ignore Annotations as the xml mapping metadata is complete.
Yes, lt;xml-mapping-metadata-complete/gt; was there and the annotations were ignored. |
|