Embed Object
Embed Object
We can use the @Embedded annotation to embed an embeddable class. The attributes of an embedded class can be overridden using
the @AttributeOverrides and the @AttributeOverride annotations.
• IDE - Eclipse
• Maven 3.5.3
• JavaSE 17
• MySQL - 8.0.32
• Maven - 3.5.3
• JDK - 1.8
Step-1
• Open Eclipse
Step-2
Java_ Page 1
Step-2
Click on Checkbox for both
Step-3
Provide GroupId and ArtifactId in next screen.
• GroupId: net.tr.maven-demo
• Name: maven-demo-project
Java_ Page 2
3. Add jar Dependencies to pom.xml
We use Apache Maven to manage the project's dependencies. Add the following dependencies to your project's pom.xml file.
<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>net.tr.hibernate</groupId>
<artifactId>hibernate-tutorial</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>hibernate-Embaddable-example</artifactId>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.32</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.1.7.Final</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
The advantage of using embedded objects is that they are reusable and you can map two entities to the same database table.
Address.java
package net.tr.hibernate.entity;
import jakarta.persistence.Embeddable;
@Embeddable
public class Address {
private StringaddressLine1;
private StringaddressLine2;
private Stringcity;
private Stringstate;
private Stringcountry;
private StringzipCode;
Java_ Page 3
private StringzipCode;
public Address() {
public StringgetAddressLine1() {
return addressLine1;
}
public StringgetAddressLine2() {
return addressLine2;
}
public StringgetCity() {
return city;
}
public StringgetState() {
return state;
}
public StringgetCountry() {
return country;
}
public StringgetZipCode() {
return zipCode;
}
Name.java
package net.tr.hibernate.entity;
import jakarta.persistence.Embeddable;
@Embeddable
public class Name {
private StringfirstName;
private StringmiddleName;
private StringlastName;
public Name() {
public StringgetFirstName() {
return firstName;
}
public StringgetMiddleName() {
return middleName;
}
public StringgetLastName() {
return lastName;
}
Java_ Page 4
Embedding an Embeddable object with @Embedded
We can simply embed an embeddable class using the @Embedded annotation. This class will be embedded in the same database table as the source. We can
optionally override the attributes of the embedded class using the @AttributeOverrides and @AttributeOverride annotations.
Let's create a User class and embed Address and Name classes into it using the @Embedded annotation:
import jakarta.persistence.*;
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Longid;
@Embedded
private Namename;
private Stringemail;
@Embedded
@AttributeOverrides(value = {
@AttributeOverride(name = "addressLine1", column = @Column(name = "house_number")),
@AttributeOverride(name = "addressLine2", column = @Column(name = "street"))
})
private Addressaddress;
public User() {
public LonggetId() {
return id;
}
public NamegetName() {
return name;
}
public StringgetEmail() {
return email;
}
public AddressgetAddress() {
return address;
}
import org.hibernate.Session;
import org.hibernate.Transaction;
import net.tr.hibernate.entity.Address;
import net.tr.hibernate.entity.Name;
import net.tr.hibernate.entity.User;
import net.tr.hibernate.util.HibernateUtil;
Transactiontransaction = null;
try (Sessionsession = HibernateUtil.getSessionFactory().openSession()) {
// start a transaction
transaction = session.beginTransaction();
// save the student object
session.persist(user);
// commit transaction
transaction.commit();
} catch (Exceptione) {
if (transaction != null) {
transaction.rollback();
}
e.printStackTrace();
Java_ Page 5
e.printStackTrace();
}
}
}
Output
Java_ Page 6