Make delicious recipes!


OneToMany and ManyToOne annotations

OneToMany annotation can be used to specify a field whose type is List, Set or a Collection.
Example:
@OneToMany(cascade=ALL, mappedBy="customer")
Set<Order> orders;

It has following important attributes:
  1. targetEntity : Used to specify the class of the target entity.
    Must be specified if class is not specified in generic.
    For example,
    It must be specified for field:
    Set children;
    
    It should not be specified for field:
    Set<Child> children;
    

  2. mappedBy : Denotes the "field" in the "other" table whose primary-key is present as foreign-key in the "other" table.
    Example:
    @OneToMany(cascade=ALL, mappedBy="customer")
    Set<Order> orders;
    
    The mappedBy property will not work if there is no matching annotation on the entity on the other side.
    In the above case, the foreign key must be present as @ManyToOne annotation on the field "customer" in the entity "Order".
    @ManyToOne
    @JoinColumn(name="CUST_ID", nullable=false)
    Customer customer;
    






Like us on Facebook to remain in touch
with the latest in technology and tutorials!


Got a thought to share or found a
bug in the code?
We'd love to hear from you:

Name:
Email: (Your email is not shared with anybody)
Comment:

Facebook comments:

Site Owner: Sachin Goyal