hibernate
Mappages d'association entre entités
Recherche…
Association OneToMany
Pour illustrer la relation OneToMany, nous avons besoin de 2 entités, par exemple, pays et ville. Un pays a plusieurs villes.
Dans la CountryEntity ci-dessous, nous définissons un ensemble de villes pour le pays.
@Entity
@Table(name = "Country")
public class CountryEntity implements Serializable
{
private static final long serialVersionUID = 1L;
@Id
@Column(name = "COUNTRY_ID", unique = true, nullable = false)
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Integer countryId;
@Column(name = "COUNTRY_NAME", unique = true, nullable = false, length = 100)
private String countryName;
@OneToMany(mappedBy="country", fetch=FetchType.LAZY)
private Set<CityEntity> cities = new HashSet<>();
//Getters and Setters are not shown
}
Maintenant l'entité de la ville.
@Entity
@Table(name = "City")
public class CityEntity implements Serializable
{
private static final long serialVersionUID = 1L;
@Id
@Column(name = "CITY_ID", unique = true, nullable = false)
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Integer cityId;
@Column(name = "CITY_NAME", unique = false, nullable = false, length = 100)
private String cityName;
@ManyToOne(optional=false, fetch=FetchType.EAGER)
@JoinColumn(name="COUNTRY_ID", nullable=false)
private CountryEntity country;
//Getters and Setters are not shown
}
Une à plusieurs associations utilisant XML
Ceci est un exemple de la façon dont vous effectueriez un mappage un à plusieurs avec XML. Nous utiliserons Author and Book comme exemple et supposons qu'un auteur a peut-être écrit de nombreux livres, mais que chaque livre n'aura qu'un auteur.
Classe d'auteur:
public class Author {
private int id;
private String firstName;
private String lastName;
public Author(){
}
public int getId(){
return id;
}
public void setId(int id){
this.id = id;
}
public String getFirstName(){
return firstName;
}
public void setFirstName(String firstName){
this.firstName = firstName;
}
public String getLastName(){
return lastName;
}
public void setLastName(String lastName){
this.lastName = lastName;
}
}
Classe de livre:
public class Book {
private int id;
private String isbn;
private String title;
private Author author;
private String publisher;
public Book() {
super();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Author getAuthor() {
return author;
}
public void setAuthor(Author author) {
this.author = author;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
}
Author.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="Author" table="author">
<meta attribute="class-description">
This class contains the author's information.
</meta>
<id name="id" type="int" column="author_id">
<generator class="native"/>
</id>
<property name="firstName" column="first_name" type="string"/>
<property name="lastName" column="last_name" type="string"/>
</class>
</hibernate-mapping>
Book.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="Book" table="book_title">
<meta attribute="class-description">
This class contains the book information.
</meta>
<id name="id" type="int" column="book_id">
<generator class="native"/>
</id>
<property name="isbn" column="isbn" type="string"/>
<property name="title" column="title" type="string"/>
<many-to-one name="author" class="Author" cascade="all">
<column name="author"></column>
</many-to-one>
<property name="publisher" column="publisher" type="string"/>
</class>
</hibernate-mapping>
Ce qui rend la connexion un à plusieurs, c'est que la classe Book contient un auteur et que le fichier XML a la balise <many-to-one>. L'attribut en cascade vous permet de définir la manière dont l'entité enfant sera enregistrée / mise à jour.