Mapping a parent/children relationship (Web hosting companies) In JPA, you map
Tuesday, January 22nd, 2008Mapping a parent/children relationship In JPA, you map this association with the @ManyToOne annotation, either on the field or getter method, depending on the access strategy for the entity (determined by the position of the @Id annotation): public class Bid { … @ManyToOne( targetEntity = auction.model.Item.class ) @JoinColumn(name = “ITEM_ID”, nullable = false) private Item item; … } There are two optional elements in this mapping. First, you don t have to include the targetEntity of the association; it s implicit from the type of the field. An explicit targetEntity attribute is useful in more complex domain models for example, when you map a @ManyToOne on a getter method that returns a delegate class, which mimics a particular target entity interface. The second optional element is the @JoinColumn. If you don t declare the name of the foreign key column, Hibernate automatically uses a combination of the target entity name and the database identifier property name of the target entity. In other words, if you don t add a @JoinColumn annotation, the default name for the foreign key column is item plus id, separated with an underscore. However, because you want to make the foreign key column NOT NULL, you need the annotation anyway to set nullable = false. If you generate the schema with the Hibernate Tools, the optional=”false” attribute on the @ManyToOne would also result in a NOT NULL constraint on the generated column. This was easy. It s critically important to realize that you can write a complete application without using anything else. (Well, maybe a shared primary key one- to-one mapping from time to time, as shown in the next chapter.) You don t need to map the other side of this class association, and you ve already mapped everything present in the SQL schema (the foreign key column). If you need the Item instance for which a particular Bid was made, call aBid.getItem(), utilizing the entity association you created. On the other hand, if you need all bids that have been made for an item, you can write a query (in whatever language Hibernate supports). One of the reasons you use a full object/relational mapping tool like Hibernate is, of course, that you don t want to write that query.
If you are looking for affordable and reliable webhost to host and run your business application visit our ftp web hosting services.