update
This commit is contained in:
79
java persistance api.md
Normal file
79
java persistance api.md
Normal file
@@ -0,0 +1,79 @@
|
||||
---
|
||||
aliases:
|
||||
- JPA
|
||||
---
|
||||
up:: [[EJB entity bean]]
|
||||
#informatique/langage/java
|
||||
|
||||
Annotations java qui permettent de créer facilement des [[EJB entity bean|entity beans]]
|
||||
|
||||
Documentation : http://www.objectdb.com/api/java/jpa
|
||||
|
||||
|
||||
# Exemples
|
||||
|
||||
```java
|
||||
@Entity
|
||||
public class Book {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
@Column(name = "book_title", nullable = false, updatable = false) private String title;
|
||||
private Float price;
|
||||
@Column(length = 2000)
|
||||
private String description;
|
||||
private String isbn;
|
||||
@Column(name = "nb_of_page", nullable = false)
|
||||
private Integer nbOfPage;
|
||||
private Boolean illustrations;
|
||||
@Basic(fetch = FetchType.LAZY)
|
||||
@Lob
|
||||
private byte[] audioText;
|
||||
|
||||
// Constructors, getters, setters ...
|
||||
}
|
||||
```
|
||||
|
||||
- `@Column` : préférences pour les colonnes (de la BDD)
|
||||
- `name, unique, nullable, insertable, updatable, table, length, precision, scale...`
|
||||
- `@GeneratedValue` : stratégie de génération automatique des clefs primaires
|
||||
- stratégie recommandée : `GenerationType.auto`
|
||||
- `@Lob` : "Large object" (notamment pour les BLOB : Binary Large OBject)
|
||||
- Souvent utilisé avec `@Basic(fetch = FetchType.LAZY)` indiquer un chargement lazy (seulement au moment du get de cet attribut).
|
||||
|
||||
## Colonnes composites
|
||||
```java
|
||||
@Embeddable
|
||||
public class Address {
|
||||
protected String street;
|
||||
protected String city;
|
||||
protected String state;
|
||||
@Embedded
|
||||
Zipcode zipcode;
|
||||
}
|
||||
|
||||
@Embeddable
|
||||
public class Zipcode {
|
||||
String zip;
|
||||
protected String plusFour;
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
## Clef primaire composite
|
||||
```java
|
||||
@Embeddable
|
||||
public class CompositeId {
|
||||
String name;
|
||||
String email;
|
||||
}
|
||||
|
||||
@Entity
|
||||
public class Dependent {
|
||||
@EmbeddedId // indique que la clef primaire est dans une autre classe
|
||||
CompositeId id;
|
||||
|
||||
@ManyToOne
|
||||
Employee emp;
|
||||
}
|
||||
```
|
Reference in New Issue
Block a user