Override Hibernate caching strategy - java

I have two Java applications, manager and server, that use a shared library, domain. The Hibernate mapping is defined through annotations in the source of domain.
What I'm trying to accomplish is overriding the caching strategy of server to the 'read-only' caching strategy for some performance tests. Following the annotations documentation, I came to the point where I was trying:
<?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="com.example.data.Entity" metadata-complete="false">
<cache usage="read-only"/>
</entity>
...
</entity-mappings>
Trying to mirror the annotation markup, mixing it with those instructions. Problem is, there's no allowed element cache for entity, and I get:
Error parsing XML (line66 : column 108): cvc-complex-type.2.4.a: Invalid content was found starting with element 'cache'. One of '{"http://java.sun.com/xml/ns/persistence/orm":description, "http://java.sun.com/xml/ns/persistence/orm":table, "http://java.sun.com/xml/ns/persistence/orm":secondary-table, "http://java.sun.com/xml/ns/persistence/orm":primary-key-join-column, "http://java.sun.com/xml/ns/persistence/orm":id-class, "http://java.sun.com/xml/ns/persistence/orm":inheritance, "http://java.sun.com/xml/ns/persistence/orm":discriminator-value, "http://java.sun.com/xml/ns/persistence/orm":discriminator-column, "http://java.sun.com/xml/ns/persistence/orm":sequence-generator, "http://java.sun.com/xml/ns/persistence/orm":table-generator, "http://java.sun.com/xml/ns/persistence/orm":named-query, "http://java.sun.com/xml/ns/persistence/orm":named-native-query, "http://java.sun.com/xml/ns/persistence/orm":sql-result-set-mapping, "http://java.sun.com/xml/ns/persistence/orm":exclude-default-listeners, "http://java.sun.com/xml/ns/persistence/orm":exclude-superclass-listeners, "http://java.sun.com/xml/ns/persistence/orm":entity-listeners, "http://java.sun.com/xml/ns/persistence/orm":pre-persist, "http://java.sun.com/xml/ns/persistence/orm":post-persist, "http://java.sun.com/xml/ns/persistence/orm":pre-remove, "http://java.sun.com/xml/ns/persistence/orm":post-remove, "http://java.sun.com/xml/ns/persistence/orm":pre-update, "http://java.sun.com/xml/ns/persistence/orm":post-update, "http://java.sun.com/xml/ns/persistence/orm":post-load, "http://java.sun.com/xml/ns/persistence/orm":attribute-override, "http://java.sun.com/xml/ns/persistence/orm":association-override, "http://java.sun.com/xml/ns/persistence/orm":attributes}' is expected.
When, I try to return to the hibernate-core XML markup, I get errors that the <class> definition is incomplete, and I can't seem to find a metadata-complete analog. Does anyone know if what I'm trying to do is possible? And if so, what steps am I going to have to take? Thanks!

Related

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 stop persistence from altering database, JPA

I am using camel and open jpa as persistent provider, but I don't want alter statements to be run on prduction.
Snapshot of persistence.xml
<persistence-unit name="camel-openjpa-oracle-alert" transaction-type="RESOURCE_LOCAL">
.
.
<provider>
org.apache.openjpa.persistence.PersistenceProviderImpl
</provider>
<properties>
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=false)" />
</properties>
.
.
</persistence-unit>
What value we have to put for openjpa.jdbc.SynchronizeMappings, so that alter command are not executed.
I searched but was unable to find any such value.
It would be nice to know a little more about what you are doing and why you need to use SynchronizeMappings. The fact that you use ForeignKeys=true tells me you want OpenJPA to read you schema and determine if you have any database FKs defined (i.e. so OpenJPA knows about these FKs so it can order SQL properly to honor parent/child FK constraints). This is a perfectly valid use of SynchMappings. However, by using 'buildSchema', you are specifically telling OpenJPA to make "the database schema match your existing mappings"....this comment is lifted from this OpenJPA doc:
http://openjpa.apache.org/builds/1.2.3/apache-openjpa/docs/ref_guide_mapping.html#ref_guide_mapping_synch
Therefore, you are specifically telling OpenJPA to update your database schema. You can remove the 'buildSchema' if you don't want OpenJPA to update your schema to match your domain model. That is, try:
Or you could use 'validate' in place of 'buildSchema'....however, as the above doc states, OpenJPA will throw an exception if it finds a schema/domain mismatch which may not be what you want. I suggest you read the above doc, and look at the available options to you.
Thanks,
Heath Thomann

Forcing xsi:type attribute on generated xml in JAXB

I need to force the xsi:type generation on fields because the xml will be deserialized into a different object through a xsl transformation sheet. Datatypes are defined with XSD schemas, anyone can use jaxb to generate classes and send data to the endpoint but the endpoint is completely dynamic and uses a special DTO containing common data fields and some Object maps which will hold the dynamic data fields.
For example this is what I get:
<?xml version="1.0" encoding="UTF-8"?>
<myp:documento xmlns:myp="mypns" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<myp:sys_name>name</myp:sys_name>
<myp:sys_path>path</myp:sys_path>
<myp:sys_type>type</myp:sys_type>
<myp:dyn_date_modified>2015-09-30T11:13:10.810+02:00</myp:dyn_date_modified>
</myp:documento>
this is what I need:
<?xml version="1.0" encoding="UTF-8"?>
<myp:documento xmlns:myp="mypns" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<myp:sys_name>name</myp:sys_name>
<myp:sys_path>path</myp:sys_path>
<myp:sys_type>type</myp:sys_type>
<myp:dyn_date_modified xsi:type="xs:dateTime">2015-09-30T11:13:10.810+02:00</myp:dyn_date_modified>
</myp:documento>
because the attribute dyn_date_modified will be deserialized into the dynamic Object map..
Let me know if you need more detail.
After another day of research seems that JAXB doesn't offer this functionality because it is supposed to marshal and unmarshal xml using always the same object. In my case I need to marshal xml from a Class generated by xjc through a XSD and then unmarshal with an annotated DTO which contains a number of fixed fields and three maps for dynamic fields (starting with def_, dyn_ and mul_). I resolved my requirement by adding a xsl preprocessing stage which maps prefixed fields to maps and appends xsi:type based on name prefix.
<myp:documento xmlns:myp="mypns" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<myp:sys_uuid>2ca56a7e9ca62dcd2d2ad8eeca100fd24</myp:sys_uuid>
<myp:sys_name>2127a09acda7bf6acf26257bb80372f90</myp:sys_name>
<myp:dyn_date_modified>2015-09-30T11:13:10.810+02:00</myp:dyn_date_modified>
</myp:documento>
converts to:
<myp:documento xmlns:myp="mypns" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<myp:sys_uuid>uuid</myp:sys_uuid>
<myp:sys_name>name</myp:sys_name>
<myp:dynFields>
<entry>
<key>dyn_date_modified</key>
<value xsi:type="xs:dateTime">2015-09-30T11:13:10.810+02:00</value>
</entry>
</myp:dynFields>
</myp:documento>
and then gets correctly unmarshalled.

Unable to use DataNucleus enhancer without JDO annotations

I am trying DataNucleus with JDO api using only XML to define the persistence model, without adding annotation like #PersistenceCapable. That is something that supposedly supported by both JDO and DataNucleus, if I did understand both documentations.
For instance if I remove all annotations of Book.java, Inventory.java, Product.java in datanucleus example and run mvn clean compile I should get the job done, because package.orm define those classes, but I get the following error for all this classes:
(main) DEBUG [DataNucleus.MetaData] - Class
org.datanucleus.samples.jdo.tutorial.Inventory was specified in
persistence-unit (maybe by not putting exclude-unlisted-classes)
Tutorial but not annotated, so ignoring
....
(main) INFO [DataNucleus.Enhancer] - DataNucleus Enhancer completed with success for 0 classes.
What I am missing?
Actual configuration files:
persistence.xml
...
<persistence-unit name="Tutorial">
<class>org.datanucleus.samples.jdo.tutorial.Inventory</class>
<class>org.datanucleus.samples.jdo.tutorial.Product</class>
<class>org.datanucleus.samples.jdo.tutorial.Book</class>
<exclude-unlisted-classes/>
...
</persistence-unit>
...
package-h2.orm
<orm>
<package name="org.datanucleus.samples.jdo.tutorial">
<!-- persistence-modifier is by default equal to: persistence-capable -->
<class name="Inventory" table="INVENTORIES" >...</class>
<class name="Product" table="PRODUCTS">...</class>
<class name="Book" table="BOOKS">...</class>
</orm>
ORM metadata is to OVERRIDE JDO metadata. Consequently you need either annotations OR a JDO XML metadata file (package.jdo).
"class" entries in persistence.xml are to specify classes that have annotations, and you say you have none.
"mapping-file" entries in persistence.xml are to specify XML metadata files ... and you haven't specified any.

Inconsistency checking between entity and table

I'm looking for easy way to check inconsistency between entity and table for my JPA application.
After changing table definition (ex. column name, type, add new column, delete column), I sometimes forget to change entity definition.
So I'd like to be notified if entity and table definitions are inconsistent.
Is some tool available? Eclipse plugin is preferable, but others are also considerable.
I know Dali. But this tool does not suit for me because I should modify Dali output.
(I'm using class inheritance as this question, and so on.)
Your JPA implementation should provide a property on persistence.xml to make it for you. By example, Hibernate provides hibernate.hbm2ddl.auto property which allow to create the schema, update or just validate.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence ...>
<persistence-unit ...>
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<!-- ... -->
<!-- ... -->
<property name="hibernate.hbm2ddl.auto" value="validate"/>
This makes the schema validation process on EntityManager initialization.
Check on your current JPA implementation documentation to find the equivalent property.
Good luck!

Categories