Home » Java » Enterprise Java » JPA @Embeddable and @Embedded

About Shubhra Srivastava

Shubhra Srivastava
Shubhra is a software professional and founder of ProgrammerGirl. She has a great experience with Java/J2EE technologies and frameworks. She loves the amalgam of programming and coffee :)

JPA @Embeddable and @Embedded

Introduction:

As an object-oriented developer, we want to avoid having larger classes with tons of unrelatable fields. And so, we might often feel the need to represent a JPA Entity using multiple objects.

In this quick tutorial, we’ll learn how to achieve it using @Embedded and @Embeddable annotations in JPA or Hibernate.

Context Buildup:

Let’s say we have a person table with the following columns:

1
id|firstName|middleName|lastName|street|city|country|pincode

And we want to map it as a JPA Entity.

Mapping so many properties directly in our entity class doesn’t feel that natural to our developer instinct. Also, changing the database table structure is not a viable option. What do we do?

@Embeddable:

Let’s define a PersonName and Address classes first:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
@Embeddable
public class PersonName {
  
    private String firstName;
    private String middleName;
    private String lastName;
  
    //constructors, getters, setters
    ...
}
  
@Embeddable
public class Address {
    private String street;
    private String city;
    private String country;
    private String pincode;
  
    //constructors, getters, setters
    ...
}

We have marked both of these with @Embeddable annotation to denote that they’ll be embedded into a JPA entity.

@Embedded:

Finally, we’ll use @Embedded annotation to embed a specific type.

Let’s define our Person entity that’ll represent our person table:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
@Entity
public class Person {
     
    @Id
    @GeneratedValue
    private Integer id;
     
    @Embedded
    private PersonName name;
  
    @Embedded
    private Address address;
  
    //constructor, getters, setters
    ...
}

We have used @Embedded annotation here to denote that these objects will be embedded in our entity. In other words, all of these objects will together be mapped to a single person database table.

Overriding Attributes:

Embeddable objects often come handy specifically when we have multiple entities using it.

Let’s now say, we have another table – office:

1
id|streetAddr|city|country|postcode|...

The office table also has an Address type with the difference in just a few field names.

Here as well, we can use the same Address embeddable object. The idea is to override the street and pincode attributes of the Address class using @AttributeOverrides and @AttributeOverride annotations:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
@Entity
public class Office {
     
    @Id
    @GeneratedValue
    private Integer id;
  
    @Embedded
    @AttributeOverrides(value = {
      @AttributeOverride(name = "street", column = @Column(name = "streetAddr")),
      @AttributeOverride(name = "pincode", column = @Column(name = "postcode"))
    })
    private Address address;
    ...
}

This approach is cleaner and saves us from having duplicate fields with just different names repeated across multiple entities. 

Rather, we can override any of the column properties of our embeddable type.

Conclusion:

In this tutorial, we explored JPA @Emdeddable and @Embedded annotations.

Be the First to comment.

Published on Java Code Geeks with permission by Shubhra Srivastava, partner at our JCG program. See the original article here: JPA @Embeddable and @Embedded

Opinions expressed by Java Code Geeks contributors are their own.

(0 rating, 0 votes)
You need to be a registered member to rate this.
2 Comments Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

2
Leave a Reply

avatar
2 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
2 Comment authors
Nenad KosanovicSt1mpy Recent comment authors

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
newest oldest most voted
Notify of
St1mpy
Guest
St1mpy

Nice article programmer girl

Nenad Kosanovic
Guest
Nenad Kosanovic

Nice and clean…