Skip to content

Commit 9b6c3d6

Browse files
author
Vitalii
committed
example of using ORM EclipseLink
1 parent 39046bc commit 9b6c3d6

File tree

10 files changed

+387
-0
lines changed

10 files changed

+387
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Manifest-Version: 1.0
2+
Class-Path:
3+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
3+
<display-name>EclipseLinkExample</display-name>
4+
<servlet>
5+
<description>
6+
</description>
7+
<display-name>main</display-name>
8+
<servlet-name>main</servlet-name>
9+
<servlet-class>
10+
web.main</servlet-class>
11+
</servlet>
12+
<servlet-mapping>
13+
<servlet-name>main</servlet-name>
14+
<url-pattern>/main</url-pattern>
15+
</servlet-mapping>
16+
<welcome-file-list>
17+
<welcome-file>index.html</welcome-file>
18+
<welcome-file>index.htm</welcome-file>
19+
<welcome-file>index.jsp</welcome-file>
20+
<welcome-file>default.html</welcome-file>
21+
<welcome-file>default.htm</welcome-file>
22+
<welcome-file>default.jsp</welcome-file>
23+
</welcome-file-list>
24+
</web-app>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<persistence version="1.0"
3+
xmlns="http://java.sun.com/xml/ns/persistence"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
6+
7+
<persistence-unit name="entity_example">
8+
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
9+
10+
<class>example.eclipselink.ExampleEntity</class>
11+
<properties>
12+
<property name="eclipselink.jdbc.driver" value="org.firebirdsql.jdbc.FBDriver" />
13+
<property name="eclipselink.jdbc.url" value="jdbc:firebirdsql://localhost:3050/d:/jpa_example.gdb?sql_dialect=3" />
14+
<!-- I work in this example without user / password.-->
15+
<property name="eclipselink.jdbc.user" value="SYSDBA" />
16+
<property name="eclipselink.jdbc.password" value="masterkey" />
17+
18+
<!-- EclipseLink should create the database schema automatically -->
19+
20+
<!-- property name="eclipselink.ddl-generation" value="drop-and-create-tables" / -->
21+
<property name="eclipselink.ddl-generation.output-mode" value="database" />
22+
</properties>
23+
24+
</persistence-unit>
25+
</persistence>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package example.eclipselink;
2+
import java.io.Serializable;
3+
4+
import javax.persistence.*;
5+
6+
@Entity
7+
public class ExampleEntity implements Serializable{
8+
private final static long serialVersionUID=1L;
9+
10+
@Id
11+
private int id;
12+
@Transient
13+
private int tempValue;
14+
private String name;
15+
private String description;
16+
17+
18+
public int getId() {
19+
return id;
20+
}
21+
public void setId(int id) {
22+
this.id = id;
23+
}
24+
public int getTempValue() {
25+
return tempValue;
26+
}
27+
public void setTempValue(int tempValue) {
28+
this.tempValue = tempValue;
29+
}
30+
public String getName() {
31+
return name;
32+
}
33+
public void setName(String name) {
34+
this.name = name;
35+
}
36+
public String getDescription() {
37+
return description;
38+
}
39+
public void setDescription(String description) {
40+
this.description = description;
41+
}
42+
43+
44+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package example.eclipselink;
2+
3+
import java.util.HashMap;
4+
5+
import javax.persistence.EntityManager;
6+
import javax.persistence.EntityManagerFactory;
7+
import javax.persistence.Persistence;
8+
import javax.persistence.Query;
9+
10+
public class Main {
11+
12+
public static void main(String[] args){
13+
System.out.println("begin");
14+
Main main=new Main();
15+
16+
System.out.println("-end-");
17+
}
18+
19+
private static final String PERSISTENCE_UNIT_NAME = "entity_example";
20+
private EntityManagerFactory factory;
21+
22+
public Main(){
23+
factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME, new HashMap());
24+
// PersistenceUnitProperties.JDBC_DRIVER
25+
EntityManager em = factory.createEntityManager();
26+
em.getTransaction().begin();
27+
Query q = em.createQuery("select m from ExampleEntity m");
28+
29+
em.close();
30+
factory.close();
31+
}
32+
33+
/** ïðèìåð çàïèñè îáúåêòà ÷åðåç JPA */
34+
private void write(ExampleEntity example){
35+
36+
}
37+
38+
/** ïðèìåð ÷òåíèÿ îáúåêòà ÷åðåç JPA */
39+
private ExampleEntity read(int key){
40+
return null;
41+
}
42+
43+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package web;
2+
3+
import java.io.IOException;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
7+
import javax.persistence.EntityManager;
8+
import javax.persistence.EntityManagerFactory;
9+
import javax.persistence.Persistence;
10+
import javax.persistence.Query;
11+
import javax.servlet.ServletException;
12+
import javax.servlet.ServletOutputStream;
13+
import javax.servlet.http.HttpServlet;
14+
import javax.servlet.http.HttpServletRequest;
15+
import javax.servlet.http.HttpServletResponse;
16+
17+
import example.eclipselink.ExampleEntity;
18+
19+
/**
20+
* Servlet implementation class main
21+
*/
22+
public class main extends HttpServlet {
23+
private static final long serialVersionUID = 1L;
24+
25+
26+
private static final String PERSISTENCE_UNIT_NAME = "entity_example";
27+
private EntityManagerFactory factory= null;
28+
29+
/**
30+
* @see HttpServlet#HttpServlet()
31+
*/
32+
public main() {
33+
super();
34+
}
35+
36+
@Override
37+
public void init() throws ServletException {
38+
super.init();
39+
try{
40+
factory= Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME, new HashMap());
41+
}catch(Exception ex){
42+
System.err.println("ERROR: "+ex.getMessage());
43+
ex.printStackTrace(System.err);
44+
};
45+
}
46+
47+
/**
48+
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
49+
*/
50+
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
51+
doProcess(request, response);
52+
}
53+
54+
/**
55+
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
56+
*/
57+
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
58+
doProcess(request, response);
59+
}
60+
61+
private void doProcess(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
62+
ServletOutputStream out=response.getOutputStream();
63+
out.println("<html>");
64+
out.println("<body>");
65+
// PersistenceUnitProperties.JDBC_DRIVER
66+
if(factory==null){
67+
factory= Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME, new HashMap());
68+
}
69+
EntityManager em = factory.createEntityManager();
70+
em.getTransaction().begin();
71+
Query q = em.createQuery("select m from ExampleEntity m");
72+
73+
if(q.getResultList().size()==0){
74+
// create object
75+
out.println("<b> create object begin </b>");
76+
ExampleEntity example=new ExampleEntity();
77+
example.setId(3);
78+
example.setName("Test1");
79+
example.setDescription("this is temp Entity");
80+
example.setTempValue(1);
81+
em.persist(example);
82+
out.println("<br>");
83+
out.println("<b>create object DONE </b>");
84+
}else{
85+
// output object
86+
List list=q.getResultList();
87+
for(int counter=0;counter<list.size();counter++){
88+
out.println(counter+" : "+list.get(counter).getClass().getName());
89+
out.println("<br>");
90+
}
91+
}
92+
em.getTransaction().commit();
93+
out.println("DONE");
94+
out.println("</body></html>");
95+
}
96+
97+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<persistence version="1.0"
3+
xmlns="http://java.sun.com/xml/ns/persistence"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
6+
7+
<persistence-unit name="entity_example">
8+
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
9+
10+
<class>example.eclipselink.ExampleEntity</class>
11+
<properties>
12+
<property name="eclipselink.jdbc.driver" value="org.firebirdsql.jdbc.FBDriver" />
13+
<property name="eclipselink.jdbc.url" value="jdbc:firebirdsql://localhost:3050/d:/jpa_example.gdb?sql_dialect=3" />
14+
<!-- I work in this example without user / password.-->
15+
<property name="eclipselink.jdbc.user" value="SYSDBA" />
16+
<property name="eclipselink.jdbc.password" value="masterkey" />
17+
18+
<!-- EclipseLink should create the database schema automatically -->
19+
20+
<property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
21+
<property name="eclipselink.ddl-generation.output-mode" value="database" />
22+
</properties>
23+
24+
</persistence-unit>
25+
</persistence>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<persistence version="1.0"
3+
xmlns="http://java.sun.com/xml/ns/persistence"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
6+
7+
<persistence-unit name="entity_example">
8+
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
9+
10+
<class>example.eclipselink.ExampleEntity</class>
11+
<properties>
12+
<property name="eclipselink.jdbc.driver" value="org.firebirdsql.jdbc.FBDriver" />
13+
<property name="eclipselink.jdbc.url" value="jdbc:firebirdsql://localhost:3050/d:/jpa_example.gdb?sql_dialect=3" />
14+
<!-- I work in this example without user / password.-->
15+
<property name="eclipselink.jdbc.user" value="SYSDBA" />
16+
<property name="eclipselink.jdbc.password" value="masterkey" />
17+
18+
<!-- EclipseLink should create the database schema automatically -->
19+
20+
<!-- property name="eclipselink.ddl-generation" value="drop-and-create-tables" / -->
21+
<property name="eclipselink.ddl-generation.output-mode" value="database" />
22+
</properties>
23+
24+
</persistence-unit>
25+
</persistence>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package example.eclipselink;
2+
import java.io.Serializable;
3+
4+
import javax.persistence.*;
5+
6+
@Entity
7+
public class ExampleEntity implements Serializable{
8+
private final static long serialVersionUID=1L;
9+
10+
@Id
11+
private int id;
12+
@Transient
13+
private int tempValue;
14+
private String name;
15+
private String description;
16+
17+
18+
public int getId() {
19+
return id;
20+
}
21+
public void setId(int id) {
22+
this.id = id;
23+
}
24+
public int getTempValue() {
25+
return tempValue;
26+
}
27+
public void setTempValue(int tempValue) {
28+
this.tempValue = tempValue;
29+
}
30+
public String getName() {
31+
return name;
32+
}
33+
public void setName(String name) {
34+
this.name = name;
35+
}
36+
public String getDescription() {
37+
return description;
38+
}
39+
public void setDescription(String description) {
40+
this.description = description;
41+
}
42+
43+
44+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package example.eclipselink;
2+
3+
import java.util.HashMap;
4+
import java.util.List;
5+
6+
import javax.persistence.EntityManager;
7+
import javax.persistence.EntityManagerFactory;
8+
import javax.persistence.Persistence;
9+
import javax.persistence.Query;
10+
11+
public class Main {
12+
13+
public static void main(String[] args){
14+
System.out.println("begin");
15+
Main main=new Main();
16+
17+
System.out.println("-end-");
18+
}
19+
20+
private static final String PERSISTENCE_UNIT_NAME = "entity_example";
21+
private EntityManagerFactory factory;
22+
23+
public Main(){
24+
factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME, new HashMap());
25+
// PersistenceUnitProperties.JDBC_DRIVER
26+
EntityManager em = factory.createEntityManager();
27+
em.getTransaction().begin();
28+
Query q = em.createQuery("select m from ExampleEntity m");
29+
if(q.getResultList().size()>0){
30+
ExampleEntity example=read(em, 3);
31+
System.out.println("Read OK:"+example.getName()+" "+example.getId());
32+
}else{
33+
ExampleEntity example=new ExampleEntity();
34+
example.setId(4);
35+
example.setName("name_4");
36+
example.setDescription("description_4");
37+
example.setTempValue(5);
38+
write(em, example);
39+
System.out.println("Write OK:"+example.getName()+" "+example.getId());
40+
}
41+
em.close();
42+
factory.close();
43+
}
44+
45+
/** ïðèìåð çàïèñè îáúåêòà ÷åðåç JPA */
46+
private void write(EntityManager manager, ExampleEntity example){
47+
manager.persist(example);
48+
}
49+
50+
/** ïðèìåð ÷òåíèÿ îáúåêòà ÷åðåç JPA */
51+
private ExampleEntity read(EntityManager manager, int key){
52+
Query q = manager.createQuery("select m from ExampleEntity m where m.id="+key);
53+
List list=q.getResultList();
54+
return (ExampleEntity)list.get(0);
55+
}
56+
57+
}

0 commit comments

Comments
 (0)