Unable to use DataNucleus enhancer without JDO annotations - java

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.

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

make openJPA caching only chosen tables

How can I set openJPA cache so that it works only for chosen entities, maybe I need use some annotaion over they?
my persistence.xml contains:
<property name="openjpa.DataCache" value="true"/>
<property name="openjpa.RemoteCommitProvider" value="sjvm"/>
but thats settings works for all my entities(tables), so i want to cache for example only that table:
#Entity(name = "IsoCountryCodes")
#Table(name = "ISO_COUNTRY_CODES", schema = "ANALYSIS")
#DataCache(timeout=120000)
public class IsoCountryCodes implements Serializable{
....
}
But #DataCache doesnt fix it, its only set the timeout of cache saving.
UPDATE:
I cannot use openJPA 2.0 cause my project deployed on WebLogic 10.36 and have provided KODO openJPA 1.3.
Also i try to include only chosen entities by adding property:
property name="openjpa.DataCache" value="true(Types=foo.bar.FullTimeEmployee)"
but got this error:
org.apache.openjpa.lib.util.ParseException: There was an error while setting up the configuration plugin option "DataCache". The plugin was of type "class kodo.datacache.KodoConcurrentDataCache". The plugin property "Type" had no corresponding setter method or accessible field. All possible plugin properties are: [CacheSize, EvictionSchedule, FailFast, NAME_DEFAULT, Name, SoftReferenceSize].
Can you help me?Maybe you know other ways to exclude or include entitites from caching, maybe with Ehcache usage?
<property name="openjpa.DataCache" value="true"/>
That enables the L2 cache for all Entities. If you are using jpa-2.0, try adding <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode> to turn the cache on. Also, replace the #DataCache annotation with a #javax.persistence.Cacheable annotation.

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!

Hibernate: Migrating from mapping to annotations - is it possible to mix hbm and annotation?

I'm currently migrating my project from Hibernate HBM Mappings to Annotations.
Everything was easy as far as i dealt with small classes.
But I have same huge classes and i try to mix both mapping and annotations for this class. I read that this was possible by using the hibernate property "hibernate.mapping.precedence" and setting it to "class, hbm" instead of "hbm, class". (see: In Hibernate: is it possible to mix Annotations and XML configuration for an Entity?)
For example I have the following Document class:
#Entity
#Table(name="DOCUMENT")
public class Document {
#Column(name="DESCRIPTION")
private String description;
}
and the following Document.hbm.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Document" table="DOCUMENT" >
<id name="id" column="DOCUMENT_ID" type="long" />
</class>
</hibernate-mapping>
In my hibernate.cfg.xml file I put:
<property name="hibernate.mapping.precedence">class, hbm</property>
<mapping class="Document"/>
<mapping resource="Document.hbm.xml"/>
My problem is that:
- if I put "class, hbm" for the precedence then I have ONLY my annotations in class Document
- if I put "hbm, class" then I have ONLY my mappings in the hbm ressource
Does anyone knwo if there is a way to have both Annotations and HBM mappings ?
Thanks
Kamran
PS: I use : Hibernate 4.1.4 and Spring Framework 3.1.1
You can't mix them for the same class. At the end of section 1.2 of hibernate annotations:
You can mix annotated persistent classes and classic hbm.cfg.xml declarations with the same SessionFactory. You can however not declare a class several times (whether annotated or through hbm.xml). You cannot mix configuration strategies (hbm vs annotations) in an entity hierarchy either.
To ease the migration process from hbm files to annotations, the configuration mechanism detects the mapping duplication between annotations and hbm files. HBM files are then prioritized over annotated metadata on a class to class basis. You can change the priority using hibernate.mapping.precedence property. The default is hbm, class, changing it to class, hbm will prioritize the annotated classes over hbm files when a conflict occurs.
Using annotations and hbm files is declaring a class two times. Therefore, one will be prioritized over the other in a class to class basis (class to class basis means that for each class, only the hbm file or the annotations are used).

Categories