Skip to content

Commit 52f25c2

Browse files
authored
JPAExamples
LayredArch Demo
1 parent c1a0983 commit 52f25c2

File tree

8 files changed

+180
-0
lines changed

8 files changed

+180
-0
lines changed

src/META-INF/persistence.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
5+
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
6+
version="2.0">
7+
<persistence-unit name="day2" transaction-type="RESOURCE_LOCAL">
8+
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
9+
<properties>
10+
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
11+
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/capgemini" />
12+
<property name="javax.persistence.jdbc.user" value="root" />
13+
<property name="javax.persistence.jdbc.password" value="root" />
14+
<property name="hibernate.hbm2ddl.auto" value="update" />
15+
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
16+
<property name="hibernate.show_sql" value="true" />
17+
</properties>
18+
</persistence-unit>
19+
</persistence>

src/com/capg/config/JPAConfig.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.capg.config;
2+
3+
import javax.persistence.EntityManager;
4+
import javax.persistence.EntityManagerFactory;
5+
import javax.persistence.Persistence;
6+
7+
public class JPAConfig {
8+
private static EntityManagerFactory factory;
9+
private static EntityManager em;
10+
11+
static {
12+
factory=Persistence.createEntityManagerFactory("day2");
13+
}
14+
15+
public static EntityManager getEntityManager() {
16+
if(em==null || !em.isOpen()) {
17+
em=factory.createEntityManager();
18+
}
19+
return em;
20+
}
21+
}

src/com/capg/dao/EmployeeDao.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.capg.dao;
2+
3+
import com.capg.pojo.Employee;
4+
5+
public interface EmployeeDao {
6+
public String saveEmployee(Employee emp);
7+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.capg.dao;
2+
3+
import javax.persistence.EntityManager;
4+
import javax.persistence.EntityTransaction;
5+
6+
import com.capg.config.JPAConfig;
7+
import com.capg.pojo.Employee;
8+
9+
public class EmployeeDaoImpl implements EmployeeDao{
10+
EntityManager eManager=JPAConfig.getEntityManager();
11+
12+
@Override
13+
public String saveEmployee(Employee emp) {
14+
EntityTransaction tx=eManager.getTransaction();
15+
tx.begin();
16+
eManager.persist(emp);
17+
tx.commit();
18+
return "Employee Created.... Your ID is "+emp.getEmpid();
19+
}
20+
}

src/com/capg/pl/EmployeeMain.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.capg.pl;
2+
3+
import java.util.Date;
4+
5+
import com.capg.pojo.Employee;
6+
import com.capg.service.EmployeeService;
7+
import com.capg.service.EmployeeServiceImpl;
8+
9+
public class EmployeeMain {
10+
11+
public static void main(String[] args) {
12+
EmployeeService es=new EmployeeServiceImpl();
13+
Employee e=new Employee("Saurabh", 35000, new Date());
14+
String result=es.createEmployee(e);
15+
System.out.println("======================");
16+
System.out.println(result);
17+
System.out.println("======================");
18+
19+
20+
}
21+
22+
}

src/com/capg/pojo/Employee.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.capg.pojo;
2+
3+
import java.util.Date;
4+
5+
import javax.persistence.Column;
6+
import javax.persistence.Entity;
7+
import javax.persistence.GeneratedValue;
8+
import javax.persistence.GenerationType;
9+
import javax.persistence.Id;
10+
import javax.persistence.Table;
11+
import javax.persistence.Temporal;
12+
import javax.persistence.TemporalType;
13+
14+
@Entity
15+
@Table(name="EmployeeDetails")
16+
public class Employee {
17+
@Id
18+
@GeneratedValue(strategy=GenerationType.AUTO)
19+
private int empid;
20+
@Column(name="EmployeeName")
21+
private String name;
22+
private double salary;
23+
@Temporal(TemporalType.DATE)
24+
@Column(name="DateOfJoining")
25+
private Date doj;
26+
27+
public Employee() {
28+
29+
}
30+
public Employee(String name, double salary, Date doj) {
31+
super();
32+
this.name = name;
33+
this.salary = salary;
34+
this.doj = doj;
35+
}
36+
public int getEmpid() {
37+
return empid;
38+
}
39+
public void setEmpid(int empid) {
40+
this.empid = empid;
41+
}
42+
public String getName() {
43+
return name;
44+
}
45+
public void setName(String name) {
46+
this.name = name;
47+
}
48+
public double getSalary() {
49+
return salary;
50+
}
51+
public void setSalary(double salary) {
52+
this.salary = salary;
53+
}
54+
public Date getDoj() {
55+
return doj;
56+
}
57+
public void setDoj(Date doj) {
58+
this.doj = doj;
59+
}
60+
61+
62+
63+
64+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.capg.service;
2+
3+
import com.capg.pojo.Employee;
4+
5+
public interface EmployeeService {
6+
public String createEmployee(Employee emp);
7+
8+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.capg.service;
2+
3+
import com.capg.dao.EmployeeDao;
4+
import com.capg.dao.EmployeeDaoImpl;
5+
import com.capg.pojo.Employee;
6+
7+
public class EmployeeServiceImpl implements EmployeeService {
8+
9+
EmployeeDao edao=new EmployeeDaoImpl();
10+
11+
@Override
12+
public String createEmployee(Employee emp) {
13+
if(emp.getName().length()<4 || emp.getSalary()<20000)
14+
return "Invalid Employee Data";
15+
else
16+
return edao.saveEmployee(emp);
17+
}
18+
19+
}

0 commit comments

Comments
 (0)