Skip to content

Commit

Permalink
adding Association mapping examples
Browse files Browse the repository at this point in the history
  • Loading branch information
giriharan13 committed Nov 30, 2023
1 parent cf41449 commit e57ec99
Show file tree
Hide file tree
Showing 18 changed files with 547 additions and 0 deletions.
40 changes: 40 additions & 0 deletions mapping/association-mapping/ExampleApp/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
1 change: 1 addition & 0 deletions mapping/association-mapping/ExampleApp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target/
23 changes: 23 additions & 0 deletions mapping/association-mapping/ExampleApp/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>ExampleApp</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/test/java=UTF-8
encoding/<project>=UTF-8
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
40 changes: 40 additions & 0 deletions mapping/association-mapping/ExampleApp/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.giriharan</groupId>
<artifactId>ExampleApp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>ExampleApp</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.6.15.Final</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.mysql/mysql-connector-j -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.2.0</version>
</dependency>


</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -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<Customer> customers = new ArrayList<>();
customers.add(customer1);

Product product1 = new Product();
Product product2 = new Product();
List<Product> 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!");
}

}
Original file line number Diff line number Diff line change
@@ -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<Book> books = new ArrayList<Book>();
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!");
}

}
Original file line number Diff line number Diff line change
@@ -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!");

}
}
Original file line number Diff line number Diff line change
@@ -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;
}


}
Original file line number Diff line number Diff line change
@@ -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<Product> products;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public List<Product> getProducts() {
return products;
}

public void setProducts(List<Product> products) {
this.products = products;
}


}
Original file line number Diff line number Diff line change
@@ -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<Book> books;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public List<Book> getBooks() {
return books;
}

public void setBooks(List<Book> books) {
this.books = books;
}


}
Loading

0 comments on commit e57ec99

Please sign in to comment.