【JavaEE】The implement of JPA by "ElipseLink"
来源:互联网 发布:淘宝一个月能赚多少钱 编辑:程序博客网 时间:2024/05/29 09:04
For recently, i had done some research for JavaEE, such as JSF & CDI & EJB & JPA, and for yesterday i did research on how to build JPA on the project i was using.By studing on some books, today i build a simple framework for JPA by 'eclipseLink', you know "ITOO" is using the EclipseLink to implete JPA. So let's do it!
1.Download the "EclipseLink Installer Zip" implementatio.
click this link to login in the "EclipseLink Download Site" (http://www.eclipse.org/eclipselink/downloads/).
Then download contains serveral jars.we need the following jars:
*eclipselink.jar
*javax.persistence_*.jar
2.Download Derby as database
For this article i will use Apache Derby as a database. Download Derby from (http://db.apache.org/derby/). From this blog we will need the "derby.jar". For details on Derby (which is not required for this tutorial) please see http://www.vogella.com/articles/ApacheDerby/article.html.
3.create a project
create a project and entity,create folder "lib" then place the required JPA jars and derby.jar into this folder and add the libs to the project classpath.
As follows show:
when you add the libs to the project classpath, like this:
4.create package "de.vogella.jpa.simple.model" and create the following classes.
package de.vogella.jpa.simple.model;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;@Entitypublic class Todo { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String summary; private String description; public String getSummary() { return summary; } public void setSummary(String summary) { this.summary = summary; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } @Override public String toString() { return "Todo [summary=" + summary + ", description=" + description + "]"; }}As above code shows, there's three annotation '@Entity','@Id','@GeneratedValue' to instead the xml configfile, besides for this example i rewrite the toString() method, and this is necessary for Entity.
5.create "persistence.xml"
create a directory "META-INF" in "src" folder and create file "persistence.xml", then use EclipseLink specific flags for example via the parameter "eclipselink.ddl-generation" you specify that the database scheme will be automaticalluy dropped and created.
<?xml version="1.0" encoding="UTF-8" ?><persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"> <persistence-unit name="todos" transaction-type="RESOURCE_LOCAL"> <class>de.vogella.jpa.simple.model.Todo</class> <properties> <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver" /> <property name="javax.persistence.jdbc.url" value="jdbc:derby:/home/vogella/databases/simpleDb;create=true" /> <property name="javax.persistence.jdbc.user" value="test" /> <property name="javax.persistence.jdbc.password" value="test" /> <!-- EclipseLink should create the database schema automatically --> <property name="eclipselink.ddl-generation" value="create-tables" /> <property name="eclipselink.ddl-generation.output-mode" value="database" /> </properties> </persistence-unit></persistence>
Addition: (but for dukes-bookstore there is no properties like jdbc etc)
for next passage, i will introduce the details configuration for this persistence.xml.
6.test your installation
Create the following Main class which will create a new entry every time it is called. After the first call you need to remove the property "eclipselink.ddl-generation" from persistence.xml otherwise you will receive an error as EclipseLink tries to create the database scheme again. Alternative you could set the property to "drop-and-create-tables" but this would drop your database schema at every run.(pay attention!!! dukes use 'drop-and-create-tables')
package de.vogella.jpa.simple.main;import java.util.List;import javax.persistence.EntityManager;import javax.persistence.EntityManagerFactory;import javax.persistence.Persistence;import javax.persistence.Query;import de.vogella.jpa.simple.model.Todo;public class Main { private static final String PERSISTENCE_UNIT_NAME = "todos"; private static EntityManagerFactory factory; public static void main(String[] args) { factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME); EntityManager em = factory.createEntityManager(); // read the existing entries and write to console Query q = em.createQuery("select t from Todo t"); List<Todo> todoList = q.getResultList(); for (Todo todo : todoList) { System.out.println(todo); } System.out.println("Size: " + todoList.size()); // create new todo em.getTransaction().begin(); Todo todo = new Todo(); todo.setSummary("This is a test"); todo.setDescription("This is a test"); em.persist(todo); em.getTransaction().commit(); em.close(); }}there i use EntityManagerFactory to create a instance of EntityManager,for this case the 'todos' is the 'persistence-unit' 's name.later I use the em.createQuery() to execute the JPQL which like the HQL in Hibernate.
Then i print the size of the result of the Query.
Finally i insert an record to the database using the EclipseLink.
Addition: when you execute this method for the second time, you should Commented out the code in the "persistence.xml"
<property name="eclipselink.ddl-generation" value="create-tables" />
So, this's the result i execute it:
The JPA was impleted by the EclipseLink successfully!!!
That's all.
- 【JavaEE】The implement of JPA by "ElipseLink"
- Implement Salesforce Outbound Message by JavaEE
- the implement of system function.
- 【JavaEE】The Introduce of 'JSF'
- JavaEE-JPA
- 【JavaEE】Vincent's Understand of JPA(XMind Picture)
- The implement of Binary Search Tree (JAVA)
- The first step of JavaEE[1]
- The first step of JavaEE[2]
- The first step of JavaEE[3]
- The first step of JavaEE[4]
- The first step of JavaEE[5]
- The first step of JavaEE[6]
- The first step of JavaEE[7]
- How to implement the built in effects of DirectX.DirectSound
- A simple implement of the Tetris in JAVA
- [LinkedIn面试]implement the take() and put() of blocking queue
- how to implement the version control of static files
- NDK使用之HelloWorld
- [PAT-乙级]1006.换个格式输出整数
- 介绍一下Unity5的AssetBundle
- 应用程序签名
- AdaBoost算法在文本分类中的总结
- 【JavaEE】The implement of JPA by "ElipseLink"
- 在恢复管理器(RMAN)创建恢复目录测试(解决报错'对表空间 XX无权限'和'名称已由现有对象使用')
- PAT_1049. Counting Ones
- Facebook为母亲节再度推出紫色小花Reaction按钮
- C++实现简易通讯录系统
- node入门(一)--内置模块
- 第6章 用例
- 超详细的Python实现新浪微博模拟登陆(小白都能懂)
- 百度地图API的简单应用