Is it ok to put list or map of embeddable objects in entity ?

#1

Hi,

Is it ok to put list or map of embeddable objects in entity? I mean, for example :

@Entity
public class Entity_A {
    (...)

    @Embedded
    List<Class_B> bList;

    @Embedded
    Map<String, Class_C> cMap;

    (...)
}

where Class_B and Class_C are annotated as @Embeddable ? In the examples, there is only situation where there is one single instance of embeddable class in entity. And what happens if I do operation like :

entA.setBList(new ArrayList<Class_B>())

on an entity that had some Class_B instances in bList? Will they be deleted correctly from database?

 

And one more question - I have some entities that have my own, serializable classes inside as parameters. When I try to inspect them with explorer, there is an error saying "Failed to deserialize - type X not found". I found an option to put a classpath for persistence objects and metadata, where I put path to jar file containing these classes, but that doesn't help.

 

Thanks in advance for help.

#2

Collections and maps of any persistable type, including embeddable types are supported. You may omit the @Embedded annotation (I am not sure it could be used with collections).

A collection of embedded objects is always stored as part of the containing entity. Since there is no separate space allocation for the collection or its elements - every time the entity object is stored previous content is automatically deleted.

To see serialzable objects in the Explorer try to add the classpath to the JVM when you run the Explorer. But actually it is recommended to avoid relying on Java serialization and instead to define each of these serializable classes also as embeddable.

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)
#3

Ok, thanks for your answer. I have another question though.

I have entity EntityA like that

@Entity
public class EntityA implements Serializable {
   (...)
   List<EmbeddableB> embeddableBList;
}

EmbeddableB class :

@Embeddable
public class EmbeddableB implements Serializable {
   (...)
   @OneToMany(cascade = CascadeType.ALL)
   List<EntityC> entityCList;
}

Where EntityC is some another simple entity.

 

My problem is - when i remove EntityA instance I would like to have removed all EntityB classes also (which are part of EmbeddableB with CascadeType.ALL). And that's not happening... Is that a bug or are cascade operations not supported with Embeddable classes ?

#4

One more thing I discovered recently. When I load instance of EntityA and work with it, the first time I access any item from embeddableBList it take several ms, even ~ 500ms in some cases. As like embeddableBList is lazily loaded?

 

#5

Maybe you can try:

@Entity
public class EntityA implements Serializable {
   (...)
    @OneToMany(cascade = CascadeType.ALL)
    List<EmbeddableB> embeddableBList;
}

It is not portable JPA but somehow you have to tell ObjectDB that this field is included in cascading (scanning all the fields would not be very efficient). I am not sure how ORM JPA implementations handle this.

About the slow lazy loading - a test case that demonstrates it is needed to evaluate this.

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)

Post Reply

To post a reply and/or subscribe to update notifications - please login