Skip to content

Commit

Permalink
one to one PK
Browse files Browse the repository at this point in the history
  • Loading branch information
Nareshit authored and Nareshit committed Apr 29, 2021
1 parent b1b430a commit 938c4eb
Show file tree
Hide file tree
Showing 29 changed files with 660 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#Generated by Maven Integration for Eclipse
#Mon Apr 26 09:44:46 IST 2021
#Thu Apr 29 09:13:13 IST 2021
m2e.projectLocation=C\:\\Users\\Nareshit\\git\\NTHB915\\HBProj77-OneToOneFK
m2e.projectName=HBProj77-OneToOneFK
groupId=nit
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Generated by Maven Integration for Eclipse
#Thu Apr 22 09:36:08 IST 2021
m2e.projectLocation=G\:\\Worskpaces\\Hibernate\\NTHB915\\HBProj78-OneToOnePK
#Thu Apr 29 09:13:17 IST 2021
m2e.projectLocation=C\:\\Users\\Nareshit\\git\\NTHB915\\HBProj78-OneToOnePK
m2e.projectName=HBProj78-OneToOnePK
groupId=nit
artifactId=HBProj75-ManyToManyBi
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Generated by Maven Integration for Eclipse
#Mon Apr 26 09:08:43 IST 2021
m2e.projectLocation=G\:\\Worskpaces\\Hibernate\\NTHB915\\HBProj86Anno-ManyToManyBi-CollectionId
#Thu Apr 29 09:13:18 IST 2021
m2e.projectLocation=C\:\\Users\\Nareshit\\git\\NTHB915\\HBProj86Anno-ManyToManyBi-CollectionId
m2e.projectName=HBProj86Anno-ManyToManyBi-CollectionId
groupId=nit
artifactId=HBProj75-ManyToManyBi
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Generated by Maven Integration for Eclipse
#Mon Apr 26 09:45:06 IST 2021
m2e.projectLocation=G\:\\Worskpaces\\Hibernate\\NTHB915\\HBProj87Anno-OneToOneFK
#Thu Apr 29 09:13:19 IST 2021
m2e.projectLocation=C\:\\Users\\Nareshit\\git\\NTHB915\\HBProj87Anno-OneToOneFK
m2e.projectName=HBProj87Anno-OneToOneFK
groupId=nit
artifactId=HBProj75-ManyToManyBi
Expand Down
27 changes: 27 additions & 0 deletions HBProj88Anno-OneToOnePK/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?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 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 kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-14">
<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>
23 changes: 23 additions & 0 deletions HBProj88Anno-OneToOnePK/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>HBProj88Anno-OneToOnePK</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
8 changes: 8 additions & 0 deletions HBProj88Anno-OneToOnePK/.settings/org.eclipse.jdt.core.prefs
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=14
org.eclipse.jdt.core.compiler.compliance=14
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=14
4 changes: 4 additions & 0 deletions HBProj88Anno-OneToOnePK/.settings/org.eclipse.m2e.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
102 changes: 102 additions & 0 deletions HBProj88Anno-OneToOnePK/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?xml version="1.0" encoding="UTF-8"?>

<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>nit</groupId>
<artifactId>HBProj75-ManyToManyBi</artifactId>
<version>0.0.1-SNAPSHOT</version>

<name>HBProj75-ManyToManyBi</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>14</maven.compiler.source>
<maven.compiler.target>14</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.30.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-hikaricp -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-hikaricp</artifactId>
<version>5.4.30.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.oracle.ojdbc/ojdbc8 -->
<dependency>
<groupId>com.oracle.ojdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>19.3.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<scope>provided</scope>
</dependency>


</dependencies>

<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Connection properties (mandatory except driver class name) -->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
<property name="connection.username">system</property>
<property name="connection.password">manager</property>

<!-- <property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql:///nthb915db</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
-->

<!-- Connection provider to work with Proxool -->
<property name="connection.provider_class">org.hibernate.hikaricp.internal.HikariCPConnectionProvider</property>

<!-- HikariCP settings -->
<property name="hikari.connectionTimeout">50000</property>
<property name="hibernate.hikari.minimumIdle">10</property>
<property name="hibernate.hikari.maximumPoolSize">20</property>
<property name="hibernate.hikari.idleTimeout">30000</property>


<!-- Hibernate properties (optional) -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<!-- mapping classes -->
<mapping class="com.nt.entity.Student"/>
<mapping class="com.nt.entity.LibraryMembership"/>
</session-factory>
</hibernate-configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.nt.dao;

public interface OneToOnePKDAO {
public void saveDataUsingParent();
public void loadDataUsingChild();
public void removeDataUsingChild();


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.nt.dao;

import java.time.LocalDateTime;
import java.util.List;

import javax.persistence.Query;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.nt.entity.LibraryMembership;
import com.nt.entity.Student;
import com.nt.utility.HibernateUtil;

public class OneToOnePKDAOImpl implements OneToOnePKDAO {

@Override
public void saveDataUsingParent() {
Transaction tx=null;
try(Session ses=HibernateUtil.getSession()){
//prepare objs
Student s1=new Student("raja", "hyd",LocalDateTime.of(1990,10, 21, 5,50));
Student s2=new Student("rajesh", "hyd",LocalDateTime.of(1991,11, 12, 5,50));
LibraryMembership library1=new LibraryMembership("gold", "Miss.Karnua",LocalDateTime.now(), LocalDateTime.of(2041,4,21,9,22));
LibraryMembership library2=new LibraryMembership("silver", "Miss.Karnua",LocalDateTime.now(), LocalDateTime.of(2031,1,21,9,22));
s1.setLibraryDetails(library1); library1.setStudentDetails(s1);
s2.setLibraryDetails(library2); library2.setStudentDetails(s2);
tx=ses.beginTransaction();
ses.save(s1); ses.save(s2);
tx.commit();
System.out.println("Objects are saved");
}
catch(HibernateException he) {
he.printStackTrace();
if(tx!=null || tx.getStatus()!=null || tx.getRollbackOnly() ) {
tx.rollback();
System.out.println("objects are not saved");
}
}

}

@Override
public void loadDataUsingChild() {
Transaction tx=null;
try(Session ses=HibernateUtil.getSession()){
//prepare and execute query
Query query=ses.createQuery("from LibraryMembership");
List<LibraryMembership> list=query.getResultList();
list.forEach(lib->{
System.out.println("child ::"+lib);
Student st=lib.getStudentDetails();
System.out.println("parent ::"+st);
});
}//try
catch(HibernateException he) {
he.printStackTrace();
}
}//method

@Override
public void removeDataUsingChild() {
Transaction tx=null;
try(Session ses=HibernateUtil.getSession()){
//Load child object
LibraryMembership lib=ses.get(LibraryMembership.class,1);
tx=ses.beginTransaction();
ses.delete(lib);
tx.commit();
System.out.println("child and its associated parent obj is deleted");
}
catch(HibernateException he) {
he.printStackTrace();
if(tx!=null || tx.getStatus()!=null || tx.getRollbackOnly() ) {
tx.rollback();
System.out.println("objects are not deleted");
}
}

}

}//class
Loading

0 comments on commit 938c4eb

Please sign in to comment.