1.
Many-To-One Association:
It is relation between entities, where,
multiple instance of an entity must be mapped with exactly one instance of
another entity.
Ex:-
Multiple student, have joined with single
Branch.
Ex code :-
class Branch
{
String
bid;
String
bname;
Branch(String
bid, String bname)
{
this.bid=bid;
this.bname=bname;
}
}
class Student
{
String
sid;
String
sname;
String
saddr;
Branch
branch;
Student(String
sid, String sname, String saddr, Branch branch)
{
this.sid=sid;
this.sname=sname;
this.saddr=saddr;
this.branch=branch;
}
public void
getStudentDetails()
{
System.out.println("Student
Details");
System.out.println("---------------");
System.out.println("Student
Id :"+sid);
System.out.println("Student
Name :"+sname);
System.out.println("Student
Address:"+saddr);
System.out.println("Branch
Id :"+branch.bid);
System.out.println("Branch
Name :"+branch.bname);
System.out.println();
}
}
class ManyToOneEx
{
public
static void main(String[] args)
{
Branch
branch=new Branch("B-111","CS");
Student
std1=new Student("S-111","AAA","Hyd",branch);
Student
std2=new Student("S-222","BBB","Hyd",branch);
Student
std3=new Student("S-333","CCC","Hyd",branch);
std1.getStudentDetails();
std2.getStudentDetails();
std3.getStudentDetails();
}
}
Output:-
Student Details
---------------
Student Id :S-111
Student Name :AAA
Student Address:Hyd
Branch Id :B-111
Branch Name :CS
Student Details
---------------
Student Id :S-222
Student Name :BBB
Student Address:Hyd
Branch Id :B-111
Branch Name :CS
Student Details
---------------
Student Id :S-333
Student Name :CCC
Student Address:Hyd
Branch Id :B-111
Branch Name :CS


No comments:
Post a Comment