How is access strategy determined in a JPA XML descriptor file? - java

If NO annotation is specified in a class and all metadata is specified in an XML descriptor file, how is the access strategy(field/method) determined?

From the book Pro JPA 2:
The access element that is defined in the persistence-unit-defaults
section is used to set the access type for all the managed classes in
the persistence unit that have XML entries but are not annotated. Its
value can be either “FIELD” or “PROPERTY”
<entity-mappings>
<persistence-unit-metadata>
<persistence-unit-defaults>
<access>PROPERTY</access>
</persistence-unit-defaults>
</persistence-unit-metadata>
</entity-mappings>
The default can be overridden via the access attribute on any entity, mapped-superclass, or embeddable element.

Related

Why hibernate not including all modified properties of an entity in dirtyProperties array?

Hibernate is not including the property annotated by Javax.Persistence.Version in dirtyProperties list which is provided by the PostUpdateEvent. Is this the default behavior of Hibernate? If yes can I configure which properties it should not blacklist?

How to validate the name property of an element in XML file using javax xml validation

I have the following XML file:
<node name="FIXED_NAME">
...
</node>
Note that all elements in my XML are node (same name) and I can not change them for some reasons.
I am using java xml validator and xsd file for validation.
I want to validate that the name property for the root node must be FIXED_NAME.
How my xsd file should be?
I cannot think of any solution to your problem, for these reasons:
XSD 1.0 identifies the type of a tag by matching the hierarchy of tags in the XML document against the element/type definitions in the XSD. In your case, there is no hierarchy of names (because all tag names are the same).
XSD 1.1 provides some extra flexibility via xs:alternative and xs:assert - but they do not allow references to the parent axis.
For these reasons, I cannot think of any way to use the features of XML Schema to describe your XML.

Ignoring Field in Apache Olingo

Currently my Project is using the JPA for the Database Connection.
I'm also using the default OdataJPA Processor.
How can i achieve not to include certain fields for example ("password") in my odata2 API response. Or do I really have to implement a customOdataJPAProcessor?
The easiest way for excluding some JPA entity attributes is to define a JPA-EDM mapping model. This is basically an XML file which adheres to this schema. You can read more about it in the documentation here: redefining OData JPA metadata.
You have two different ways of linking the mapping model XML, either you specify a file name of a file located in the WEB-INF folder (assuming that you are building a WAR) or, if this is not flexible enough, you can create a JPA EDM extension and return the mapping model file as a stream.
This is how such a file may look like:
<?xml version="1.0" encoding="UTF-8"?>
<JPAEDMMappingModel xmlns="http://www.apache.org/olingo/odata2/jpa/processor/api/model/mapping">
<PersistenceUnit name="My_Persistence_Unit">
<JPAEntityTypes>
<JPAEntityType name="MyEntity">
<EDMEntityType>MyEntity</EDMEntityType>
<EDMEntitySet>MyEntities</EDMEntitySet>
<JPAAttributes>
<JPAAttribute name="attribute" exclude="true" />
</JPAAttributes>
<JPARelationships />
</JPAEntityType>
</JPAEntityTypes>
<JPAEmbeddableTypes />
</PersistenceUnit>
</JPAEDMMappingModel>

How to generate List instead of Set with Hibernate tools reverse engineering?

I would like to know how I can get in POJOS generated by Hibernate tools when doing reverse engineering of a database the collections of relationships (OneToMany, ManyToMany) like List instead of Set.
Is there any way to specify in the file reveng.xml?
Answer depends on engineering pipeline. I use hibernate-tools 4.3.5.Final and Ant task hibernatetool with notable configurations:
<hbm2hbmxml/> for HBM files generation;
<hbm2java jdk5="true" ejb3="true"/> to create Java classes from HBM. Solution is in HBM contents.
Is there any way to specify in the file reveng.xml?
No. hbm2hbmxml is configured by reveng.xml. Péter's answer suggests to specify desired type in <column/>, but OneToMany has no column. Relationship may explicitly be defined as following
<table name="MY_TABLE">
<foreign-key constraint-name="FK_TO_MY_TABLE_IN_OTHER_TABLE">
<set property="otherTables"/>
</foreign-key>
</table>
Notice <set/> element. There is no <list/> or other collection in hibernate-reverse-engineering-3.0 namespace. I have not found a way to change collection type in reveng.xml or via Ant task parameters, so every relationship in generated HBM is <set> element (in hibernate-mapping-3.0 namespace).
Is there a way to change collection type?
Yes. HBM file must contain desired type element (<list>, <bag>, <map>, etc.) instead of <set>. By default Hibernate tools use templates in JAR folders hbm and pojo. Each of those may be overridden by files in subfolders of folder specified in templatepath attribute of hibernatetool Ant task. You need to provide custom FreeMaker templates in hbm folder with following modifications.
In the end of persistentclass.hbm.ftl replace
<#if c2h.getTag(property)!="version" && c2h.getTag(property)!="timestamp">
with (notice template name starts with list, desired collection type, and processed tag is set)
<#if c2h.getTag(property) == "set">
<#include "list.hbm.ftl"/>
<#elseif c2h.getTag(property)!="version" && c2h.getTag(property)!="timestamp">
In list.hbm.ftl remove following line (because set has no index property)
<#assign indexValue = value.getIndex()>
and replace mandatory element
<list-index>
<#foreach column in indexValue.columnIterator>
<#include "column.hbm.ftl">
</#foreach>
</list-index>
with (put whatever you like instead of ID, it is not present in generated Java code)
<list-index column="ID"/>
List implies order. I suggest to add OrderBy annotation with related entity ID field name. Add to list.hbm.ftl after <#include "meta.hbm.ftl">
<meta attribute="scope-get">#javax.persistence.OrderBy("id") public</meta>
maggu suggests to add extra-import meta attribute to have package name mentioned in class file only once, but in my experience it breaks build with undefined symbol.
Notice actual scope after annotation. There is no way to put it on separate line (whitespace gets trimmed).
Alternative to scope hack is adding specific annotation in pojo build stage (one with hbm2java) together with other annotations, e.g., add to Ejb3PropertyGetAnnotation.ftl within <#elseif c2h.isCollection(property)> block following line
<#if property.value.type.returnedClass.simpleName == "List">#javax.persistence.OrderBy("id")</#if>
<list> and <bag> lead to new ArrayList<>(0) as default field value, but list is exposed as useful java.util.List, while bag is only java.util.Collection. It should be possible to change this (also for set) via collection-type attribute, but it accepts only org.hibernate.usertype.UserCollectionType implementations, not JDK classes.
In my project I had to replace Set with List only for certain relationships, so my list.hbm.ftl contains <#if property.name == "otherTables">. You can adjust condition as needed.

SolrJ: Field mappings by mixin

Is it possible with SolrJ to define a bean and provide its field mapping by a mixin class when using QueryResponse#getBeans(Class)?
Example: Let E be an entity with a name attribute. The schema defines *_s_de and *_s_en dynamic fields.
Depending on a supplied language identifier I want to map the name_s_de or name_s_en field value to the name attribute of E without changing its implementation to have #Field("name_s_de") hardcoded.
Thanks!

Categories