15 jpa

43
Professional Open Source™ © JBoss, Inc. 2003, 2004. 1 07/17/04 JPA

Upload: thirumuru2012

Post on 18-Dec-2014

260 views

Category:

Documents


2 download

DESCRIPTION

 

TRANSCRIPT

Page 1: 15 jpa

Professional Open Source™

© JBoss, Inc. 2003, 2004. 1

07/17/04

JPA

Page 2: 15 jpa

© JBoss, Inc. 2003, 2004. 2

Professional Open Source™

The @Id annotation

Using the javax.persistence.Id annotations is the simplest way of telling the persistence provider where the entity identity is stored.

The @Id annotation marks afield or property as identity for an entity.

Page 3: 15 jpa

© JBoss, Inc. 2003, 2004. 3

Professional Open Source™

The @IdClass annotation

The @IdClass annotation enables you to use more than one @Id annotation in a sensible way

This is the basic problem with using more than one @Id field or property in an entity class: it is not obvious how to compare two instances in an automated fashion. This is especially true since in cases where composite keys are necessary

Page 4: 15 jpa

© JBoss, Inc. 2003, 2004. 4

Professional Open Source™

Using @IdClass

Page 5: 15 jpa

© JBoss, Inc. 2003, 2004. 5

Professional Open Source™

The @EmbeddedId annotation

Using the @EmbeddedId annotation is like moving the IdClass right into your entity and using the identity fields nested inside it to store entity data.

See the example in next page

Page 6: 15 jpa

© JBoss, Inc. 2003, 2004. 6

Professional Open Source™

The @EmbeddedId annotation

Page 7: 15 jpa

© JBoss, Inc. 2003, 2004. 7

Professional Open Source™

GENERATING PRIMARY KEYS

Page 8: 15 jpa

© JBoss, Inc. 2003, 2004. 8

Professional Open Source™

Database sequences as generators

To use sequence generators, first define a sequence in the database. The following is a sample sequence for the USER_ID column in an Oracle database:

CREATE SEQUENCE USER_SEQUENCE START WITH 1 INCREMENT BY 10;

@SequenceGenerator(name="USER_SEQUENCE_GENERATOR", sequenceName="USER_SEQUENCE", initialValue=1, allocationSize=10)

The @SequenceGenerator annotation creates a sequence generator named USER_SEQUENCE_GENERATOR referencing the Oracle sequence we created and matching its setup.

Page 9: 15 jpa

© JBoss, Inc. 2003, 2004. 9

Professional Open Source™

Using Sequence Generator

Page 10: 15 jpa

© JBoss, Inc. 2003, 2004. 10

Professional Open Source™

Sequence tables as generators

The first step is creating a table to use for generating values as follows :

CREATE TABLE SEQUENCE_GENERATOR_TABLE (SEQUENCE_NAME VARCHAR2(80) NOT NULL, SEQUENCE_VALUE NUMBER(15) NOT NULL, PRIMARY KEY (SEQUENCE_NAME));

The next step is to prepare the table for use by inserting the initial value manually as follows:

INSERT INTO SEQUENCE_GENERATOR_TABLE (SEQUENCE_NAME,

SEQUENCE_VALUE) VALUES ('USER_SEQUENCE', 1);

Page 11: 15 jpa

© JBoss, Inc. 2003, 2004. 11

Professional Open Source™

Sequence tables as generators

Next step : @TableGenerator (name="USER_TABLE_GENERATOR", table="SEQUENCE_GENERATOR_TABLE", pkColumnName="SEQUENCE_NAME", valueColumnName="SEQUENCE_VALUE", pkColumnValue="USER_SEQUENCE")

Next step :

@Id @GeneratedValue(strategy=GenerationType.TABLE, generator="USER_TABLE_GENERATOR") @Column(name="USER_ID") protected Long userId;

Page 12: 15 jpa

© JBoss, Inc. 2003, 2004. 12

Professional Open Source™

Mapping embeddable classes

See next page for sample code

Page 13: 15 jpa

© JBoss, Inc. 2003, 2004. 13

Professional Open Source™

Mapping embeddable classes

Page 14: 15 jpa

© JBoss, Inc. 2003, 2004. 14

Professional Open Source™

Unidirectional one-to-one relationship

Page 15: 15 jpa

© JBoss, Inc. 2003, 2004. 15

Professional Open Source™

Bidirectional one-to-one

you can access EJBContext through DI. For example, a SessionContext could be injected into a bean as follows:

See next page for explanation

Page 16: 15 jpa

© JBoss, Inc. 2003, 2004. 16

Professional Open Source™

Bidirectional one-to-one

The @OneToOne annotation on the user field has two interesting things going on. – The first is the mappedBy="billingInfo" specification . This tells the

container that the “owning” side of the relationship exists in the User class’s billingInfo instance variable.

– The second interesting feature of the @OneToOne annotation on the user field is that the optional parameter is set to false this time. This means that a BillingInfo object cannot exist without a related User object.

Page 17: 15 jpa

© JBoss, Inc. 2003, 2004. 17

Professional Open Source™

One-to-many bidirectional relationship

Page 18: 15 jpa

© JBoss, Inc. 2003, 2004. 18

Professional Open Source™

Many-to-one as owning-side of relationship

For bidirectional one-to-many relationships,ManyToOne is always the owning side of the relationship.

Because of this fact, the mappedBy element does not exist in the definition of the @ManyToOne annotation:

Page 19: 15 jpa

© JBoss, Inc. 2003, 2004. 19

Professional Open Source™

@ManyToMany

Page 20: 15 jpa

© JBoss, Inc. 2003, 2004. 20

Professional Open Source™

Mapping an entity

Page 21: 15 jpa

© JBoss, Inc. 2003, 2004. 21

Professional Open Source™

Page 22: 15 jpa

© JBoss, Inc. 2003, 2004. 22

Professional Open Source™

Specifying the table

Page 23: 15 jpa

© JBoss, Inc. 2003, 2004. 23

Professional Open Source™

Mapping the columns

If the insertable parameter is set to false, the field or property will not be included in the INSERT statement generated by the persistence provider to create a new record corresponding to the entity. Likewise, setting the updatable parameter to false excludes the field or property from being updated when the entity is saved. These two parameters are usually helpful in dealing with read-only data, like primary keys generated by the database.

Page 24: 15 jpa

© JBoss, Inc. 2003, 2004. 24

Professional Open Source™

Mapping CLOBs and BLOBs

• Whether a field or property designated @Lob is a CLOB or a BLOB is determined by its type.

• If the data is of type char[] or String, the persistence provider maps the data to a CLOB column. Otherwise, the column is mapped as a BLOB.

• An extremely useful annotation to use in conjunction with @Lob is @Basic. @Basic can be marked on any attribute with direct-to-field mapping. Just as we have done for the picture field, the @Basic(fetch=FetchType.LAZY) specification causes the BLOB or CLOB data to be loaded from the database only when it is first accessed.

• Postponing of loading of entity data from the database is known as lazy loading.

Page 25: 15 jpa

© JBoss, Inc. 2003, 2004. 25

Professional Open Source™

Mapping temporal types

The @Temporal annotation specifies which of these data types we want to map a java.util.Date or java.util.Calendar persistent data type to.

Note this explicit mapping is redundant while using the java.sql.Date, java.sql.Time or java.sql.Timestamp Java types.

If we do not specify a parameter for @Temporal annotation or omit it altogether, the persistence provider will assume the data type mapping to be TIMESTAMP

Page 26: 15 jpa

© JBoss, Inc. 2003, 2004. 26

Professional Open Source™

Mapping an entity to multiple tables

Page 27: 15 jpa

© JBoss, Inc. 2003, 2004. 27

Professional Open Source™

Generating primary keys

Page 28: 15 jpa

© JBoss, Inc. 2003, 2004. 28

Professional Open Source™

Mapping one-to-one relationships

Depending on where the foreign key resides, the relationship could be implemented in two different ways: using the @JoinColumn or the @PrimaryKeyJoinColumn annotation.

User has a one-to-one unidirectional relationship with BillingInfo. The User and BillingInfo entities are mapped to the USERS and BILLING_INFO tables, respectively, and the USERS table has a foreign key reference to the BILLING_INFO table. Such associations are mapped using @JoinColumn.

Page 29: 15 jpa

© JBoss, Inc. 2003, 2004. 29

Professional Open Source™

Using @JoinColumn

Page 30: 15 jpa

© JBoss, Inc. 2003, 2004. 30

Professional Open Source™

Using @JoinColumn

Like the @Column annotation, the @JoinColumn annotation contains the updatable,insertable, table, and unique elements. The elements serve the same purposeas the elements of the @Column annotation. In our case, updatable is set to false, which means that the persistence provider would not update the foreign key even if the billingInfo reference were changed.

If you have more than one column in the foreign key, you can use the JoinColumns annotation instead.

Page 31: 15 jpa

© JBoss, Inc. 2003, 2004. 31

Professional Open Source™

Using @PrimaryKeyJoinColumn

User has a one-to-one unidirectional relationship with BillingInfo. The User andBillingInfo entities are mapped to the USERS and BILLING_INFO tables, respectively, and theBILLING_INFO and USERS tables share the same primary key; the primary key of the BILLING_INFO table is also a foreign key referencing the primary key of the USERS table. Such associations are mapped using @PrimaryKeyJoinColumn.

Page 32: 15 jpa

© JBoss, Inc. 2003, 2004. 32

Professional Open Source™

Mapping a one-to-one relationship using @PrimaryKeyJoinColumn

Page 33: 15 jpa

© JBoss, Inc. 2003, 2004. 33

Professional Open Source™

One-to-many bidirectional relationship mapping

Page 34: 15 jpa

© JBoss, Inc. 2003, 2004. 34

Professional Open Source™

Many-to-one self-referencing relationship mapping

Page 35: 15 jpa

© JBoss, Inc. 2003, 2004. 35

Professional Open Source™

Many-to-many

Many-to-many relationships are modeled in the database world using join tables. A join table essentially pairs foreign keys pointing to primary keys on either side of the relationship.

Page 36: 15 jpa

© JBoss, Inc. 2003, 2004. 36

Professional Open Source™

Many-to-many relationship mapping

Page 37: 15 jpa

© JBoss, Inc. 2003, 2004. 37

Professional Open Source™

Mapping inheritance

Entities in the hierarchy in below figure can be mapped to database tables using different types of inheritance mapping strategies supported by JPA:– Single table– Joined tables– Table per class

Page 38: 15 jpa

© JBoss, Inc. 2003, 2004. 38

Professional Open Source™

Single-table strategy

In the single-table strategy, all classes in the inheritance hierarchy are mapped to a single table. This means that the single table will contain a superset of all data stored in the class hierarchy. Different objects in the OO hierarchy are identified using a special column called a discriminator column. In effect, the discriminator column contains a value unique to the object type in a given row.

Page 39: 15 jpa

© JBoss, Inc. 2003, 2004. 39

Professional Open Source™

Inheritance mapping using a single table

Page 40: 15 jpa

© JBoss, Inc. 2003, 2004. 40

Professional Open Source™

Joined-tables strategy

The joined-tables inheritance strategy uses one-to-one relationships to model OO inheritance. In effect, the joined-tables strategy involves creating separate tables for each entity in the OO hierarchy and relating direct descendants in the hierarchy with one-to-one relationships.

The discriminator column in the USERS table is still used, primarily as a way of easily differentiating data types in the hierarchy.

Page 41: 15 jpa

© JBoss, Inc. 2003, 2004. 41

Professional Open Source™

Inheritance mapping using joined tables

Page 42: 15 jpa

© JBoss, Inc. 2003, 2004. 42

Professional Open Source™

Table-per-class strategy

Entity data are stored in their own tables even if they are inherited from the superclass. This is true even for the USER_ID primary key.

Page 43: 15 jpa

© JBoss, Inc. 2003, 2004. 43

Professional Open Source™

Inheritance mapping using the table-per-class strategy