Skip to content

Instantly share code, notes, and snippets.

@pdemanget
Last active October 24, 2016 21:43
Show Gist options
  • Save pdemanget/6cc196504cebb69c494f to your computer and use it in GitHub Desktop.
Save pdemanget/6cc196504cebb69c494f to your computer and use it in GitHub Desktop.
JPA standalone
package com.viseo.cv.jpa;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
/*
* Class.forName("org.h2.Driver");
Connection conn = DriverManager.getConnection("jdbc:h2:~/test", "sa", "");
* jdbc:h2:mem:test
*
* @author user
*
*/
public class JpaMain {
EntityManagerFactory emf;
EntityManager em;
public void run() {
// EntityManagerFactory emf =
// Persistence.createEntityManagerFactory("myDbFile.odb");
// Another form of the createEntityManagerFactory method takes a map of
// persistence unit properties as a second parameter:
// connect();
connect();
Query query = em.createQuery("select a from Toto a");
query.getResultList();
}
private void connect1() {
emf = Persistence.createEntityManagerFactory("my-app");
em = emf.createEntityManager();
}
private void connect() {
Map<String, String> properties = new HashMap<String, String>();
// properties.put("javax.persistence.jdbc.user", "admin");
// properties.put("javax.persistence.jdbc.password", "admin");
emf = Persistence.createEntityManagerFactory(
"jdbc:h2:mem:test", properties);
}
public static void main(String[] args) {
new JpaMain().run();
}
}
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence persistence_1_0.xsd" version="1.0">
<persistence-unit name="my-app" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test"/>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
<!-- <property name="javax.persistence.jdbc.user" value="scott"/> -->
<!-- <property name="javax.persistence.jdbc.password" value="tiger"/> -->
</properties>
<class>com.viseo.cv.jpa.Toto</class>
</persistence-unit>
</persistence>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.5.1</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.178</version>
</dependency>
@pdemanget
Copy link
Author

Note: to inject in a container using JNDI, you will need a Context implementation and a Datasource implementation, and then inject it:

    System.setProperty (Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
    System.setProperty (Context.PROVIDER_URL, tmpFolder.toURI ().toURL ().toString ());
    final InitialContext ictx = new InitialContext ();
    final ComboPooledDataSource pooled = new ComboPooledDataSource ();
// pooled.setDriverClass ("com.mysql.jdbc.Driver");
    // pooled.setJdbcUrl
    // ("jdbc:mysql://localhost:3306/...");
    // pooled.setUser ("...");
    // pooled.setPassword ("...");

see http://stackoverflow.com/questions/12545129/setting-up-jndi-datasource-in-junit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment