Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for javax.persistence.ElementCollection #255

Open
yashkolte3 opened this issue Dec 10, 2021 · 1 comment
Open

Support for javax.persistence.ElementCollection #255

yashkolte3 opened this issue Dec 10, 2021 · 1 comment

Comments

@yashkolte3
Copy link

yashkolte3 commented Dec 10, 2021

Can we expect the support to javax.persistence.ElementCollection any soon ? For me, I have a use-case to use my existing JPA entities also in android. OrmLite looks very promising to do so, however it is just about this specific annotation right now.
I evaluated also the com.j256.ormlite.dao.ForeignCollection but the difference there is it would create a single table and add a foreign key to it.
Minimal structure of my entities looks like :
``

@javax.persistence.Entity
class SecondActualEntity {
    @javax.persistence.Id
    UUID id;
    String someElement;
    Integer someNumber;
    
    @javax.persistence.ElementCollection
    List<EmbeddableEntity> embeddableEntityList;
}

``

``

@javax.persistence.Entity
class ActualEntity {
   @javax.persistence.Id
    UUID id;
    String someElement;
    Integer someNumber;
    
    @javax.persistence.ElementCollection
    List<EmbeddableEntity> embeddableEntityList;
}

``

``

@javax.persistence.Embeddable
class EmbeddableEntity {
    String key;
    String value;
}

``
Hibernate creates following tables:

actual_entity => some_element, some_number
actual_entity_embeddable_entity_list => key, value, actual_entity_id

second_actual_entity => some_element, some_number
second_actual_entity_embeddable_entity_list => key, value, second_actual_entity_id

@yashkolte3 yashkolte3 changed the title Support of javax.persistence.ElementCollection Support for javax.persistence.ElementCollection Dec 10, 2021
@yashkolte3
Copy link
Author

yashkolte3 commented Apr 20, 2022

In case anyone else is trying to achieve the same, I have implemented this as an extension to com.j256.ormlite.field.types.SerializableType.
Assumption : The elements marked as @ElementCollection are java.io.Serializable (there is however no such hard check)

`import com.j256.ormlite.field.SqlType;
import com.j256.ormlite.field.types.SerializableType;

import java.lang.reflect.Field;

import javax.persistence.ElementCollection;

/**
 * Persistor which can be used for persisting {@link ElementCollection} fields, assuming such fields are {@link java.io.Serializable}.
 */
public class SerializedElementCollectionPersister extends SerializableType {

    private static SerializedElementCollectionPersister singleTon = null;

    private SerializedElementCollectionPersister() {
        super(SqlType.SERIALIZABLE, new Class[]{});
    }

    public static SerializedElementCollectionPersister getInstance() {
        if (singleTon == null) {
            singleTon = new SerializedElementCollectionPersister();
        }
        return singleTon;
    }

    @Override
    public boolean isValidForField(Field field) {
        return field.getAnnotation(ElementCollection.class) != null;
    }
}`

This is then registered as a "custom persister" =>
DataPersisterManager.registerDataPersisters(SerializedElementCollectionPersister.getInstance())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant