Presentation is loading. Please wait.

Presentation is loading. Please wait.

JPA: Mapping to an existing database

Similar presentations


Presentation on theme: "JPA: Mapping to an existing database"— Presentation transcript:

1 JPA: Mapping to an existing database
IS-907 Java EE JPA: Mapping to an existing database

2 Mapping entities to existing databases
We have seen how JPA automatically maps classes to tables To map a class model to an existing database more detailed control over the mapping may be needed package javax.persistence, Annotation Types Even Åby Larsen IS-102 Introduksjon

3 Table names Tablename, schema and catalog can optionally be specified with annotation: @Entity @Table(name=“tablename””, schema=“schema”, catalog=“catalog”) public class Employee { ... } Even Åby Larsen IS-102 Introduksjon

4 Columns Column properties are specified with column annotation:
@Entity public class Employee { @Id private long empId; @Column(name=“login”, unique=“true”, nullable=“false”) private String username; ... } Even Åby Larsen IS-102 Introduksjon

5 Large objects annotation is used to map fields to CLOBs or BLOBs byte[], Byte[], and Serializable classes are mapped to BLOB char[], Character[], and String are mapped to CLOB @Entity public class Employee { @Lob byte[] speech; @Lob Picture picture; @Lob String biography; } Even Åby Larsen IS-102 Introduksjon

6 @Basic annotation Fetching of field values can be deferred until the field is actually needed, to speed up loading of data: @Entity public class Employee { @Basic(fetch=“FetchType.LAZY”) @Lob private byte[] picture; ... } Even Åby Larsen IS-102 Introduksjon

7 Temporal types The mapping of Date and Calendar fields can be controlled annotations: @Entity public class Employee { @Temporal(TemporalType.DATE) private Calendar birthDate; @Temporal(TemporalType.TIMESTAMP) private Timestamp lastLogin; } Even Åby Larsen IS-102 Introduksjon

8 Foreign keys The name of foreign key columns can be specified with a JoinColumn annotation: @Entity public class Employee { @ManyToOne @JoinColumn(name=“dep”, nullable=“false”, unique=“false”) private Department dep; ... } Even Åby Larsen IS-102 Introduksjon

9 Join tables @Entity public class Project { @ManyToMany
@JoinTable(name=“proj_emp” private List<Employee> members; ... } Even Åby Larsen IS-102 Introduksjon

10 Ordered list @Entity public class Project { @ManyToMany
@OrderBy(“name ASC”) private List<Employee> members; ... } Even Åby Larsen IS-102 Introduksjon

11 Using a Map instead of a List
@Entity public class private Map<String,Employee> members; ... } Even Åby Larsen IS-102 Introduksjon

12 Lazy relationships Loading of related objects may be deferred by specifying fetch type: @Entity public class Employee { @ManyToOne(fetch=“FetchType.LAZY”) private Department department; ... } Even Åby Larsen IS-102 Introduksjon

13 Cascading @Entity public class Order {
@OneToMany(cascade={CascadeType.PERSIST, CascadType.MERGE}) List<OrderLine> lines; } ALL DETACH MERGE PERSIST REFRESH REMOVE Even Åby Larsen IS-102 Introduksjon


Download ppt "JPA: Mapping to an existing database"

Similar presentations


Ads by Google