question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is it possible to have the same classes be @Entity and @Embedded? (2.0.2)

See original GitHub issue

I am storing certain objects as both separate entities in a collection and embedded within other entities in different collections (for caching, basically). Prior to 2.0 I have been using the same class for both without any issues, however in 2.0 it is causing various issues.

Now that @Embedded has to be used on types, the constraints don’t let me use both @Embedded and @Entity on the same class, and they also neither allow an @Embedded class with an @Id property, nor they allow an @Entity class without one.

The structure I have is kind of like this (actually there is a base class for Job to make this possible but just to have a shorter example):

@Entity("job")
@Indexes(Index(fields = [Field("shifts.startTime", type = IndexType.ASC)]))
class Job(
    @Id var id: ObjectId,
    var shifts: List<Shift>,
)

class Shift(
    @Id var id: ObjectId,
    var startTime: Date,
)

// and also
@Entity("shiftAndJob")
class ShiftAndJob(
    var shift: Shift,
    var job: Job,
)
  • Mapping fails with a ValidationException on the shifts.startTime index, it’s not being found.

  • If I try to retrieve a Job entity, the operation fails with org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class Shift.

  • Validations fail when trying to query ShiftAndJob for e.g. job.shifts or shift.startTime

So basically I want to use the Job class here as both an embedded object and its own separate entity and I can’t figure out what’s the way to do so in 2.0 (in 1.* it all just worked regardless of @Embedded), are there any “best practices” on how to achieve this without cloning the classes altogether or using too much inheritance?

Driver Version: 4.0.5
Morphia Version: 2.0.2

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
evanchoolycommented, Oct 31, 2020

You can just use @Entity on the type and then also embed. The annotations are there to mark which classes to map and which set of validations to apply, to perhaps oversimplify things a bit. But an entity can also be embedded at that point because it’s already mapped. I believe there are some unit tests that do just that.

0reactions
DefiantCatgirlcommented, Oct 31, 2020

Yes, it’s working fine, thank you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

JPA 2.0: Embedded object exclusive to one entity
Is there a possibility in JPA 2.0 to asure that an embedded object is embedded with only one object, but not several? In...
Read more >
Java Persistence/Embeddables - Wikibooks
AdvancedEdit. Embedding more than one Instance of the same embeddable TypeEdit. Instances of an embeddable object can be embedded in the same parent...
Read more >
Chapter 2. Mapping Entities - Red Hat on GitHub
It is possible to declare an embedded component inside an entity and even override its column mapping. Component classes have to be annotated...
Read more >
Named query in Entity Bean - Problem with embedded class
Hello Forum, I'm trying to set up a named query in my Entity Bean and I'm unable to get it up and running...
Read more >
Embedded abstract class not working with 2.0.0? · Issue #1491
How can I use abstract classes properly with 2.0.0 I used to have ... work on the previous version: @Entity public class Root...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found