Understanding JPA, 6

本文探讨了Java Persistence API (JPA) 中的两种主要继承映射策略:joined-table和table-per-class。通过示例详细说明了每种策略如何在数据库中存储超类和子类的数据,并提供了具体的实体类实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
ENTITYTABLE NAME
CustomerCUSTOMER
OnlineCustomerONLINECUSTOMER (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
ENTITYTABLE NAME
CustomerCUSTOMER
OnlineCustomerONLINECUSTOMER

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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值