Automatically Ignore #OneToMany, #ManyToOne - javers

I'm successfully logging entities with Javers, but for every entity I have to put #DiffIgnore and #ShallowReference to ignore fields mapped with #OneToMany, #ManyToOne, etc.
I'm trying to configure on JaversBuilder, but without success.
Is there a way to configure on Javers to automatically ignore properties mapped with these annotation? Or only take properties with #Column annotation?
I read these, and tried to follow some of the answers, but it didn't worked either.
Javers - DiffIgnore on bidirectional OneToMany
Javers default ignore instead of default include

There is no way to order JaVers to ignore a field when it has a specific annotation. JaVers doesn't read all annotations in your classes.
You can implement a annotation scanner on your side and then register your EntityDefinitions in JaversBuilder:
public JaversBuilder registerEntity(EntityDefinition entityDefinition)

Related

Spring Data JPA + Bytecode Enhancement

Is it possible to load #*ToOne attributes eagerly using JPA interface(Entity Graphs) which are set lazy using #LazyToOne , #LazyGroup in the parent entity class and enabled bytecode enhancement ? I am trying to load such attributes eagerly using entity graph but it is firing another query for such #*ToOne attributes when an parent entity is queried.
Trying to have another way to override static fetch type in entity classes including #LazyToOne which was added with bytecode enhancement.
Using Spring 5.1.3 , Spring JPA 2.2 , Hibernate 5.4.19
Update : Data JPA is working as expected and i could see joins for the attributes which i am trying to fetch eagerly but those lazy attributes are not being initialised with the join query response and hibernate causing each query on referencing attributes which were annotated with #LazyToOneOption.NO_PROXY and was already fetched eagerly using entity graph in my repository.
How can i avoid this second select which is not even required since i got the that data eagerly from entity graph in JPA respository ??
Any help would be highly appreciated.
Entity Graphs just like Hibernate fetch profiles apply regardless of what annotations you have on the association. If it does not, maybe there is a bug in Spring Data or maybe even Hibernate. It's probably best if you create a new JIRA issue with a test case reproducing the problem.
Having said that, I think this is the perfect use case for Blaze-Persistence Entity Views.
I created the library to allow easy mapping between JPA models and custom interface or abstract class defined models, something like Spring Data Projections on steroids. The idea is that you define your target structure(domain model) the way you like and map attributes(getters) via JPQL expressions to the entity model.
An example DTO model could look like the following with Blaze-Persistence Entity-Views:
#EntityView(User.class)
public interface UserDto {
#IdMapping
Long getId();
String getName();
Set<RoleDto> getRoles();
#EntityView(Role.class)
interface RoleDto {
#IdMapping
Long getId();
String getName();
}
// Other mappings
}
Querying is a matter of applying the entity view to a query, the simplest being just a query by id.
UserDto a = entityViewManager.find(entityManager, UserDto.class, id);
The Spring Data integration allows you to use it almost like Spring Data Projections: https://persistence.blazebit.com/documentation/entity-view/manual/en_US/index.html#spring-data-features

How to map a Spring Data JPA repository entity into a view model?

Here is the situation, I want to fetch an entity from database and map it to a new view domain model which has more or less properties, if this view model has more properties, signs the extra properties with default value. I want a map technique in JPA to complete this, which is similar to MyBatis mapping mechanism.
So how to do it?
Just load the entity, copy it over in the new entity, fill the unset properties with the desired default values and store it using JPA (possibly via Spring Data JPA).
For copying over the data from one entity to another you might want to look int Dozer or similar libraries.
You could also misuse Spring Data's projection support to query the original entity, but return it as the target entity with methods similar to the following:
interface SourceRepository<Source, Long> extends CrudRepository<Source, Long> {
List<Target> findTargetBy();
}
The resulting Target entities then could be stored again using another repository (you might have to set version and id properties to null to make it clear to the framework that these are new entities.

How do I execute named queries from a JPA EntityListener?

I have a requirement to set a date_updated value in my database for each row when that row is updated. Let's call the entity that I'm working with Order, which has a corresponding orders table in the database.
I've added the date_updated column to the orders table. So far, so good.
The #Entity Order object that I'm working with is provided by a third party. I do not have the ability to modify the source code to add a field called dateUpdated. I have no requirement to map this value to the object anyway - the value is going to be used for business intelligence purposes only and does not need to be represented in the Java entity object.
My problem is this: I want to update the date_updated column in the database to the current time each time an Order object (and its corresponding database table row) is modified.
Constraints:
We are using Oracle, Spring, JPA and Hibernate
I cannot use Oracle triggers to update the value. We are using a database replication technology that prevents us from using triggers.
My approach thus far has been to use a JPA EntityListener, defined in xml, similar to this:
<entity-mappings xmlns="....">
<entity class="com.theirs.OrderImpl">
<entity-listeners>
<entity-listener class="com.mine.listener.OrderJPAListener" />
</entity-listeners>
</entity>
</entity-mappings>
My listener class looks like this:
public class OrderJPAListener {
#PostPersist
#PostUpdate
public void recordDateUpdated(Order order) {
// do the update here
}
}
The problem I'm having is injecting any sort of persistence support (or anything at all, really) into my listener. Because JPA loads the listener via its methods, I do not have access to any Spring beans in my listener class.
How do I go about injecting an EntityManager (or any Spring bean) into my listener class so that I can execute a named query to update the date_updated field?
How do I go about injecting an EntityManager (or any Spring bean) into
my listener class so that I can execute a named query to update the
date_updated field?
As noted above JPA 2.1 supports injecting managed beans to an Entity Listener via CDI. Whether or not Spring supports this I am not sure. The folloiwng post proposes a Spring specific solution.
https://guylabs.ch/2014/02/22/autowiring-pring-beans-in-hibernate-jpa-entity-listeners/
A possible alternative approach would be however to override the SQL generated by Hibernate on an update which is possible as detailed below.
https://docs.jboss.org/hibernate/orm/3.6/reference/en-US/html/querysql.html#querysql-cud
This would be straightforward if you had the source as you would just need to add the #SQLUpdate annotation and tag on the additional date_update column. As you don't however you would need to look at redefining the metadata for that Entity via an xml configuration file and defining the sql-update statement as outlined above:
https://docs.jboss.org/hibernate/stable/annotations/reference/en/html/xml-overriding.html#xml-overriding-principles-entity
Since JPA 2.1 Entity Listeners are CDI managed. Have you tried using #PersistenceUnit annotation? Are you using JTA transaction type?
Otherwise you could use Persistence.createEntityManagerFactory within the Listener class to retrieve the Persistence Context.

How to change which field is used as _id without annotations in Spring data MongoDB

I have some classes which are being generated from a WSDL using the CXF wsdl2java tool.
I would like to store instances of these classes in a MongoDB database, using Spring data MongoDB.
The default mapping is acceptable for this, except for one thing:
I would like to change which field is used as _id.
Normally this is done with a annotation like #Id.
But because these classes are generated, I would like to do this without an annotation.
Is there a (correct?) way to do this?
So my generated class is:
class Simple {
String businessId;
String otherfield1;
.
String otherfield999;
}
And I would like Spring data MongoDB to use 'businessId' as the '_id' field in MongoDB, without changing the 'Simple' class by adding an annotation.
Thanks!
That's currently not supported. The property either needs to be id, _id or annotated with #Id.

How can I get the fieldnames of the PK (#Id) of a JPA #Entity

I'm using JPA2 (Hibernate 4) and have a bunch of #Entity classes with my PKs defined using #Id. How can I programatically retrieve the fieldnames of the PKs of each entity class?
The business case for this is that I want to use Spring's BeanUtils.copyProperties method to copy over entity objects, but want to make sure that I ignore the id fields of my entities while copying.
Is there a way to find this information? Does JPA provide this information? I realize that I can use reflection to go through every class, but hoped there would be an easier solution. I looked into scanning the PropertyDescriptors of the class, but cannot seem to find any method on the PropertyDescriptor that would give me the annotations of a property.
Try this:
#PersistenceUnit
private EntityManagerFactory emf;
Metamodel metamodel = emf.getMetamodel();
metamodel.entity(MyEntity.class).getId();

Resources