Kbase P186818: How to insert a CLOB using JPA - Java Persistence API?
Autor |
  Progress Software Corporation - Progress |
Acesso |
  Público |
Publicação |
  4/29/2011 |
|
Status: Unverified
GOAL:
How to insert a CLOB using JPA - Java Persistence API?
GOAL:
How to setup OpenEdge Architect to run a JPA program?
GOAL:
How to insert an image in a Progress Database using JPA?
GOAL:
How to read a CLOB to create a new file using JPA?
FACT(s) (Environment):
Windows
OpenEdge 10.x
FIX:
The following steps will generate a JPA sample project in OpenEdge Architect:
- JARs needed to run a JPA application
a) Download Toplink from Oracle Website: http://www.oracle.com/technetwork/middleware/toplink/overview/index.html
- Select link "Download Oracle TopLink 11g Release 1 (11.1.1.1.0)" from EclipseLink Downloads - Oracle TopLink 11g
b) Download toplink essentials from same Oracle Website: http://www.oracle.com/technetwork/middleware/toplink/overview/index.html .
- Scroll down and select link "Download Oracle TopLink 10g (10.1.3.4.0) includes TopLink Essentials 2.0.1-b09d-fcs (12/06/2007)" from TopLink Essentials Downloads - Oracle TopLink 10g
c) Uncompress files to different directories toplink11 and toplink10
t-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">- Create a Progress database
a) Create an empty database (jpasample)
b) Using the Data Dictionary create a new table as follows:
name: myjpatable
column: myClobColumn, datatype Clob with max size to 1M
- Configure OpenEdge Architect to run a JPA application
1. Open OpenEdge Architect and Create a new Java Project - Project Name : JPA press Finish
2. Right Click on the new Project and select Properties->Java Build Path (You should have JRE System Library listed)
3. Press Add External JARs button and add the following JARs:
- From Toplink11 modules directory:
> javax.ejb_version.jar
> javax.persistence_1.0.0.0_1-0-2.jar
- From Toplink11 jlib directory:
> toplink.jar
- Froms Toplink10 jlib directory:
> toplink.essentials.jar
- From OpenEdge\java directory :
> openedge.jar
> progress.jar
4. Press OK
5. Right-click on src folder under your new project and select New->Folder. Name it : META-INF
6. Right-click on META-INF folder and select New-File. Name it : persistence.xml
7. Paste the following code and save it:
<?xml version="1.0" encoding="UTF-8" ?>
<persistence version="1.0" xmlns="http://java.sun.com/xml
s/persistence">
<persistence-unit name="jpasample" transaction-type="RESOURCE_LOCAL">
<provider>oracle.toplink..essentials.ejb.cmp3.EntityManagerFactoryProvider</provider>
<class>myjpapackage.sample.myjpatable</class>
<properties>
<property name="toplink.jdbc.driver" value="com.ddtek.jdbc.openedge.OpenEdgeDriver" />
<property name="toplink.jdbc.url" value="jdbc:datadirect:openedge://localhost:<dbport>;databaseName=jpasample" />
<property name="toplink.jdbc.user" value="<user>" />
<property name="toplink.jdbc.password" value="<password>" />
</properties>
</persistence-unit>
</persistence>
(Check for user and password in the database. Cannot be blank any of these values. If no user is created in the database then you need to use Windows user/password.)
8. Right-Click on src folder and select New->Package. Enter name : myjpapackage.sample
9. Right-click on the new package created and select New->Class. Enter name: myjpatable and press OK
10. Select all in the file and Copy the following code in the new class:
package myjpapackage.sample;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Column;
import javax.persistence.Lob;
@Entity
@Table(name = "myjpatable", schema = "pub")
public class myjpatable {
@Id
private byte[] myClobColumn;
@Lob
@Column(name="myclobcolumn")
public byte[] getMyClobColumn() {
return myClobColumn;
}
public void setMyClobColumn(byte[] myClobColumn) {
this.myClobColumn = myClobColumn;
}
}
11. Right-Click on src folder and select New->Class.
12. Click on "public static void main(String[] args) and enter name: Main. Press finish.
13. Select all in the file Main.java and paste the following code in it:
import java.io.*;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import myjpapackage.sample.myjpatable;
public class Main {
private static final String PERSISTENCE_UNIT_NAME = "jpasample";
private static EntityManagerFactory factory;
public static void main(String[] args) {
factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
EntityManager em = factory.createEntityManager();
// Insert Clob in the database.
em.getTransaction().begin();
myjpatable jpatable = new myjpatable();
try {
File file = new File("c:\\Winter.jpg");
jpatable.setMyClobColumn(getBytesFromFile(file));
} catch (IOException e) {
e.printStackTrace();
}
em.persist(jpatable);
em.getTransaction().commit();
em.close();
// Read the data from database and save it to a different file
String strFilePath = "C://mydemofile.jpg";
try
{
FileOutputStream fos = new FileOutputStream(strFilePath);
fos.write(jpatable.getMyClobColumn());
fos.close();
}
catch(FileNotFoundException ex)
{
System.out.println("FileNotFoundException : " + ex);
&nb.sp; }
catch(IOException ioe)
{
System.out.println("IOException : " + ioe);
}
}
public static byte[] getBytesFromFile(File file) throws IOException {
InputStream is = new FileInputStream(file);
long length = file.length();
if (length > Integer.MAX_VALUE) {
// File is too large
}
byte[] bytes = new byte[(int)length];
int offset = 0;
int numRead = 0;
while (offset < bytes.length
&& (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
offset += numRead;
}
if (offset < bytes.length) {
throw new IOException("Could not completely read file "+file.getName());
}
is.close();
return bytes;
}
}
14. Save your files, click on the Main.java file and run the project. Press Run button on toolbar
15. Your new image file should be saved in the new directory with file name provided in step 13.
16. Verify you created a record in table using sql explorer and running : select count(*) from pub.myjpatable;.