Skip to main content

Mapping with Annotations

  • Chapter
  • First Online:
Beginning Hibernate

Abstract

Mappings can be created in two different ways: via inline annotations (as we’ve done through the book so far), or as separate XML files in one of two primary formats.

This is a preview of subscription content, log in via an institution to check access.

Access this chapter

Chapter
USD 29.95
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
eBook
USD 44.99
Price excludes VAT (USA)
  • Available as EPUB and PDF
  • Read on any device
  • Instant download
  • Own it forever

Tax calculation will be finalised at checkout

Purchases are for personal use only

Institutional subscriptions

Notes

  1. 1.

    Most IDEs can generate XML mappings for you; also see JBoss Tools ( http://tools.jboss.org/ ), XDoclet ( http://xdoclet.sourceforge.net/xdoclet/index.html ), and MyEclipse ( https://www.genuitec.com/products/myeclipse/ ) for other possibilities. With that said, most people prefer the annotations, for good reason.

  2. 2.

    There’s probably value in creating tests for your object model, which would, one hopes, eliminate this as a concern, but there’s no point in creating extra work for its own sake.

  3. 3.

    The JPA configuration was largely derived from Hibernate’s configuration specification, which preceded JPA.

  4. 4.

    Naturally, there’s a way around this. The normal requirement is to have a no-argument constructor; interceptors allow you to not have to do this.

  5. 5.

    The reasoning here is that if you needed something to be part of the object’s state, you’d include it in the database, which would mean you wouldn’t need to set it when the object was instantiated. With that said, your mileage may vary; do what works for you.

  6. 6.

    Actually, the default generator is the “assigned” generator, which means the application is responsible for assigning the primary key before save() is called. This ends up having the same effect as if no key generation is specified.

  7. 7.

    Serializable relies on Java’s introspection to serialize and deserialize data. Externalizable forces the author to implement explicit serialization mechanisms. Externalizable can be far faster, but this doesn’t give you any real benefits in this case.

  8. 8.

    However, the data for the class might not be initialized yet. When an instance is loaded from the session, the data might have been retrieved eagerly, but the object won’t be initialized until something in it has been requested. This can yield some “interesting” behaviors, some of which were exposed in the previous chapters.

  9. 9.

    This is not an endorsement of XML configuration. Use @Transient instead.

  10. 10.

    The ISBN example required an additional annotation for “group” because group is a reserved word in SQL.

  11. 11.

    You could also use this situation – where you want an embedded object spread across multiple tables – as a sign that maybe you’re not meant to use object-relational mapping for those entities. But the truth is that you’re probably mapping it incorrectly. Probably.

  12. 12.

    An association is bidirectional if each entity maintains a property or field representing its end of the same relationship. For example, if our Address class maintained a reference to the Publisher located there, and the Publisher class maintained a reference to its Address, then the association would be bidirectional.

  13. 13.

    When a join table is being used, the foreign key relationship is maintained within the join table itself – it is therefore not appropriate to combine the mappedBy attribute of the @OneToMany annotation with the use of a @JoinTable annotation.

  14. 14.

    That is to say, the highest class in the hierarchy that is mapped to the database as an entity should be annotated in this way.

  15. 15.

    Before you think this example is entirely contrived, some titles can be amazingly long. Check out   http://oldbooktitles.tumblr.com/ for some examples that might not fit in traditional-size columns.

  16. 16.

    Note that something being possible is very different from something being preferable. Your author knows of exactly zero instances of this feature having been used in production; this doesn’t mean it’s not ever been used, but it’s far from common.

  17. 17.

    Again, this is not an endorsement of the Hibernate XML configuration. It’s also not exactly a condemnation, but…

  18. 18.

    Note that there are different views of this. Most anecdotal data would suggest artificial keys as primary keys because they’re short and immutable by nature; however, you can find many who advocate natural keys because they naturally map to the data model instead of adding to it. With that said, there are some data-oriented applications (data warehousing, for example) in which artificial keys are advocated without opposition. With this in mind, the recommendation stands: use artificial keys. Your data is likely to be warehoused at some point.

  19. 19.

    The @UniqueConstraints annotation, mentioned earlier in this chapter, can do the same for a compound index. With that said, we’re trying to look at a better way to do at least some ordering and indexing.

  20. 20.

    Let’s assume that we usually make sense.

  21. 21.

    In the previous footnote, we said that we usually make sense. This is a good example of when we really don’t; this is horribly contrived and would earn a solid scolding in an actual project. With that said, the code works and is fairly demonstrative of the concept.

Author information

Authors and Affiliations

Authors

Rights and permissions

Reprints and permissions

Copyright information

© 2016 Joseph B. Ottinger, Jeff Linwood and Dave Minter

About this chapter

Cite this chapter

Ottinger, J.B., Linwood, J., Minter, D. (2016). Mapping with Annotations. In: Beginning Hibernate. Apress, Berkeley, CA. https://doi.org/10.1007/978-1-4842-2319-2_6

Download citation

Publish with us

Policies and ethics