Chapter 4. Mapping persistent classes

 

In this chapter

  • Understanding entities and value type concepts
  • Mapping entity classes with identity
  • Controlling entity-level mapping options

This chapter presents some fundamental mapping options and explains how to map entity classes to SQL tables. We show and discuss how you can handle database identity and primary keys, and how you can use various other metadata settings to customize how Hibernate loads and stores instances of your domain model classes. All mapping examples use JPA annotations. First, though, we define the essential distinction between entities and value types, and explain how you should approach the object/relational mapping of your domain model.

Major new feature in JPA 2

You can globally enable escaping of all names in generated SQL statements with the <delimited-identifiers> element in the persistence.xml configuration file.

4.1. Understanding entities and value types

4.2. Mapping entities with identity

Path: /model/src/main/java/org/jpwh/model/simple/Item.java

Path: /model/src/main/java/org/jpwh/model/package-info.java

4.3. Entity-mapping options

Path: /model/src/main/java/org/jpwh/model/simple/User.java

@Entity
@Table(name = "USERS")
public class User implements Serializable {
<enter/>
    // ...
}

Path: /shared/src/main/java/org/jpwh/shared/CENamingStrategy.java

public class CENamingStrategy extends
    org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl {
<enter/>
    @Override
    public Identifier toPhysicalTableName(Identifier name,
                                          JdbcEnvironment context) {
        return new Identifier("CE_" + name.getText(), name.isQuoted());
    }
<enter/>
}

Path: /model/src/main/java/org/jpwh/model/advanced/ItemBidSummary.java

Path: /examples/src/test/java/org/jpwh/test/advanced/MappedSubselect.java

ItemBidSummary itemBidSummary = em.find(ItemBidSummary.class, ITEM_ID);
// select * from (

//      select i.ID as ITEMID, i.ITEM_NAME as NAME, ...
// ) where ITEMID = ?

Path: /examples/src/test/java/org/jpwh/test/advanced/MappedSubselect.java

4.4. Summary

sitemap