MongoDB Mapping classes - java

Does MongoDB (specifically the Morphia ORM) support any class mapping features like gson's type adapter, I'm trying to map a class that contains some fields that I don't want to store.

You can annotate fields with #Transient and they won't get mapped.

Related

SprinBoot DTO to Entity (viceversa) conversion using JMapper

I'm using SpringBoot and I would like to write the adapter to have the conversion utility from DTOs to Entities and vice versa.
I'm exploring some Converter tools like JMapper, ObjectMapper, etc
Into my converters I need to have some custom conversions (for example from Enum to different Enum and other types).
I'm not able to find into the documentation if there is a way define custom converter method using JMapperAPI. Is there this possibility?
I saw that it's possible to use an annotation #JMapConversion on the custom method but I prefer to have the conversion logic into my adapter so I prefer to don't use this annotation.
Suggestions?
I suppose you have these classes:
DtoRequest
Enttiy
DtoResponse
You can create a new class Common which contains common fields after that extend this class by all other classes.
class Common { //...common fields}
class DtoRequest extend Common { //fields in request but not in the enitty}
class Enttiy extend Common { //fields in entity but not in the dto classes}
class DtoResponse extend Common { //fields in response not not in the enitty}
You can use copyProperties from spring in order to automatically map common values. Other values can be mapped manually.
BeanUtils.copyProperties(Object source, Object target, Common.class)
// DtoRequest --> Enttiy
// Enttiy --> DtoResponse

Is it possible to map Spring Data Couchbase with external document (from dependency)?

I need to retrieve data from legacy Couchbase bucket without a specific schema. It can be mapped as com.couchbase.client.java.document.StringDocument from the Couchbase Java client. I can do this directly using Java client:
bucket.get(key, StringDocument.class)
But how can I map this StringDocument using org.springframework.data.repository.CrudRepository?
I can't create such interface interface UserRepository extends CrudRepository<StringDocument, String> because Spring Data Couchbase requires #Document and #Id annotations.
All entities should be annotated with the #Document annotation. Also, every field in the entity should be annotated with the #Field annotation. While this is - strictly speaking - optional, it helps to reduce edge cases and clearly shows the intent and design of the entity.
There is also a special #Id annotation which needs to be always in place. Best practice is to also name the property id.
Should I directly use Bucket, create my own similar entity, or there is another solution?
I use following version of spring-boot-starter-data-couchbase: 1.5.9.RELEASE.
If its just a StringDocument (and it wasn't even saved by spring-data-couchbase to begin with.. which injects the _class attribute for deserialization purposes) then I'd just use the bucket to retrieve it.
Keep in mind if your repository (I'm talking about any other repository you have defined that is mapped to the same bucket the StringDocument is in) is defined as CouchbaseRepository you will be able to access the bucket itself from the repository methods like so repository.getCouchbaseOperations().getCouchbaseBucket(). Or maybe you can try the following repository.getCouchbaseOperations().findById("id", StringDocument.class).

Annotation based hibernate mapping for external classes

Is it possible to provide annotation based hibernate mapping informations for external classes?
I have a model class (for example Credit) in an external package (without any dependencies to hibernate) which I can't / wan't edit and now I wan´t to define mapping informations for this class.
It's no problem to define these mapping informations in a xml file but I am looking for a possibility to define these informations via annotations.
I can extend the model class but then the child class is mapped but i wan't to map the superclass.

How to instantiate a class from string FQN in database in hibernate?

I'm trying to convert a legacy application to hibernate.
I have an entity that has a field, which should be instantiated to object instance based on a fully qualified name string in a database.
Consider the example below - if I have somePackageName.FirstClass in a database the someObject field should be an instance of FirstClass.
I guess I could use property access and persist / retrieve a string but that doesn't look very elegant to me.
I cannot use #PostLoad etc. - I'm using pure hibernate with spring (not JPA) - these annotations get ignored.
I know for example in MyBatis one can register a custom handler for field. Would anything similar be available in Hibernate?
I'm new to hibernate so I'm not really sure what the options are.
#Entity
class SomePersistentClass{
private SomeInterface someObject;
}
class FirstClass implements SomeInterface{
}
class SecondClass implements SomeInterface{
}
You can use JPA features such as #PostLoad, etc callbacks simply by enabling the proper Hibernate event listeners. Check the Hibernate EntityManager guide for details.
But this one is even easier. This is the role of a Type in Hibernate. First, you'll have to write an implementation of org.hibernate.type.Type or org.hibernate.usertype.UserType and specify that in #Type( type=... ) that handles the conversions (lots of web resources about writing custom Hibernate types). Then annotate your 'someObject' attribute with #Type( type="your.custom.TypeImpl" )

Is is possible to create a custom jpa annotation in java

Jpa one of the big successfull module of jpa and so are its annotation features .I have weird requirement in which i need to create jpa annotation ,one that jpa can process
ex. We have in jpa a table annotation that create a table for this java pojo class.
i need to make another annotation that behaves exactly as what table annotation does + some of my custom reqirements;
what is mean
if i create a customAnnotation like #Anil that is suppose to work same as #Table Annotation does
than
#Anil
public class Anp
{
}
than this should create a table in the database is that possible or not .if it is give me some way to do this
thanks
JPA does not process just any annotation. The JPA implementation processes the annotations that it supports and these are typically just the javax.persistence annotations, and optionally its own extensions. Your JPA provider may allow you to define your own, but this is not going to be very common - look at the docs for your JPA provider if they allow a user to define annotations.
For example, the JPA provider I have used (DataNucleus JPA) allows the user to provide annotations for the class or for the field/property.

Categories

Resources