Creating Persistent Classes and Mapping
This lesson focuses on defining persistent classes in Java, representing database tables. We will explore different ways to map these classes to tables using XML mapping files (`.hbm.xml`) and annotations, specifying column mappings, data types, and relationships.
Creating Persistent Classes and Mapping in Hibernate
Introduction
This lesson focuses on defining persistent classes in Java, representing database tables. We will explore different ways to map these classes to tables using XML mapping files (.hbm.xml
) and annotations, specifying column mappings, data types, and relationships.
Persistent Classes
A persistent class is a Java class whose instances can be stored in a database. These classes represent database tables, and their properties correspond to columns within those tables. To make a class persistent in Hibernate, you need to map it to a database table.
Example of a Simple Persistent Class
public class Product {
private Long id;
private String name;
private Double price;
// Getters and setters
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public Double getPrice() { return price; }
public void setPrice(Double price) { this.price = price; }
}
Mapping Strategies: XML Mapping Files
XML mapping files (.hbm.xml
) provide a way to define the mapping between your Java classes and database tables. These files specify the table name, column mappings, data types, and other important metadata.
Example of an XML Mapping File (Product.hbm.xml)
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Product" table="products">
<id name="id" column="product_id">
<generator class="native"/>
</id>
<property name="name" column="product_name" />
<property name="price" column="product_price" />
</class>
</hibernate-mapping>
Explanation:
<class name="Product" table="products">
: Maps theProduct
class to theproducts
table.<id name="id" column="product_id">
: Defines theid
property as the primary key, mapped to theproduct_id
column.<generator class="native"/>
uses a database-native ID generation strategy.<property name="name" column="product_name" />
: Maps thename
property to theproduct_name
column.<property name="price" column="product_price" />
: Maps theprice
property to theproduct_price
column.
Mapping Strategies: Annotations
Annotations provide an alternative way to map classes and their properties directly within the Java class files. This approach is often more convenient and cleaner than using XML mapping files.
Example of a Persistent Class with Annotations
import javax.persistence.*;
@Entity
@Table(name = "products")
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "product_id")
private Long id;
@Column(name = "product_name")
private String name;
@Column(name = "product_price")
private Double price;
// Getters and setters
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public Double getPrice() { return price; }
public void setPrice(Double price) { this.price = price; }
}
Explanation:
@Entity
: Marks the class as a persistent entity.@Table(name = "products")
: Specifies the database table name.@Id
: Marks theid
property as the primary key.@GeneratedValue(strategy = GenerationType.IDENTITY)
: Configures auto-generation of the primary key (using database identity column).@Column(name = "product_id")
: Maps theid
property to theproduct_id
column.@Column(name = "product_name")
: Maps thename
property to theproduct_name
column.@Column(name = "product_price")
: Maps theprice
property to theproduct_price
column.
Data Types
When mapping properties to database columns, it's crucial to ensure that the data types are compatible. Hibernate provides various built-in types and allows you to define custom types if needed.
Common Data Type Mappings
java.lang.String
: Maps toVARCHAR
orTEXT
columns.java.lang.Integer
orint
: Maps toINTEGER
columns.java.lang.Long
orlong
: Maps toBIGINT
columns.java.lang.Double
ordouble
: Maps toDOUBLE
orDECIMAL
columns.java.util.Date
: Maps toDATE
,TIME
, orTIMESTAMP
columns.java.lang.Boolean
orboolean
: Maps toBOOLEAN
orTINYINT
columns.
Relationships
Hibernate supports various types of relationships between persistent classes, including:
- One-to-One: One instance of a class is related to one instance of another class.
- One-to-Many: One instance of a class is related to multiple instances of another class.
- Many-to-One: Multiple instances of a class are related to one instance of another class.
- Many-to-Many: Multiple instances of a class are related to multiple instances of another class.
Mapping relationships involves using annotations like @OneToOne
, @OneToMany
, @ManyToOne
, and @ManyToMany
(or their XML equivalents) along with appropriate join columns or join tables.
Conclusion
Understanding persistent classes and mapping techniques is crucial for working with Hibernate. Whether you choose XML mapping files or annotations, the goal is the same: to accurately represent your object model in the relational database.