Last active
October 24, 2016 21:43
-
-
Save pdemanget/6cc196504cebb69c494f to your computer and use it in GitHub Desktop.
JPA standalone
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note: to inject in a container using JNDI, you will need a Context implementation and a Datasource implementation, and then inject it:
see http://stackoverflow.com/questions/12545129/setting-up-jndi-datasource-in-junit