CDI Injection Services in JPA Managed Facilities

I am sure that this is strongly related to this question , but the op on this question has a bit of a scenario that I am not sure even makes sense for the diatom so that’s what I understand, it’s usually not recommended to try to mix JPA Entity with CDI Bean because both are typically accomplished by creating proxy objects. Here is what I imagined, but from what I read, it is impossible.

@Entity
public class MyUniqueObject implements Serializable {

    @Inject
    private transient Logger log;

    @Inject
    private transient Event<MyUniqueObjectEvent> events;

    @Id
    private long id;

    @NotNull
    private String text;

    public void setText( final String text ) {
       log.debug( "updating text {}", this );
       this.text = text;
       events.fire( new MyUniqueObjectEvent( this ) ); // consumed by an @Observes method
    }
}

What is the best way to do what I'm trying to accomplish? which is mainly related to events originating from objects that support JPA, access to journal objects. Code examples are helpful.

+4
source share
1

, , . , " " ? CDI JPA 2.1 ,

  • @PrePersist
  • @PreRemove
  • @PostPersist
  • @PostRemove
  • @PreUpdate
  • @PostUpdate
  • @PostLoad

,

@EntityListeners(class=Audit.class)
@Entity
public class MyUniqueObject implements Serializable {}


public class Audit {

    @Inject
    private Logger log;

    @Inject
    private Event<MyUniqueObjectEvent> events;

}

- , , ( ) . , . , .

+3

Source: https://habr.com/ru/post/1526657/


All Articles