Hibernate's one-to-many, multi-pair explanation

Source: Internet
Author: User

: Two-way one-to-many relationship, the first is the relationship maintenance side (owner side), mostly the relationship is maintained side (inverse side). you need to establish a foreign key column on the relationship maintenance side to point to the primary key column of the relationship maintenance side @JoinColumn .

Public classOrder implements Serializable { Privateset<orderitem> orderitems = new hashset<orderitem> (); ....      @ Onetomany (Mappedby= "order" ,cascade = Cascadetype. all fetch = Fetchtype. lazy @ by(value = "id ASC") Public set<orderitem> getorderitems () { returnorderitems;      }} Public class OrderItem implements Serializable { Privateorder order; .... @ manytoone(cascade=cascadetype. REFRESH, optional= false) @ joincolumn(name = "order_id") Public Order getorder () { returnorder;      }}@ ORDER BY (value = "id ASC") indicates an ascending sort by id when loading OrderItem @ onetomany Properties : 1> targetentity defines the type of the relationship class, which by default is the class type that corresponds to the member property, so you typically do not need to provide a definition. 2> mappedby defines a bidirectional relationship between classes. if the class is a one-way relationship, there is no need to provide a definition, if the class and class form a bidirectional relationship, we need to use this attribute to define, otherwise it may cause data consistency problem. The value of this property is "many" side class The "One" side of the variable name in the3> Cascade This property defines a cascade relationship between classes and classes. A defined cascade relationship is considered by the container to take the same action on the current class object and its associated class object, and this relationship is called recursively. For example:order and OrderItem have a cascading relationship, and when you delete an order, the OrderItem object that corresponds to it is deleted . If the OrderItem also has a cascade relationship with other objects, then the operation will continue to execute recursively. The value of cascade can only be from cascadetype.persist(Cascade New),cascadetype.remove(cascade delete),Cascadetype.refresh (cascade refresh),cascadetype.merge(cascading update), select one or more. Another option is to use Cascadetype.all, which means that all four items are selected. 4> fatch Selectable items include:fetchtype.eager and fetchtype.lazy. The former indicates that the relationship class ( in this case, the OrderItem Class) is loaded at the same time as the main class (in this case, the Order class ) . The latter indicates that the relationship class is loaded only when it is accessed. The default value is fetchtype.lazy. @ joincolumn(name = "order_id") note specifies that the order_id column of the OrderItem mapping table is associated with the primary key column of the Order mapping table as a foreign key. @ manytoone: Indicates a many-to-one relationship between OrderItem and order. @ManyToOne Note has four properties: targetentity,Cascade, Fetch, and optional, The exact meaning of the first three attributes is the same as the property with the same name as @onetomany, but the default value for the Fetch property of the @ManyToOne is Fetchtype.eager. OptionalProperty is defined if the associated class must exist and the value is false, both sides of the associated class must exist, and the result of the query is null if the relationship is not present on the maintenance side. When the value is true, the relationship is maintained and the result of the query is still returned to the maintenance side of the relationship, and the property that points to the maintenance side of the relationship on the relationship maintenance side is null. The default value of the optional property is true. The optional property actually specifies a join query relationship between the associated class and the associated class , such as Optional=false when the join query relationship is INNER join, optional=true The join query relationship is a LEFT join. The following code snippet is interpreted as follows: It is important to emphasize that when a business method needs to return an entity bean as a parameter to the client, except that the entity Bean itself needs to implement the Serializable interface , if the association class (OrderItem) is a deferred load, and you need to return the entity Bean the associated class was previously loaded by accessing the associated class (see the following example). Otherwise, the load exception will be thrown when the client accesses the associated class. Public Order Getorderbyid (Integer OrderID) { Order order = em. Find (Order.         Class, OrderID);        //!!!!! because it is a deferred load, all order items under the order are obtained by executing size () order.getorderitems (). Size (); return order;      } In addition, whether or not to delay loading, Join Fetch Associative statements can explicitly load an association class, as in the following example:Public List getallorder () { query query = em. CreateQuery ("select DISTINCT o from Order o inner Join fetch O.orderitems ORDER by O.orderid "); List result = Query.getresultlist (); return result;      }

Hibernate's one-to-many, multi-pair explanation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.