Persistence in J2SE project

The big advantage of the EJB 3.0 is using entity beans out of container. This new feature will allow to test your business objects outside of application server. I would like to show how you can write simple J2SE project that uses EntityManager in NetBeans 5.0. Let's create new J2SE project in NetBeans.
  • We need to add persistence.xml file in META-INF directory in jar file. This can be simple done in NetBeans. Create new META-INF directory in src directory of your NetBeans project and add new persistence.xml into it. This file will be copied in correct place during building. In the properties.xml file we need to specify provider class name and persistence classes:
         <persistence xmlns="http://java.sun.com/xml/ns/persistence">
        <persistence-unit name="pu1">
            <!-- Provider class name is required in Java SE -->
            <provider>oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider</provider>
            <!-- All persistence classes must be listed -->
            <class>persistence.Product</class>
            <properties>
                <!-- Provider-specific connection properties -->
                <property name="jdbc.driver" value="com.mysql.jdbc.Driver"/>
                <property name="jdbc.connection.string" value="jdbc:mysql://localhost:3306/EJB"/>
                <property name="jdbc.user" value="blaha"/>
                <property name="jdbc.password" value="passw"/>
                <!-- Provider-specific settings -->
                <property name="toplink.logging.level" value="INFO"/>
            </properties>
        </persistence-unit>
    </persistence>
    
  • Add javaee.jar on project classpath. The jar is located in lib directory in Glassfish installation. Now, we can use persistence. The EntityManager need to be call first and then you can get EntityManager. See code snapshot:
    36 EntityManagerFactory emF = Persistence.createEntityManagerFactory("pu1");
    37         EntityManager em = emF.createEntityManager();
    38         EntityTransaction tx = em.getTransaction();
    39         tx.begin();
    40         Product product = new Product();
    41         product.setId(1);
    42         product.setPartNumber("ADSD-64464");
    43         product.setDescription("HAMMER");
    44         product.setPrice(12);
    45          em.persist(product); 
    46         tx.commit();
    47         
    48         // try to find product
    49         Query query = em.createQuery("select p from Product p where p.description =:descr");
    50         query.setParameter("descr","hammer");
    51         List results = query.getResultList();
    52         
    
  • Don't forget to add JDBC driver on project classpath.
  • The persistence projects must be run with -javaagent:${GLASSFISH_HOME}/lib/toplink-essentials-agent.jar. Add this option in project properties - Run - VM Options in NetBeans.
Comments:

Post a Comment:
Comments are closed for this entry.
About

pblaha

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
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
   
       
Today
Bookmarks