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

Understanding JPA,4

阅读更多

Page 4 of 6

A composite primary key

Now, how do you find a record in the database with a primary key if the entity has a composite primary key? You'll see how that works in a moment. Assume that the CUSTOMER table doesn't have a CUST_ID field, but that FIRST_NAME and LAST_NAME together make up the primary key. To make this work, you need to create a separate class, generally called an identity class, with attributes the same as the IDs; then you reference the identity class in the entity class. This is shown in Listing 7.

Listing 7. An identity class

public class CustomerId {
public String firstName;
public String lastName;
// override equal() method
//override hascode() method
..................
}


The identity class can be a separate class or an inner class. If the class is an inner class, it must be static and should be referenced in the entity class, as in Listing 8. The code for finding records with a composite primary key is exactly the same as that for finding records with a single primary key.

Listing 8. Using an identity class

@Entity
@IdClass(Customer.CustomerId.class)
public class Customer implements Serializable{
@Id
@Column(name = "FIRST_NAME", nullable = false, length = 50)
private String firstName;

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

private String street;

@Column(name = "APPT",nullable = false)
private String appt;

................
}

The callbacks

JPA provides callback methods for performing actions at different stages of persistence operations. Imagine that you want to update a customer record, but, before you update, you want to remove the hyphen from the zip code if one is present. Or say that you want to populate some transient fields after a successful fetch. JPA provides listeners for these kinds of activities before and after each fetch, insert, or update operation. The callback methods can be annotated as any of the following:

  • @PostLoad
  • @PrePersist
  • @PostPersist
  • @PreUpdate
  • @PostUpdate
  • @PreRemove
  • @PostRemove

You can write the callback methods in the entity class itself, or you can write them in a separate class and reference them in the entity class with @EntityListeners, as shown in Listing 9.

Listing 9. implementing callback

@EntityListeners({CustListner.class})
@Entity(name = "CUSTOMER") //Name of the entity
public class Customer implements Serializable{
...
...
}
public class CustListner {
@PreUpdate
public void preUpdate(Customer cust) {
System.out.println("In pre update");
}
@PostUpdate
public void postUpdate(Customer cust) {
System.out.println("In post update");
}
}

Embedded objects

As you've seen so far, the Customer entity has the address information inline in the entity itself. What if you want to apply class normalization concepts and come up with a separate Address class and refer to that in the Customer entity? After all, an address object could be used with Customer, Employee, Order, or User entities.

All you need is an embedded object. You move the address information into a separate class and mark that class as being embeddable, as shown in Listing 10. Refer to this newly created class from the Customer entity with @Embedded annotations.

Listing 10. An embeddable class

@Embeddable
public class Address implements Serializable{

private String street;

@Column(name = "APPT",nullable = false)
private String appt;

private String city;
..
..
}

Embedded classes are mapped together with their owning entity as part of the state of that entity. However, they cannot be queried separately. Listing 11 illustrates a sample entity that uses an embedded object.

Listing 11. A sample entity using an embedded object

@Entity
public class Customer {
...............
@Column(name = "FIRST_NAME", nullable = false,length = 50)
private String firstName;

@Embedded
private Address address;
..............
}

分享到:
评论

相关推荐

    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

    4. **持久性(Durability)**:一旦事务提交,其结果将永久保存,即使系统崩溃也不会丢失。 在实际编程中,我们常常使用不同的事务隔离级别来平衡性能和数据一致性。常见的隔离级别有读未提交(READ UNCOMMITTED)...

    Spring.Essentials.178398

    Build mission-critical enterprise applications using ...4. Understanding WebSocket 5. Securing Your Applications 6. Building a Single-Page Spring Application 7. Integrating with Other Web Frameworks

    Java Magazine NovDec 2017.pdf

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

    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. ...

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

    Chapter 4. Aspect-oriented Spring 4.1. What is aspect-oriented programming? 4.1.1. Defining AOP terminology 4.1.2. Spring’s AOP support 4.2. Selecting join points with pointcuts 4.2.1. Writing ...

    JBoss in Action.pdf

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

    IT日语

    8. **Understanding ORM**:对象关系映射是将数据库操作转化为对对象的操作的技术,它可以减少数据库操作的复杂性,提高代码的可维护性和可读性。ORM工具如Hibernate使得开发者可以使用面向对象的方式来处理数据库...

    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

    Using SLF4J ............................................................................................ 13 Using Log4J ...................................................................................

    spring-framework-reference-4.1.2

    Using SLF4J ............................................................................................ 13 Using Log4J ...................................................................................

Global site tag (gtag.js) - Google Analytics