Powered by Blogger.

Wednesday, 9 July 2014

OneToMany and ManyToOne

By Shambhu Kumar  |  02:46 No comments

Annotation OneToMany
Target: Fields (including property get methods)
Defines a many-valued association with one-to-many multiplicity.
If the collection is defined using generics to specify the element type, the associated target entity type need not be specified; otherwise the target entity class must be specified. If the relationship is bidirectional, the mappedBy element must be used to specify the relationship field or property of the entity that is the owner of the relationship.
1.    Bidirectional
Customer.java

@OneToMany(mappedBy="customer")
private Set<Order> orders = new HashSet<Order>();
Order.java
@ManyToOne
@JoinColumn(name="CUST_ID", nullable=false)
private Customer customer;

2.            Unidirectional

Customer.java

@OneToMany(orphanRemoval=true)
@JoinColumn(name="CUST_ID") // join column is in table for Order

public Set<Order> getOrders() {return orders;}





Annotation ManyToOne
Target: Fields (including property get methods)
Defines a single-valued association to another entity class that has many-to-one multiplicity. It is not normally necessary to specify the target entity explicitly since it can usually be inferred from the type of the object being referenced. If the relationship is bidirectional, the non-owning OneToMany entity side must use the mappedBy element to specify the relationship field or property of the entity that is the owner of the relationship.


UserList.java
@ManyToOne(optional=false)
@JoinColumn(name="Active_ID1", nullable=false, updatable=false)
private  Active active;
@ManyToOne(optional=false)
@JoinColumn(name="Active_ID1", nullable=false, updatable=false)
public Active getActive() {
       return active;
}


Author: Shambhu Kumar

Hello, I am Author, decode to know more: In commodo magna nisl, ac porta turpis blandit quis. Lorem ipsum dolor sit amet, consectetur adipiscing elit. In commodo magna nisl, ac porta turpis blandit quis. Lorem ipsum dolor sit amet.

0 comments:

Recent Articles

© 2014 Learning Java. WP themonic converted by Bloggertheme9. Published By Gooyaabi Templates
TOP