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
In a container-managed environment, you can directly inject an
EntityManager
:If you need to deal with different persistence units (and thus several
EntityManager
instances), declare them in thepersistence.xml
and get the rightEntityManager
injected by its name: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 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.