From e57ec9981fecd5a421663d952d35beb6d90f6e23 Mon Sep 17 00:00:00 2001 From: giriharan13 Date: Fri, 1 Dec 2023 00:47:13 +0530 Subject: [PATCH] adding Association mapping examples --- .../association-mapping/ExampleApp/.classpath | 40 ++++++++++++++ .../association-mapping/ExampleApp/.gitignore | 1 + .../association-mapping/ExampleApp/.project | 23 ++++++++ .../org.eclipse.core.resources.prefs | 4 ++ .../.settings/org.eclipse.jdt.core.prefs | 8 +++ .../.settings/org.eclipse.m2e.core.prefs | 4 ++ .../association-mapping/ExampleApp/pom.xml | 40 ++++++++++++++ .../java/com/giriharan/ExampleApp/AppMTM.java | 47 +++++++++++++++++ .../com/giriharan/ExampleApp/AppOTM_MTO.java | 41 +++++++++++++++ .../java/com/giriharan/ExampleApp/AppOTO.java | 33 ++++++++++++ .../java/com/giriharan/ExampleApp/Book.java | 52 +++++++++++++++++++ .../com/giriharan/ExampleApp/Customer.java | 42 +++++++++++++++ .../com/giriharan/ExampleApp/Library.java | 40 ++++++++++++++ .../com/giriharan/ExampleApp/Passport.java | 34 ++++++++++++ .../java/com/giriharan/ExampleApp/Person.java | 50 ++++++++++++++++++ .../com/giriharan/ExampleApp/Product.java | 37 +++++++++++++ .../src/main/java/hibernate.cfg.xml | 13 +++++ .../com/giriharan/ExampleApp/AppTest.java | 38 ++++++++++++++ 18 files changed, 547 insertions(+) create mode 100644 mapping/association-mapping/ExampleApp/.classpath create mode 100644 mapping/association-mapping/ExampleApp/.gitignore create mode 100644 mapping/association-mapping/ExampleApp/.project create mode 100644 mapping/association-mapping/ExampleApp/.settings/org.eclipse.core.resources.prefs create mode 100644 mapping/association-mapping/ExampleApp/.settings/org.eclipse.jdt.core.prefs create mode 100644 mapping/association-mapping/ExampleApp/.settings/org.eclipse.m2e.core.prefs create mode 100644 mapping/association-mapping/ExampleApp/pom.xml create mode 100644 mapping/association-mapping/ExampleApp/src/main/java/com/giriharan/ExampleApp/AppMTM.java create mode 100644 mapping/association-mapping/ExampleApp/src/main/java/com/giriharan/ExampleApp/AppOTM_MTO.java create mode 100644 mapping/association-mapping/ExampleApp/src/main/java/com/giriharan/ExampleApp/AppOTO.java create mode 100644 mapping/association-mapping/ExampleApp/src/main/java/com/giriharan/ExampleApp/Book.java create mode 100644 mapping/association-mapping/ExampleApp/src/main/java/com/giriharan/ExampleApp/Customer.java create mode 100644 mapping/association-mapping/ExampleApp/src/main/java/com/giriharan/ExampleApp/Library.java create mode 100644 mapping/association-mapping/ExampleApp/src/main/java/com/giriharan/ExampleApp/Passport.java create mode 100644 mapping/association-mapping/ExampleApp/src/main/java/com/giriharan/ExampleApp/Person.java create mode 100644 mapping/association-mapping/ExampleApp/src/main/java/com/giriharan/ExampleApp/Product.java create mode 100644 mapping/association-mapping/ExampleApp/src/main/java/hibernate.cfg.xml create mode 100644 mapping/association-mapping/ExampleApp/src/test/java/com/giriharan/ExampleApp/AppTest.java diff --git a/mapping/association-mapping/ExampleApp/.classpath b/mapping/association-mapping/ExampleApp/.classpath new file mode 100644 index 0000000..f7e4a1d --- /dev/null +++ b/mapping/association-mapping/ExampleApp/.classpath @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/mapping/association-mapping/ExampleApp/.gitignore b/mapping/association-mapping/ExampleApp/.gitignore new file mode 100644 index 0000000..b83d222 --- /dev/null +++ b/mapping/association-mapping/ExampleApp/.gitignore @@ -0,0 +1 @@ +/target/ diff --git a/mapping/association-mapping/ExampleApp/.project b/mapping/association-mapping/ExampleApp/.project new file mode 100644 index 0000000..cbabd4c --- /dev/null +++ b/mapping/association-mapping/ExampleApp/.project @@ -0,0 +1,23 @@ + + + ExampleApp + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/mapping/association-mapping/ExampleApp/.settings/org.eclipse.core.resources.prefs b/mapping/association-mapping/ExampleApp/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..f9fe345 --- /dev/null +++ b/mapping/association-mapping/ExampleApp/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,4 @@ +eclipse.preferences.version=1 +encoding//src/main/java=UTF-8 +encoding//src/test/java=UTF-8 +encoding/=UTF-8 diff --git a/mapping/association-mapping/ExampleApp/.settings/org.eclipse.jdt.core.prefs b/mapping/association-mapping/ExampleApp/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..2f5cc74 --- /dev/null +++ b/mapping/association-mapping/ExampleApp/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,8 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore +org.eclipse.jdt.core.compiler.release=disabled +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/mapping/association-mapping/ExampleApp/.settings/org.eclipse.m2e.core.prefs b/mapping/association-mapping/ExampleApp/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 0000000..f897a7f --- /dev/null +++ b/mapping/association-mapping/ExampleApp/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/mapping/association-mapping/ExampleApp/pom.xml b/mapping/association-mapping/ExampleApp/pom.xml new file mode 100644 index 0000000..fadf36c --- /dev/null +++ b/mapping/association-mapping/ExampleApp/pom.xml @@ -0,0 +1,40 @@ + + 4.0.0 + + com.giriharan + ExampleApp + 0.0.1-SNAPSHOT + jar + + ExampleApp + http://maven.apache.org + + + UTF-8 + + + + + junit + junit + 3.8.1 + test + + + + org.hibernate + hibernate-core + 5.6.15.Final + + + + + com.mysql + mysql-connector-j + 8.2.0 + + + + + diff --git a/mapping/association-mapping/ExampleApp/src/main/java/com/giriharan/ExampleApp/AppMTM.java b/mapping/association-mapping/ExampleApp/src/main/java/com/giriharan/ExampleApp/AppMTM.java new file mode 100644 index 0000000..4285a90 --- /dev/null +++ b/mapping/association-mapping/ExampleApp/src/main/java/com/giriharan/ExampleApp/AppMTM.java @@ -0,0 +1,47 @@ +package com.giriharan.ExampleApp; + +import java.util.ArrayList; +import java.util.List; + +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.hibernate.Transaction; +import org.hibernate.cfg.Configuration; + +public class AppMTM { + public static void main(String[] args) { + Configuration cfg = new Configuration().configure("hibernate.cfg.xml").addAnnotatedClass(Customer.class).addAnnotatedClass(Product.class); + SessionFactory sf = cfg.buildSessionFactory(); + + Session session = sf.openSession(); + Transaction transaction = session.beginTransaction(); + + Customer customer1 = new Customer(); + List customers = new ArrayList<>(); + customers.add(customer1); + + Product product1 = new Product(); + Product product2 = new Product(); + List products = new ArrayList<>(); + product1.setCustomers(customers); + product2.setCustomers(customers); + customer1.setProducts(products); + products.add(product1); + products.add(product2); + + /* Before cascading + session.save(customer1); + session.save(product1); + session.save(product2); */ + + // After cascading + session.save(customer1); + + + transaction.commit(); + session.close(); + + System.out.println("Successfully added!"); + } + +} diff --git a/mapping/association-mapping/ExampleApp/src/main/java/com/giriharan/ExampleApp/AppOTM_MTO.java b/mapping/association-mapping/ExampleApp/src/main/java/com/giriharan/ExampleApp/AppOTM_MTO.java new file mode 100644 index 0000000..cc81cd7 --- /dev/null +++ b/mapping/association-mapping/ExampleApp/src/main/java/com/giriharan/ExampleApp/AppOTM_MTO.java @@ -0,0 +1,41 @@ +package com.giriharan.ExampleApp; + +import java.util.ArrayList; +import java.util.List; + +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.hibernate.Transaction; +import org.hibernate.cfg.Configuration; + +public class AppOTM_MTO { + public static void main(String args[]) { + Configuration cfg = new Configuration().configure("hibernate.cfg.xml").addAnnotatedClass(Library.class).addAnnotatedClass(Book.class); + SessionFactory sf = cfg.buildSessionFactory(); + + Session session = sf.openSession(); + Transaction transaction = session.beginTransaction(); + + Library library = new Library(); + Book book1 = new Book("God of war : Ragnarok"); + Book book2 = new Book("Red Dead Redemption II"); + book1.setLibrary(library); + book2.setLibrary(library); + + List books = new ArrayList(); + books.add(book1); + books.add(book2); + + library.setBooks(books); + + session.save(book1); + session.save(book2); + session.save(library); + + transaction.commit(); + session.close(); + + System.out.println("Successfully added!"); + } + +} diff --git a/mapping/association-mapping/ExampleApp/src/main/java/com/giriharan/ExampleApp/AppOTO.java b/mapping/association-mapping/ExampleApp/src/main/java/com/giriharan/ExampleApp/AppOTO.java new file mode 100644 index 0000000..124c7f6 --- /dev/null +++ b/mapping/association-mapping/ExampleApp/src/main/java/com/giriharan/ExampleApp/AppOTO.java @@ -0,0 +1,33 @@ +package com.giriharan.ExampleApp; + +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.hibernate.Transaction; +import org.hibernate.cfg.Configuration; + +public class AppOTO +{ + public static void main( String[] args ) + { + Configuration cfg = new Configuration().configure("hibernate.cfg.xml").addAnnotatedClass(Person.class).addAnnotatedClass(Passport.class); + SessionFactory sf = cfg.buildSessionFactory(); + + Session session = sf.openSession(); + Transaction transaction = session.beginTransaction(); + + + Person person = new Person("Arjun"); + Passport passport = new Passport(); + person.setPassport(passport); + passport.setPerson(person); + + session.save(person); + session.save(passport); + + transaction.commit(); + session.close(); + + System.out.println("Successfully added!"); + + } +} diff --git a/mapping/association-mapping/ExampleApp/src/main/java/com/giriharan/ExampleApp/Book.java b/mapping/association-mapping/ExampleApp/src/main/java/com/giriharan/ExampleApp/Book.java new file mode 100644 index 0000000..5c9653b --- /dev/null +++ b/mapping/association-mapping/ExampleApp/src/main/java/com/giriharan/ExampleApp/Book.java @@ -0,0 +1,52 @@ +package com.giriharan.ExampleApp; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.ManyToOne; + +@Entity +public class Book { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private int id; + + @Column(name="name") + private String name; + + @ManyToOne + private Library library; + + public Book(String name) { + super(); + this.name = name; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Library getLibrary() { + return library; + } + + public void setLibrary(Library library) { + this.library = library; + } + + +} diff --git a/mapping/association-mapping/ExampleApp/src/main/java/com/giriharan/ExampleApp/Customer.java b/mapping/association-mapping/ExampleApp/src/main/java/com/giriharan/ExampleApp/Customer.java new file mode 100644 index 0000000..bd3733c --- /dev/null +++ b/mapping/association-mapping/ExampleApp/src/main/java/com/giriharan/ExampleApp/Customer.java @@ -0,0 +1,42 @@ +package com.giriharan.ExampleApp; + +import java.util.List; + +import javax.persistence.CascadeType; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.JoinTable; +import javax.persistence.ManyToMany; + + +@Entity +public class Customer { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private int id; + + @ManyToMany(cascade = CascadeType.ALL) + @JoinTable(joinColumns = @JoinColumn(name="customer_id"), inverseJoinColumns = @JoinColumn(name="product_id")) + private List products; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public List getProducts() { + return products; + } + + public void setProducts(List products) { + this.products = products; + } + + +} diff --git a/mapping/association-mapping/ExampleApp/src/main/java/com/giriharan/ExampleApp/Library.java b/mapping/association-mapping/ExampleApp/src/main/java/com/giriharan/ExampleApp/Library.java new file mode 100644 index 0000000..2505890 --- /dev/null +++ b/mapping/association-mapping/ExampleApp/src/main/java/com/giriharan/ExampleApp/Library.java @@ -0,0 +1,40 @@ +package com.giriharan.ExampleApp; + +import java.util.List; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.JoinTable; +import javax.persistence.OneToMany; + +@Entity +public class Library { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private int id; + + @OneToMany + @JoinTable(name = "library_tb_book_tb", joinColumns = @JoinColumn(name = "library_id"),inverseJoinColumns = @JoinColumn(name = "book_id")) + private List books; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public List getBooks() { + return books; + } + + public void setBooks(List books) { + this.books = books; + } + + +} diff --git a/mapping/association-mapping/ExampleApp/src/main/java/com/giriharan/ExampleApp/Passport.java b/mapping/association-mapping/ExampleApp/src/main/java/com/giriharan/ExampleApp/Passport.java new file mode 100644 index 0000000..a331656 --- /dev/null +++ b/mapping/association-mapping/ExampleApp/src/main/java/com/giriharan/ExampleApp/Passport.java @@ -0,0 +1,34 @@ +package com.giriharan.ExampleApp; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.OneToOne; + +@Entity +public class Passport { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private int id; + + @OneToOne // annotation for one-to-one mapping + private Person person; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public Person getPerson() { + return person; + } + + public void setPerson(Person person) { + this.person = person; + } + +} diff --git a/mapping/association-mapping/ExampleApp/src/main/java/com/giriharan/ExampleApp/Person.java b/mapping/association-mapping/ExampleApp/src/main/java/com/giriharan/ExampleApp/Person.java new file mode 100644 index 0000000..8f444d5 --- /dev/null +++ b/mapping/association-mapping/ExampleApp/src/main/java/com/giriharan/ExampleApp/Person.java @@ -0,0 +1,50 @@ +package com.giriharan.ExampleApp; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.OneToOne; + +@Entity +public class Person { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private int id; + + @Column(name = "name") + private String name; + + @OneToOne // annotation for one-to-one mapping + private Passport passport; + + public Person(String name) { + this.name = name; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Passport getPassport() { + return passport; + } + + public void setPassport(Passport passport) { + this.passport = passport; + } + +} diff --git a/mapping/association-mapping/ExampleApp/src/main/java/com/giriharan/ExampleApp/Product.java b/mapping/association-mapping/ExampleApp/src/main/java/com/giriharan/ExampleApp/Product.java new file mode 100644 index 0000000..26636f4 --- /dev/null +++ b/mapping/association-mapping/ExampleApp/src/main/java/com/giriharan/ExampleApp/Product.java @@ -0,0 +1,37 @@ +package com.giriharan.ExampleApp; + +import java.util.List; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.ManyToMany; + +@Entity +public class Product { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private int product; + + @ManyToMany(mappedBy = "products") + private List customers; + + public int getProduct() { + return product; + } + + public void setProduct(int product) { + this.product = product; + } + + public List getCustomers() { + return customers; + } + + public void setCustomers(List customers) { + this.customers = customers; + } + + +} diff --git a/mapping/association-mapping/ExampleApp/src/main/java/hibernate.cfg.xml b/mapping/association-mapping/ExampleApp/src/main/java/hibernate.cfg.xml new file mode 100644 index 0000000..4497f2a --- /dev/null +++ b/mapping/association-mapping/ExampleApp/src/main/java/hibernate.cfg.xml @@ -0,0 +1,13 @@ + + + + + Password@123 + jdbc:mysql://localhost:3306/mapping?createDatabaseIfNotExist=true + admin + true + update + + diff --git a/mapping/association-mapping/ExampleApp/src/test/java/com/giriharan/ExampleApp/AppTest.java b/mapping/association-mapping/ExampleApp/src/test/java/com/giriharan/ExampleApp/AppTest.java new file mode 100644 index 0000000..ac41e68 --- /dev/null +++ b/mapping/association-mapping/ExampleApp/src/test/java/com/giriharan/ExampleApp/AppTest.java @@ -0,0 +1,38 @@ +package com.giriharan.ExampleApp; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Unit test for simple App. + */ +public class AppTest + extends TestCase +{ + /** + * Create the test case + * + * @param testName name of the test case + */ + public AppTest( String testName ) + { + super( testName ); + } + + /** + * @return the suite of tests being tested + */ + public static Test suite() + { + return new TestSuite( AppTest.class ); + } + + /** + * Rigourous Test :-) + */ + public void testApp() + { + assertTrue( true ); + } +}