Configuring Persistence Unit Definition

  1. The EntityManager in the PersistentCalculatorBean.java source (see the previous section), uses the name "calculator" to identify the persistence unit to use:

    @PersistenceContext(unitName = "calculator")
    private EntityManager manager;
    								

    This persistence unit is defined in a new file called persistence.xml, in the src/META-INF folder of the Calculator project.

    Select File > New > Other... from the Eclipse menu.

    Browse to General > File and click the Next button.

    New file

    In the New File dialog, enter the following:

    The Enter or select the parent folder field: Calculator/src/META-INF

    The File name field: persistence.xml

    Click the Finish button.

    New file - Step 2

  2. Switch to the Source tab of the file editor and add the persistence.xml file contents as follows:

    
    <?xml version="1.0" encoding="UTF-8"?>
    
    <persistence>
    	<persistence-unit name="calculator">
    		<jta-data-source>java:/DefaultDS</jta-data-source>
    		<properties>
    			<property name="hibernate.hbm2ddl.auto" value="create-drop" />
    		</properties>
    	</persistence-unit>
    </persistence>
    
    								

    The persistence.xml file makes a link between the persistence unit and the datasource. JBoss comes with the Hypersonic SQL database embedded within it and a default datasource available in JNDI under java:/DefaultDS.

    The persistence configuration says to create new tables each time the application is deployed and to drop tables when it's undeployed:

    
    <property name="hibernate.hbm2ddl.auto" value="create-drop" />
    								
    								

    This means that your data will be lost each time you redeploy your application.

Professional hosting     Belorussian informational portal         Free SCWCD 1.4 Study Guide     Free SCDJWS 1.4 Study Guide     SCDJWS 1.4 Quiz     Free IBM Certified Associate Developer Study Guide     IBM Test 000-287. Enterprise Application Development with IBM WebSphere Studio, V5.0 Study Guide     Free Mock Exam Engine