Background
[wp_ad_camp_1]
Software Environment
- Windows 7 Professional SP1
- Eclipse – Kepler Release
- Java 1.7 (1.7.0_67 – Windows x86)
- MySQL 5.6.16 – Community Server (GPL)
- MySQL Java Connector 5.1.34
- Batoo JPA 2.0.1.2
Create a Maven Project
First, create a maven project. You may follow the steps to do so on this article How to create Maven project in Eclipse.
Create a MySQL Database
Run the following DDL to create against an existing MySQL database. On this article, the database schema is named “turretadb”.
1 2 3 4 5 6 7 | CREATE TABLE IF NOT EXISTS person ( id int(11) NOT NULL AUTO_INCREMENT, lastname varchar(30) NOT NULL, firstname varchar(30) NOT NULL, middlename varchar(30) NOT NULL, PRIMARY KEY (id) ) ENGINE=InnoDB; |
Maven pom.xml Dependecies
Add the following dependencies in your pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | <dependencies> <dependency> <groupId>org.batoo.jpa</groupId> <artifactId>batoo-jpa</artifactId> <version>2.0.1.2</version> </dependency> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>7.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.34</version> </dependency> <dependency> <groupId>com.jolbox</groupId> <artifactId>bonecp</artifactId> <version>0.8.0.RELEASE</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.7</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.7</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.7</version> </dependency> </dependencies> |
The persistence.xml
[wp_ad_camp_2]
Take note of the namespase used in the XML. Without this, taboo will not be able to load the persistent unit “default” and fail upon create an EntityManagerFactory using Persistence.createEntityManagerFactory(“default”).
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?xml version="1.0" encoding="UTF-8"?> <persistence xmlns="http://java.sun.com/xml/ns/persistence"> <persistence-unit name="default" transaction-type="RESOURCE_LOCAL"> <provider>org.batoo.jpa.core.BatooPersistenceProvider</provider> <class>com.turreta.jpa.batoo.entity.Person</class> <properties> <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/turretadb" /> <property name="javax.persistence.jdbc.user" value="root" /> <property name="javax.persistence.jdbc.password" value="password" /> </properties> </persistence-unit> </persistence> |
Sample JPA Application
The file BatooAppSample.java contains the main method and can be ran from command line or Eclipse. Running the application from Eclipse is more convenient.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.EntityTransaction; import javax.persistence.Persistence; import com.turreta.jpa.batoo.entity.Person; public class BatooAppSample { public static void main(String[] args) { EntityManagerFactory emf = Persistence .createEntityManagerFactory("default"); EntityManager em = emf.createEntityManager(); EntityTransaction et = em.getTransaction(); et.begin(); Person person = new Person(); person.setFirstName("Karl"); person.setMiddleName("R"); person.setLastName("San Gabriel"); em.persist(person); et.commit(); em.close(); emf.close(); } } |
Sample Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | log4j:WARN Continuable parsing error 2 and column 82 log4j:WARN Document root element "log4j:configuration", must match DOCTYPE root "null". log4j:WARN Continuable parsing error 2 and column 82 log4j:WARN Document is invalid: no grammar found. log4j: reset attribute= "". log4j: Threshold ="". log4j: Level value for root is [trace]. log4j: root level set to TRACE log4j: Class name: [org.apache.log4j.ConsoleAppender] log4j: Parsing layout of class: "org.apache.log4j.PatternLayout" log4j: Setting property [conversionPattern] to [%d{yyyy/MM/dd HH:mm:ss} %-5p %c{1}:%L - %m%n]. log4j: Adding appender named [console] to category [root]. 2014/11/19 20:47:28 INFO PersistenceUnitInfoImpl:394 - Loading persistence.xml 2014/11/19 20:47:29 DEBUG ReflectHelper:200 - Loading direct access library.... 2014/11/19 20:47:29 DEBUG ReflectHelper:200 - Direct access library loaded successfully 2014/11/19 20:47:30 WARN BoneCPConfig:618 - releaseHelperThreads has been deprecated -- it tends to slow down your application more. 2014/11/19 20:47:30 DEBUG BoneCPDataSource:270 - JDBC URL = jdbc:mysql://localhost:3306/turretadb, Username = root, partitions = 1, max (per partition) = 5, min (per partition) = 1, idle max age = 60 min, idle test period = 240 min, strategy = DEFAULT 2014/11/19 20:47:32 DEBUG LinkManager:200 - Number of threads is 8 2014/11/19 20:47:32 DEBUG LinkManager:200 - Deployment pass took 7 msecs 2014/11/19 20:47:32 DEBUG LinkManager:200 - Number of threads is 8 2014/11/19 20:47:32 DEBUG LinkManager:200 - Deployment pass took 3 msecs 2014/11/19 20:47:32 DEBUG LinkManager:200 - Number of threads is 8 2014/11/19 20:47:32 DEBUG LinkManager:200 - Deployment pass took 1 msecs 2014/11/19 20:47:32 DEBUG DdlManager:200 - Number of threads is 8 2014/11/19 20:47:32 INFO MetamodelImpl:394 - Performing DDL operations for entity Person, mode NONE 2014/11/19 20:47:32 INFO MetamodelImpl:394 - Performing DDL operations for Person, mode NONE 2014/11/19 20:47:32 DEBUG DdlManager:200 - Deployment pass took 3 msecs 2014/11/19 20:47:32 DEBUG DdlManager:200 - Number of threads is 8 2014/11/19 20:47:32 DEBUG DdlManager:200 - Deployment pass took 1 msecs 2014/11/19 20:47:32 DEBUG IdQueue:200 - Ids will be fetched for BATOO_ID from the database... 2014/11/19 20:47:32 DEBUG NamedQueriesManager:200 - Number of threads is 8 2014/11/19 20:47:32 DEBUG NamedQueriesManager:200 - Deployment pass took 1 msecs 2014/11/19 20:47:32 DEBUG MetamodelImpl:200 - StaticMetamodel not present for com.turreta.jpa.batoo.entity.Person_ 2014/11/19 20:47:32 DEBUG ManagedInstance:200 - Instance status changing for ManagedInstance [session=Session1, type=Person, status=MANAGED, id=null]: MANAGED -> NEW 2014/11/19 20:47:32 DEBUG ManagedInstance:200 - Auto generating id values for ManagedInstance [session=Session1, type=Person, status=NEW, id=null] 2014/11/19 20:47:32 DEBUG ManagedInstance:200 - Cascading persist on ManagedInstance [session=Session1, type=Person, status=NEW, id=0] 2014/11/19 20:47:32 DEBUG SessionImpl:200 - Inspecting updated external entities on session Session1 2014/11/19 20:47:32 DEBUG SessionImpl:200 - Processing additions to the session Session1 2014/11/19 20:47:32 DEBUG SessionImpl:200 - Cascading removals on session Session1 2014/11/19 20:47:32 DEBUG SessionImpl:200 - Inspecting orphan on session Session1 2014/11/19 20:47:32 DEBUG SessionImpl:200 - Flushing session Session1 2014/11/19 20:47:32 DEBUG SessionImpl:200 - Flushing session Session1: updates 1, removals 0 2014/11/19 20:47:32 DEBUG SessionImpl:200 - Performing version upgrades on session Session1 2014/11/19 20:47:32 DEBUG SessionImpl:200 - Batch insert is being performed for Person with the size 1 2014/11/19 20:47:32 DEBUG SQL:200 - 1:0 executeQuery() ------------------ | SELECT NEXT_ID | | FROM BATOO_ID | | WHERE NAME = ? | | | | [BATOO_ID] | ------------------ 2014/11/19 20:47:32 DEBUG SQL:200 - 2:0 executeUpdate() ----------------------------------------- | INSERT INTO Person | | (id, middlename, lastname, firstname) | | VALUES | | (?, ?, ?, ?) | | | | [0, R, San Gabriel, Karl] | ----------------------------------------- 2014/11/19 20:47:32 DEBUG SQL:200 - 2:0 26 msecs, executeUpdate() 2014/11/19 20:47:32 DEBUG ManagedInstance:200 - Instance status changing for ManagedInstance [session=Session1, type=Person, status=NEW, id=0]: NEW -> MANAGED 2014/11/19 20:47:32 DEBUG ManagedInstance:200 - Flushing associations for instance ManagedInstance [session=Session1, type=Person, status=MANAGED, id=0] 2014/11/19 20:47:32 TRACE ManagedInstance:550 - Reset instance ManagedInstance [session=Session1, type=Person, status=MANAGED, id=0] 2014/11/19 20:47:32 TRACE ManagedInstance:550 - Snapshot generated for instance ManagedInstance [session=Session1, type=Person, status=MANAGED, id=0] 2014/11/19 20:47:32 DEBUG SessionImpl:200 - Flush successful for session Session1 2014/11/19 20:47:32 DEBUG SQL:200 - 1:0 63 msecs, executeQuery() 2014/11/19 20:47:32 INFO BoneCP:159 - Shutting down connection pool... 2014/11/19 20:47:32 DEBUG PoolWatchThread:96 - Terminating pool watch thread 2014/11/19 20:47:32 INFO BoneCP:188 - Connection pool has been shutdown. |
Download the Project
https://www.dropbox.com/s/thpov3m86vlxkb3/jpabatoosample.zip?dl=0
[wp_ad_camp_5]