`
isiqi
  • 浏览: 16579267 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

Understanding JPA, 6

阅读更多

Page 6 of 6

Joined-table inheritance

In the joined-table inheritance strategy, the common states of the class are stored in one table, and the state of the subclass is stored in another table that is joined to the first table, as shown in Table 3.

Table 3. Joined-table inheritance mapping strategy

ENTITY TABLE NAME
Customer CUSTOMER
OnlineCustomer ONLINECUSTOMER (only Website information is stored here; the rest of the information is stored in the CUSTOMER table)

The common data that the OnlineCustomer entity shares with Customer is stored in the CUSTOMER table; OnlineCustomer-specific data is stored in the ONLINECUSTOMER table, connected by a foreign key constraint. From the JPA implementation standpoint, the only change needed in the OnlineCustomer entity is that the JOINED strategy must be provided, as in Listing 17.

Listing 17. A superclass in joined-table inheritance

@Entity(name = "CUSTOMER") //Name of the entity
@Inheritance(strategy=InheritanceType.JOINED)
@DiscriminatorColumn(name="CUST_TYPE", discriminatorType=DiscriminatorType.STRING,length=10)
@DiscriminatorValue("RETAIL")

public class Customer implements Serializable{
@Id //signifies the primary key
@Column(name = "CUST_ID", nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
private long custId;

@Column(name = "FIRST_NAME", nullable = false,length = 50)
private String firstName;

@Column(name = "LAST_NAME", length = 50)
private String lastName;

@Embedded
private Address address = new Address();

@Column(name = "CUST_TYPE", length = 10)
private String custType;
.................
}

In the OnlineCustomer entity, shown in Listing 18, you specify the subclass-specific attributes and a foreign key join column with the @PrimaryKeyJoinColumn annotation, which maps to the primary key of the parent table.

Listing 18. A sample subclass in joined-table inheritance

@Table(name="ONLINECUSTOMER")
@Entity(name = "ONLINECUSTOMER") //Name of the entity
@DiscriminatorValue("ONLINE")
@PrimaryKeyJoinColumn(name="CUST_ID",referencedColumnName="CUST_ID")
public class OnlineCustomer extends Customer{

@Column(name = "WEBSITE", length = 100)
private String website;
................
}

In Listing 18, the name property of @PrimaryKeyJoinColumn denotes the primary key of the subclass table. referencedColumnName denotes the name of the superclass table column to which this subclass table column joins. Nothing changes in the way in which you persist and fetch the Customer and OnlineCustomer objects.

Table per class inheritance

In the table per class inheritance strategy, each class state in a hierarchy is stored in a separate table, as illustrated in Table 4.

Table 4. Table per class inheritance mapping strategy

ENTITY TABLE NAME
Customer CUSTOMER
OnlineCustomer ONLINECUSTOMER

You don't need to provide the @DiscriminatorColumn annotation here, as the entities are stored in separate tables altogether. Also, no @PrimaryKeyJoinColumn annotation is required, as no relationship exists between subclass and superclass tables. The Customer superclass will look like Listing 19.

Listing 19. A sample superclass in table per class inheritance

@Entity(name = "CUSTOMER")
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public class Customer implements Serializable{
@Id //signifies the primary key
@Column(name = "CUST_ID", nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
private long custId;

@Column(name = "FIRST_NAME", nullable = false,length = 50)
private String firstName;

@Column(name = "LAST_NAME", length = 50)
private String lastName;

@Embedded
private Address address = new Address();
...........
}

OnlineCustomer will be like a normal subclass, as shown in Listing 20. Nothing changes in the way in which you persist and fetch the Customer and OnlineCustomer objects.

Listing 20. A sample subclass in table per class inheritance

@Entity(name = "ONLINECUSTOMER") //Name of the entity
public class OnlineCustomer extends Customer{

@Column(name = "WEBSITE", length = 100)
private String website;
.................
}

Toward a more object-oriented world

In this first half of "Understanding the Java Persistence API," you saw how you can apply object-oriented concepts like inheritance and event callbacks using JPA. The object-oriented possibilities in JPA go far beyond what has been covered in this article: JPA also lets you write JPQL queries (similar to Hibernate's HQL queries) or native SQL queries, and has transaction management facilities.

In the second half of this article, you'll have the opportunity to explore data relationships the JPA way -- that is, with object-oriented grace. Look for that article next week. In the meantime, have fun on your own, exploring JPA's object-oriented paradigm of data persistence!

About the author

Aditi Das is a technical architect with Infosys Technologies and has seven years of specialized experience in Java and JEE. She is a Sun-certified enterprise architect (SCEA), Web component developer (SCWCD), business component developer (SCBCD), and Web service developer (SCDJWS). She is very much inspired by the Head First philosophy of learning new technologies, and hopes that someday a book will come out on the past, present, and future of SOA.
分享到:
评论

相关推荐

    Pro JPA 2 in Java EE 8: An In-Depth Guide to Java Persistence APIs.pdf

    After completing Pro JPA 2 in Java EE 8, you will have a full understanding of JPA and be able to successfully code applications using its annotations and APIs. The book also serves as an excellent ...

    jpa_tutorial.pdf

    This tutorial provides a basic understanding of how to store a copy of database objects into temporary memory using JAVA Persistence API (JPA).

    Understanding Transaction

    在本文中,我们将深入探讨事务的基本原理、类型、特性以及在实际开发中的应用,尤其是与Java JPA(Java Persistence API)的结合。 首先,让我们了解什么是事务。在数据库管理中,事务是一系列数据库操作的逻辑单元...

    Spring.Essentials.178398

    Explore simplified but powerful data access techniques including JPA (Java Persistence Architecture) repositories and NoSQL data access Who This Book Is For If you are a Java developer who is looking ...

    Java Magazine NovDec 2017.pdf

    在技术细节方面,文章"Understanding Java Method Invocation with Invokedynamic"讨论了Java 7中引入的invokedynamic指令。这条指令使得Java在运行时可以动态地解决方法调用,为Java虚拟机带来了动态类型语言支持。...

    JBoss in Action.pdf

    第十二章“Understanding clustering”介绍了JBoss的集群化技术,包括集群架构、节点通信机制、负载均衡策略等内容,为构建高可用、可扩展的分布式系统提供了理论基础。而第十三章可能进一步深入讲解了集群的具体...

    spring-boot-reference.pdf

    46.1. Understanding Auto-configured Beans 46.2. Locating Auto-configuration Candidates 46.3. Condition Annotations 46.3.1. Class Conditions 46.3.2. Bean Conditions 46.3.3. Property Conditions 46.3.4. ...

    IT日语

    6. **Spring AOP**:前面提到的面向切面编程,AOP允许开发者定义“切面”,这些切面可以包含业务逻辑的各个方面,如日志、事务管理、安全检查等。Spring AOP通过代理模式实现了这一功能,使得切面可以在不修改目标...

    Manning.Spring.in.Action.4th.Edition.2014.11.epub

    Chapter 6. Rendering web views 6.1. Understanding view resolution 6.2. Creating JSP views 6.2.1. Configuring a JSP-ready view resolver 6.2.2. Using Spring’s JSP libraries 6.3. Defining a layout with ...

    Seam in Action - MEAP - 2008

    - **Java 持久化 API (JPA)**:Seam 与 JPA 密切结合,提供了对实体管理、查询语言等方面的强大支持,帮助开发者更容易地进行数据访问和管理。 8. **Seam 管理的事务和持久化 (Seam-Managed Transactions and ...

    JAVA总复习

    12. **Java EE(企业版)**:包含一系列为企业级应用开发设计的组件和服务,如EJB(Enterprise JavaBeans)、JMS(Java Message Service)、JPA(Java Persistence API)等。 13. **Android开发**:Java也是Android...

    spring-framework-reference4.1.4

    3.3. Java 8 (as well as 6 and 7) ............................................................................... 17 3.4. Java EE 6 and 7 ..................................................................

    spring-framework-reference-4.1.2

    3.3. Java 8 (as well as 6 and 7) ............................................................................... 17 3.4. Java EE 6 and 7 ..................................................................

Global site tag (gtag.js) - Google Analytics