问题
Hey guys, my goal is create an EntityManager using properties dependent on which database is in use. I've seen something like this done in all my Google searches(I made the code more basic for the purpose of this question):
@PersistenceUnit
private EntityManagerFactory emf;
private EntityManager em;
private Properties props;
@PostConstruct
public void createEntityManager(){
//if oracle set oracle properties else set postgres properties
emf = Persistence.createEntityManagerFactory("app-x");
em = emf.createEntityManager(props);
}
This works and I can load Oracle or Postgres properties successfully and I can Select from either database. HOWEVER, I am running into issues when doing INSERT statements. Whenever an INSERT is done I get a duplicate primary key exception.. every time! Can anyone shed some light on why this may be happening? Thanks -Brad
回答1:
In a container-managed environment, you can directly inject an EntityManager
:
To obtain an EntityManager instance, inject the entity manager into the application component:
@PersistenceContext EntityManager em;
If you need to deal with different persistence units (and thus several EntityManager
instances), declare them in the persistence.xml and get the right EntityManager
injected by its name:
@PersistenceContext(unitName = "MyFirstPU")
EntityManager em;
Update: According to Specifying the Database (and also mentioned this blog post), EclipseLink may be able to auto-detect the database platform and the eclipselink.target-database
is optional:
If you are using the default persistence provider, the provider attempts to automatically detect the database type based on the connection metadata.
If this works with Oracle and PostgreSQL (and my understanding is that it should), the customer would only have to setup a datasource which is IMO the ideal scenario.
回答2:
Annotate your EntityManager
with @PersistenceContext(unitName="app-x")
Thus you will not need to create new entity managers and factories - everything is automatically handled by your container.
来源:https://stackoverflow.com/questions/2405916/use-of-entitymanagerfactory-causing-duplicate-primary-key-exceptions