Skip to content

Commit

Permalink
Final Commit-Hibernate.
Browse files Browse the repository at this point in the history
  • Loading branch information
gargh committed Apr 10, 2020
1 parent 966a9b2 commit dc46223
Show file tree
Hide file tree
Showing 23 changed files with 345 additions and 0 deletions.
38 changes: 38 additions & 0 deletions user-dao/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?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"/>
</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"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5">
<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 user-dao/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>user-dao</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>
8 changes: 8 additions & 0 deletions user-dao/.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=1.5
org.eclipse.jdt.core.compiler.compliance=1.5
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.5
4 changes: 4 additions & 0 deletions user-dao/.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
24 changes: 24 additions & 0 deletions user-dao/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.apex.user.dao</groupId>
<artifactId>user-dao</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>user-dao</name>
<description>User Hibernate DAO
User Hibernate DAO</description>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.1.1.Final</version>
</dependency>

<!-- http://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>

</dependencies>
</project>
28 changes: 28 additions & 0 deletions user-dao/src/main/java/com/apex/user/client/UserClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.apex.user.client;

import com.apex.user.dao.UserDAO;
import com.apex.user.vo.UserVO;

public class UserClient {

public static void main(String[] args) {

UserDAO userDAO = new UserDAO();
UserVO user = new UserVO();

user.setFirstName("ABC");
user.setLastName("XYZ");
user.setMiddleName("G");

userDAO.createUser(user);

/*
* UserVO user1 = userDAO.getUser(8); user1.setMiddleName("G");
*
* userDAO.updateUser(user1);
*
* userDAO.deleteUser(9);
*/

}
}
48 changes: 48 additions & 0 deletions user-dao/src/main/java/com/apex/user/dao/UserDAO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.apex.user.dao;

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

import com.apex.user.util.HibernateUtil;
import com.apex.user.vo.UserVO;

public class UserDAO {

public void createUser(UserVO user) {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.getTransaction();
tx.begin();
session.save(user);
tx.commit();
session.close();
}

public UserVO getUser(int id) {
Session session = HibernateUtil.getSessionFactory().openSession();
UserVO user = (UserVO) session.load(UserVO.class, new Integer(id));
System.out.println(user);
session.close();
return user;

}

public void updateUser(UserVO user) {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.getTransaction();
tx.begin();
session.update(user);
tx.commit();
session.close();
}

public void deleteUser(int id) {
Session session = HibernateUtil.getSessionFactory().openSession();
UserVO user = getUser(id);
Transaction tx = session.getTransaction();
tx.begin();
session.delete(user);
tx.commit();
session.close();
}

}
33 changes: 33 additions & 0 deletions user-dao/src/main/java/com/apex/user/util/HibernateUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.apex.user.util;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistryBuilder;

import com.apex.user.vo.UserVO;

public class HibernateUtil {

private static SessionFactory sessionFactory;

private static Configuration config;

// Create the initial SessionFactory from the default configuration files
static {
try {
config = new Configuration().configure().addAnnotatedClass(UserVO.class);

org.hibernate.service.ServiceRegistry serviceReg = new ServiceRegistryBuilder()
.applySettings(config.getProperties()).buildServiceRegistry();

sessionFactory = config.buildSessionFactory(serviceReg);
} catch (Throwable ex) {
throw new ExceptionInInitializerError(ex);
}
}

public static SessionFactory getSessionFactory() {
return sessionFactory;
}

}
64 changes: 64 additions & 0 deletions user-dao/src/main/java/com/apex/user/vo/UserVO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.apex.user.vo;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="usr")
public class UserVO {

@Id
@Column(name="userid")
private int id;
@Column(name="firstname")
private String firstName;
@Column(name="lastname")
private String lastName;
@Column(name="middlename")
private String middleName;

public UserVO() {
super();
}

public int getId() {
return id;
}

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

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getMiddleName() {
return middleName;
}

public void setMiddleName(String middleName) {
this.middleName = middleName;
}

@Override
public String toString() {
return "UserVO [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", middleName=" + middleName
+ "]";
}

}
15 changes: 15 additions & 0 deletions user-dao/src/main/resources/hibernate.cfg.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?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>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
</session-factory>
</hibernate-configuration>
5 changes: 5 additions & 0 deletions user-dao/target/classes/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Manifest-Version: 1.0
Built-By: gargh
Build-Jdk: 1.8.0_231
Created-By: Maven Integration for Eclipse

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#Generated by Maven Integration for Eclipse
#Fri Apr 10 13:21:55 PDT 2020
version=0.0.1-SNAPSHOT
groupId=com.apex.user.dao
m2e.projectName=user-dao
m2e.projectLocation=C\:\\Users\\gargh\\eclipse-workspace-new-temp\\user-dao
artifactId=user-dao
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.apex.user.dao</groupId>
<artifactId>user-dao</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>user-dao</name>
<description>User Hibernate DAO
User Hibernate DAO</description>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.1.1.Final</version>
</dependency>

<!-- http://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>

</dependencies>
</project>
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
15 changes: 15 additions & 0 deletions user-dao/target/classes/hibernate.cfg.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?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>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
</session-factory>
</hibernate-configuration>
5 changes: 5 additions & 0 deletions user-dao/target/maven-archiver/pom.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#Generated by Maven
#Sat Mar 21 14:52:24 PDT 2020
version=0.0.1-SNAPSHOT
groupId=com.apex.user.dao
artifactId=user-dao
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
C:\Users\gargh\eclipse-workspace-new\user-dao\src\main\java\com\apex\user\util\HibernateUtil.java
C:\Users\gargh\eclipse-workspace-new\user-dao\src\main\java\com\apex\user\client\UserClient.java
C:\Users\gargh\eclipse-workspace-new\user-dao\src\main\java\com\apex\user\vo\UserVO.java
C:\Users\gargh\eclipse-workspace-new\user-dao\src\main\java\com\apex\user\dao\UserDAO.java
Binary file added user-dao/target/user-dao-0.0.1-SNAPSHOT.jar
Binary file not shown.

0 comments on commit dc46223

Please sign in to comment.