How to access EntityManager inside Entity class in EJB3

I need to execute a db query inorder to set the extra column "The order by column" in a many to many association table. so I need to access the db sequence from inside the Entity class and and select the nextval of the sequence and assign it to the order_by column in @prepersist lifecycle callback method.

@Entity public class ProductWishlist implements Serializable { .... @Column(name="ORDER_BIT") private long orderBit; // getter setter // ....... @Prepersist public void setOrderBit(EntityManager entityManager) { Query q=entityManager.createNativeQuery("select nextval('SHP_PRODUCTS_PICS_ORDER_SEQ')"); Long order=(Long)q.getResultList().get(0); this.setOrderBit(order); } }

HOw can I access entitymanger from within setOrderBit ()? how can i Pass Entitymanager into it? Or How can i execute native query inside an Entity class?


Injecting EntityManager in entity bean isn't recommended. In my view, the entity bean acting as POJO is meant for data transmission between layers or network.

Its better to pre-populate entity, data manipulation prior persistence. But some validation on attributes or formatting of data can be done within entity callback methods.

Here, you can try using entity listener by applying @EntityListeners annotation on entity, which gets notified upon entity lifecycle callback method.


You should not use EntityManager in pre-persist (or in lifecycle methods in general), because it is not aloud according specification:

In general, the lifecycle method of a portable application should not invoke EntityManager or Query operations, access other entity instances, or modify relationships within the same persistence context.[43] A lifecycle callback method may modify thepost persist non-relationship state of the entity on which it is invoked.

[43] The semantics of such operations may be standardized in a future release of this specification.

Just obtain normal JDBC connection and use it to execute query.


  • Dynamic module importing is trying to do relative imports when it shouldn't
  • import tensorflow as tf gives errors even after installing without errors
  • Stop-AzureVM does not shutdown my Azure-VM (Runbook)
  • Custom “cellfilter” in Angular js
  • Converting VBScript's Imp Operator
  • ActiveMQ JMS XA Atomikos - Transaction not started error
  • How to solve singular matrices?
  • What are all the ways to import modules in Python?
  • Python's imp.reload() function is not working?
  • Removing Small Regions from a Binary Image
  • ImageJ jar file plugin shortcut creation - More help needed
  • Python: Sort nested dictionary by value
  • Django mod-python error
  • VBA Filter Table and Copy Subset of Resulting Columns to Clipboard
  • Batch - Search for part/exact name and copy line from text file into batch as var
  • Slickgrid Grouped header ShowTotal
  • Getter and Setter conventions in go lang
  • SWIG ImportError: undefined symbol: _Py_RefTotal
  • In regards to the difference between Java “Properties” and C#' Properties
  • Restore session after restart in JBoss
  • Java getNodeName and namespaces
  • How to monitor transaction isolation level changes in SQL Profiler or in any other tool
  • Passing and ArrayList through intent
  • How to add an object in my collection by only using add method? [closed]
  • Serializing socket
  • Validity Method for Reference Classes
  • Does anyone have a Categorized XML Corpus Reader for NLTK?
  • Using an enum contained in a Cloud Endpoint model on a Android client
  • Type safe keys with Entity Framework model
  • What's wrong with my datatrigger binding?
  • Eloquent paginate function in Slim 3 project using twig
  • Groovy: Unexpected token “:”
  • Do I need to reset a Perl hash index?
  • Replace value with Factor in r data.table
  • How to access EntityManager inside Entity class in EJB3
  • Repeat a vertical line on every page in Report Builder / SSRS
  • EntityFramework adding new object to nested object collection
  • Checking variable from a different class in C#
  • failed to connect to specific WiFi in android programmatically
  • How can I use threading to 'tick' a timer to be accessed by other threads?