jpa-spec
  1. jpa-spec
  2. JPA_SPEC-24

Possibility to inject an EntityManager into an Entity

    Details

    • Type: Improvement Improvement
    • Status: Closed
    • Priority: Major Major
    • Resolution: Works as designed
    • Affects Version/s: None
    • Fix Version/s: None
    • Labels:
      None

      Description

      Hello Group,

      we are currently confronted with the following scenario:
      A legacy database schema has a special table that holds Table-names and associated IDs.
      This table's data differs from one database instance to the other.
      In order to write data to any other table of the database, this table's ID is needed -> look-up in the ID-table.
      To reflect this behavior in a JPA-mapped environment, we wanted each entity to look up its ID in the ID-table before persisting.
      But therefore we'd need a reference to an EntityManager from within an Entity.
      Our solution: make it possible to inject an EntityManager into an Entity.

      Best Regards,

      Andreas Bauer

        Activity

        AndreasBauer created issue -
        Hide
        Mark Struberg added a comment -

        If you really go down this route, then expect a few more problems. Doing a select in a @PrePersiste might lead to a flush() which in turns changes your EM. So what you end up is (implicitly) trying to change your current transaction while you're at committing it...

        Imo the only sane usage of having a (manually accessible) EntityManager inside an Entity is when it writes to a completely different database (PU). I've seen this for doing changeLog tracking (but this is actually not much superior than a native transaction log). Operating on the same PU will in most cases end up being non-portable ... if you ever manage to survive he operation without Exception on your system.

        Show
        Mark Struberg added a comment - If you really go down this route, then expect a few more problems. Doing a select in a @PrePersiste might lead to a flush() which in turns changes your EM. So what you end up is (implicitly) trying to change your current transaction while you're at committing it... Imo the only sane usage of having a (manually accessible) EntityManager inside an Entity is when it writes to a completely different database (PU). I've seen this for doing changeLog tracking (but this is actually not much superior than a native transaction log). Operating on the same PU will in most cases end up being non-portable ... if you ever manage to survive he operation without Exception on your system.
        Hide
        ldemichiel added a comment -

        Well, in Java EE environments, the lifecycle callbacks share the naming context of the calling component, so I suppose you could look up the entity manager in JNDI anyway. That said, the consensus of the group has been that injection of an entity manager into an entity should not be supported. I am therefore closing out this issue.

        Show
        ldemichiel added a comment - Well, in Java EE environments, the lifecycle callbacks share the naming context of the calling component, so I suppose you could look up the entity manager in JNDI anyway. That said, the consensus of the group has been that injection of an entity manager into an entity should not be supported. I am therefore closing out this issue.
        ldemichiel made changes -
        Field Original Value New Value
        Status Open [ 1 ] Closed [ 6 ]
        Resolution Works as designed [ 7 ]

          People

          • Assignee:
            Unassigned
            Reporter:
            AndreasBauer
          • Votes:
            1 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: