EJB‎ > ‎

Persistence

Persistence Context

  • Transaction-scoped
  • Extended - lives beyond a single transaction

Enterprise Java Beans, EJB, Persistence

persistence.xml example:

<persistence
xmlns="http://java.sun.com/xml/ns/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_1_0.xsd"
version="1.0">
<persistence-unit name="kr">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/krDB</jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
</properties>
</persistence-unit>
</persistence>

this file should be placed in META-INF folder.

Next, the data source which can be named in this format <file name>-ds.xml You can modify an example located in
<jboss home>/docs/examples/jca/ You should place it in the same deploy folder that your jar or war file is placed.

Then you need to download jdbc driver for the database you are using (e.g. mysql-connector-java-5.1.11-bin.jar) and place it in
<jboss home>/common/lib/

XML Mapping File

By default the persistence provider looks in the META-INF directory for a file name orm.xml, otherwise the mapping filename would be specified in the <mapping-file> element of persistence.xml.

Simple Primary Keys

For automatically generating the primary key use @GeneratedValue tag after @Id. Note that by default the persistence provider uses strategy AUTO for GeneratedValue, which means that the persistence provider will generate the Id automatically. To leave the key generation to the database which in some cases (i.e. when using MySQL replication) is necessary use the IDENTITY strategy.

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)

Here is an example of defining the primary key using an xml mapping file:

<entity-mappings>
    <entity class="info.compute.code.example" access="PROPERTY">
        <attributes>
            <id name="id">
                <generated-value strategy="IDENTITY">
            </id>
        </attributes>
    </entity>
</entity-mappings>

There are two other GeneratedValue strategies, TABLE and SEQUENCE. TABLE uses a user defined table to generate the keys and SEQUENCE is used with some specific RDBMs, specially Oracle, to utilize their efficient sequential ID generators.

Example of a SEQUENCE Mapping:

import javax.persistence.*;

@Entity
@Table(name="KR_TABLE")
@SequenceGenerator(name="KR_SEQUENCE", sequenceName="KR_SEQ")

public class Example implements java.io.Serializable {

    private long id;
 

    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="KR_SEQUENCE")
    public long getId( ) { return id; }
}

Composite Primary Keys

For composite primary keys, which are composed of multiple columns in a table, you first need to define a primary-key class with the following requirements:
  • Serializable
  • Contains a public no-argument constructor
  • Implements equals() and hashCode() methods
Note that key auto-generation is not supported with composite primary keys

Example of a primary-key class:
public class krPK implements java.io.Serializable {
private String lastName;
private long dlnum;

public krPK( ) {}

public krPK(String lastName, long dlnum)
{
this.lastName = lastName;
this.ssn = dlnum;
}

public String getLastName( ) { return this.lastName; }
public void setLastName(String lastName) { this.lastName = lastName; }

public long getDlnum( ) { return dlnum; }
public void setDlnum(long dlnum) { this.dlnum = dlnum; }

public boolean equals(Object obj)
{
      krPK pk = (krPK)obj;


if (obj == this)
            return true;

if (!(obj instanceof krPK))
            return false;

if ((!lastName.equals(pk.lastName)) || (dlnum != pk.dlnum))
            return false;


return true;
}

public int hashCode( )
{
return lastName.hashCode( ) + (int)dlnum;
}
}

Mapping Example:
import javax.persistence.*;

@Entity
@IdClass(krPK.class)
public class Example implements java.io.Serializable {
private String lastName;
private long dlnum;

@Id
public String getLastName( ) { return lastName; }
public void setLastName(String lastName) { this.lastName = lastName; }

@Id
public long getDlnum( ) { return dlnum; }
public void setDlnum(long dlnum) { this.dlnum = dlnum; }
}





Comments