cours/java persistance api.md
Oscar Plaisant 602a41e7f8 update
2024-12-25 22:30:24 +01:00

79 lines
1.8 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
aliases:
- JPA
---
up:: [[EJB entity bean]]
#s/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;
}
```