Solution What will be the steps Steps to get the output? • Step-4: Write an application program to persist address objects in the database as compositeDemo method. Mention the package name, and import all the necessary classes. Create your driver class and create the main method to call the compositeDemo. • Step-5: Now, define the compositeDemo method. • Step-6: Close all the classes and methods brackets and execute the program.
Code Code for Step 1: public class Certificate { private String course; private String university; public Certificate() {} public Certificate(String course, String university) { super(); this.course = course;
Code this.university = university; } public String getCourse() { return course; } public void setCourse(String course) { this.course = course; } public String getUniversity() { return university; }
Code public void setUniversity(String university) { this.university = university; } } Code for Step 2: @Entity public class Student { private int id; private String name; @EmbeddedId private Certificate cert;
Code public Student() {} public Student(int id, String name, Certificate cert) { super(); this.id = id; this.name = name; this.cert = cert; } public int getId() { return id; }
Code public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Certificate getCert() { return cert; }
Code public void setCert(Certificate cert) { this.cert = cert; } @Override public String toString() { return \"Student [id=\" + id + \", name=\" + name + \", course=\" + cert.getCourse() + \", university=\" + cert.getUniversity() + \"]\"; } }
Code Code for Step 3: <mapping class=\"com.TechLease.Student\"/> package com.TechLease; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; public class App { public static void main( String[] args )
Code { compositeDemo(); } Code for Step 5: public static void compositeDemo() { Configuration cfg = new Configuration(); cfg.configure(\"hibernate.cfg.xml\"); SessionFactory factory = cfg.buildSessionFactory(); Session session = factory.openSession(); Transaction t = session.beginTransaction();
Code Certificate c1 = new Certificate(\"MBA\",\"Delhi University\"); Certificate c2 = new Certificate(\"BCom\",\"Pune University\"); Student s1 = new Student(11,\"Sanjay\",c1); Student s2 = new Student(12,\"Sunita\",c2); System.out.println(s1); System.out.println(2); session.save(s1); session.save(s2); t.commit(); session.close(); }
Output Output for the Addexterxctihserwe ill be The output of the application:
Learning Outcome After Performing this Add teExxt ehrecriese You will be able to: ⦁ Map composite key ⦁ Use of @EmbeddedId annotation
Implement Annotations
Problem What will you do? Statement • Create an Address class • Use @Entity and @Table annotations on the class • Define id, street, city, and Pincode as properties • The id property will be the primary key, hence use @Id and @GeneratedValue annotation • Use @Column annotation on the remaining properties
Solution What will be the steps Steps to get the output? • Step-1: Firstly, let us create the Address class. Map this class using the @Entity property. Specify the name of the table using @Table annotation. Define id (int type), street (String type), city (String type), and pincode (long type) as properties. Further, id property will be the primary key so let us use @Id annotation on this property. Also, use @GeneratedValue annotation to specify the strategy of generating the primary key. Specify column names in the database through @Column annotation for each attribute. Also, make sure to import the annotations at every step. • Step-2: Next, define the default constructor and parameterized constructor. Define getter and setter methods for the properties. Override toString method to display the address properties in a systematic fashion. • Step-3: In the configuration file, mention the class to map using the mapping element.
Solution What will be the steps Steps to get the output? • Step-4: Write an application program to persist address objects in the database as AnnotationDemo method. Mention the package name, and import all the necessary classes. Create your driver class and create main method to call the AnnotationDemo. • Step-5: Now, define the annotationDemo method.
Code Code for Step 1: package com.TechLease; import javac.persistence.Column; import javac.persistence.Entity; import javac.persistence.GeneratedValue; import javac.persistence.GenerationType; import javac.persistence.Id; import javac.persistence.Table; @Entity @Table(name=\"EMPLOYEE_ADDRESS\")
Code public class Address { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; @Column(length=40,name=\"STREET\") private String street; @Column(length=30,name=\"CITY\") private String city; @Column(length=10,name=\"PINCODE\") private long pincode;
Code Code for step 2: public Address() {} public Address(int id, String street, String city, double pincode) { this.id = id; this.street = street; this.city = city; this.pincode = pincode; } public int getId() { return id; }
Code public void setId(int id) { this.id = id; } public String getStreet() { return street; } public void setStreet(String street) { this.street = street; }
Code public String getCity() { return city; } public void setCity(String city) { this.city = city; } public double getPincode() { return pincode; }
Code public void setPincode(double pincode) { this.pincode = pincode; } @Override public String toString() { return \"Address [id=\" + id + \", street=\" + street + \", city=\" + city + \", pincode=\" + pincode + \"]\"; } } Code for Step 3: <mapping class=\"com.TechLease.Address\"/>
Code Code for Step 4: package com.TechLease; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; public class App { public static void main( String[] args ) { annotationDemo(); }
Code Code for Step 5: public static void annotationDemo() { Configuration cfg = new Configuration(); cfg.configure(\"hibernate.cfg.xml\"); SessionFactory factory = cfg.buildSessionFactory(); Session session = factory.openSession(); Transaction t = session.beginTransaction(); Address a1 = new Address(201,\"KG Road\",\"Mumbai\",400252); Address a2 = new Address(202,\"Ram Nagar\",\"Pune\",400516); System.out.println(a1);
Code System.out.println(a2); session.save(a1); session.save(a2); t.commit(); session.close(); }
Output Output for the Addexterxctihserwe ill be Output of the application:
Learning Outcome After Performing AddttheixstEhxeerercise You will be able to work with: ⦁ @Table, @Entity, @Column annotations ⦁ @Id and @GeneratedValue annotations
Happy Learning!
Search