From a6d4fadbc28c7b0cf294bf96dbe4fd4a634e9480 Mon Sep 17 00:00:00 2001 From: giriharan13 Date: Fri, 1 Dec 2023 20:16:48 +0530 Subject: [PATCH] adding examples for Collection mapping --- mapping/collection-mapping/.gitignore | 1 + .../collection-mapping/ExampleApp/.classpath | 40 ++++++++++++++ .../collection-mapping/ExampleApp/.gitignore | 1 + .../collection-mapping/ExampleApp/.project | 23 ++++++++ .../org.eclipse.core.resources.prefs | 4 ++ .../.settings/org.eclipse.jdt.core.prefs | 8 +++ .../.settings/org.eclipse.m2e.core.prefs | 4 ++ mapping/collection-mapping/ExampleApp/pom.xml | 38 ++++++++++++++ .../java/com/giriharan/ExampleApp/App.java | 34 ++++++++++++ .../com/giriharan/ExampleApp/Student.java | 52 +++++++++++++++++++ .../src/main/java/hibernate.cfg.xml | 13 +++++ .../com/giriharan/ExampleApp/AppTest.java | 38 ++++++++++++++ 12 files changed, 256 insertions(+) create mode 100644 mapping/collection-mapping/.gitignore create mode 100644 mapping/collection-mapping/ExampleApp/.classpath create mode 100644 mapping/collection-mapping/ExampleApp/.gitignore create mode 100644 mapping/collection-mapping/ExampleApp/.project create mode 100644 mapping/collection-mapping/ExampleApp/.settings/org.eclipse.core.resources.prefs create mode 100644 mapping/collection-mapping/ExampleApp/.settings/org.eclipse.jdt.core.prefs create mode 100644 mapping/collection-mapping/ExampleApp/.settings/org.eclipse.m2e.core.prefs create mode 100644 mapping/collection-mapping/ExampleApp/pom.xml create mode 100644 mapping/collection-mapping/ExampleApp/src/main/java/com/giriharan/ExampleApp/App.java create mode 100644 mapping/collection-mapping/ExampleApp/src/main/java/com/giriharan/ExampleApp/Student.java create mode 100644 mapping/collection-mapping/ExampleApp/src/main/java/hibernate.cfg.xml create mode 100644 mapping/collection-mapping/ExampleApp/src/test/java/com/giriharan/ExampleApp/AppTest.java diff --git a/mapping/collection-mapping/.gitignore b/mapping/collection-mapping/.gitignore new file mode 100644 index 0000000..e10e727 --- /dev/null +++ b/mapping/collection-mapping/.gitignore @@ -0,0 +1 @@ +/.metadata/ diff --git a/mapping/collection-mapping/ExampleApp/.classpath b/mapping/collection-mapping/ExampleApp/.classpath new file mode 100644 index 0000000..f7e4a1d --- /dev/null +++ b/mapping/collection-mapping/ExampleApp/.classpath @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/mapping/collection-mapping/ExampleApp/.gitignore b/mapping/collection-mapping/ExampleApp/.gitignore new file mode 100644 index 0000000..b83d222 --- /dev/null +++ b/mapping/collection-mapping/ExampleApp/.gitignore @@ -0,0 +1 @@ +/target/ diff --git a/mapping/collection-mapping/ExampleApp/.project b/mapping/collection-mapping/ExampleApp/.project new file mode 100644 index 0000000..cbabd4c --- /dev/null +++ b/mapping/collection-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/collection-mapping/ExampleApp/.settings/org.eclipse.core.resources.prefs b/mapping/collection-mapping/ExampleApp/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..f9fe345 --- /dev/null +++ b/mapping/collection-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/collection-mapping/ExampleApp/.settings/org.eclipse.jdt.core.prefs b/mapping/collection-mapping/ExampleApp/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..2f5cc74 --- /dev/null +++ b/mapping/collection-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/collection-mapping/ExampleApp/.settings/org.eclipse.m2e.core.prefs b/mapping/collection-mapping/ExampleApp/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 0000000..f897a7f --- /dev/null +++ b/mapping/collection-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/collection-mapping/ExampleApp/pom.xml b/mapping/collection-mapping/ExampleApp/pom.xml new file mode 100644 index 0000000..03270cb --- /dev/null +++ b/mapping/collection-mapping/ExampleApp/pom.xml @@ -0,0 +1,38 @@ + + 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.1.0 + + + + diff --git a/mapping/collection-mapping/ExampleApp/src/main/java/com/giriharan/ExampleApp/App.java b/mapping/collection-mapping/ExampleApp/src/main/java/com/giriharan/ExampleApp/App.java new file mode 100644 index 0000000..2949281 --- /dev/null +++ b/mapping/collection-mapping/ExampleApp/src/main/java/com/giriharan/ExampleApp/App.java @@ -0,0 +1,34 @@ +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 App +{ + public static void main( String[] args ) + { + Configuration cfg = new Configuration().configure("hibernate.cfg.xml").addAnnotatedClass(Student.class); + SessionFactory sf = cfg.buildSessionFactory(); + + Session session = sf.openSession(); + Transaction transaction = session.beginTransaction(); + + List classmates = new ArrayList<>(); + classmates.add("Takemichi"); + classmates.add("Draken"); + + Student student = new Student("Mikey",classmates); + + session.save(student); + + transaction.commit(); + session.close(); + + System.out.println("Successfully added!"); + } +} diff --git a/mapping/collection-mapping/ExampleApp/src/main/java/com/giriharan/ExampleApp/Student.java b/mapping/collection-mapping/ExampleApp/src/main/java/com/giriharan/ExampleApp/Student.java new file mode 100644 index 0000000..3a7d1d0 --- /dev/null +++ b/mapping/collection-mapping/ExampleApp/src/main/java/com/giriharan/ExampleApp/Student.java @@ -0,0 +1,52 @@ +package com.giriharan.ExampleApp; + +import java.util.List; + +import javax.persistence.CollectionTable; +import javax.persistence.Column; +import javax.persistence.ElementCollection; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.OrderColumn; + +@Entity +public class Student { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private int id; + + @Column(name="name") + private String name; + + @ElementCollection + @CollectionTable(name="classmates_table",joinColumns = @JoinColumn(name="sid")) + @Column(name="classmates") + @OrderColumn(name="pos") + private List classmates; + + + + public Student(String name, List classmates) { + this.name = name; + this.classmates = classmates; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public List getClassmates() { + return classmates; + } + + public void setClassmates(List classmates) { + this.classmates = classmates; + } +} diff --git a/mapping/collection-mapping/ExampleApp/src/main/java/hibernate.cfg.xml b/mapping/collection-mapping/ExampleApp/src/main/java/hibernate.cfg.xml new file mode 100644 index 0000000..4e0c241 --- /dev/null +++ b/mapping/collection-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/collection-mapping/ExampleApp/src/test/java/com/giriharan/ExampleApp/AppTest.java b/mapping/collection-mapping/ExampleApp/src/test/java/com/giriharan/ExampleApp/AppTest.java new file mode 100644 index 0000000..ac41e68 --- /dev/null +++ b/mapping/collection-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 ); + } +}