Chapter 3. Domain models and metadata

 

In this chapter

  • Discovering the CaveatEmptor example application
  • Implementing the domain model
  • Object/relational mapping metadata options

The “Hello World” example in the previous chapter introduced you to Hibernate; certainly, it isn’t useful for understanding the requirements of real-world applications with complex data models. For the rest of the book, we use a much more sophisticated example application—CaveatEmptor, an online auction system—to demonstrate Hibernate and Java Persistence. (Caveat emptor means “Let the buyer beware”.)

Major new features in JPA 2
  • A JPA persistence provider now integrates automatically with a Bean Validation provider. When data is stored, the provider automatically validates constraints on persistent classes.
  • The Metamodel API has been added. You can obtain (unfortunately not change) the names, properties, and mapping metadata of the classes in a persistence unit.

3.1. The example CaveatEmptor application

3.2. Implementing the domain model

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

public class User implements Serializable {
<enter/>
    protected String username;
<enter/>
    public User() {
    }
<enter/>
    public String getUsername() {
        return username;
    }
<enter/>
    public void setUsername(String username) {
        this.username = username;
    }
<enter/>
    public BigDecimal calcShippingCosts(Address fromLocation) {
        // Empty implementation of business method
        return null;
    }
<enter/>
    // ...
}
<enter/>

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

public class Bid {
<enter/>
    protected Item item;
<enter/>
    public Item getItem() {
        return item;
    }
<enter/>
    public void setItem(Item item) {
        this.item = item;
    }
}

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

public class Item {
<enter/>
    protected Set<Bid> bids = new HashSet<Bid>();
<enter/>
    public Set<Bid> getBids() {
        return bids;
    }
<enter/>
    public void setBids(Set<Bid> bids) {
        this.bids = bids;
    }
}

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

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

3.3. Domain model metadata

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

import javax.persistence.Entity;
<enter/>
@Entity
public class Item {
<enter/>
}

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

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

import javax.validation.constraints.Future;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
<enter/>
@Entity
public class Item {
<enter/>
   @NotNull
   @Size(
       min = 2,
       max = 255,
       message = "Name is required, maximum 255 characters."
   )
   protected String name;
   @Future
   protected Date auctionEnd;
}

Path: /examples/src/test/java/org/jpwh/test/simple/ModelOperations.java

Path: /model/src/main/resources/META-INF/persistence.xml

<persistence-unit name="SimpleXMLCompletePU">
    ...

<enter/>
    <mapping-file>simple/Mappings.xml</mapping-file>
    <mapping-file>simple/Queries.xml</mapping-file>
    ...
</persistence-unit>

3.4. Summary

sitemap