Advertisement
Guest User

Untitled

a guest
Mar 11th, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.99 KB | None | 0 0
  1. CREATE DATABASE IF NOT EXISTS `java_util_set` /*!40100 DEFAULT CHARACTER SET latin1 */;
  2. USE `java_util_set`;
  3. CREATE TABLE IF NOT EXISTS `certificate` (
  4. `id` int(11) NOT NULL AUTO_INCREMENT,
  5. `certificate_name` varchar(30) DEFAULT NULL,
  6. `employee_id` int(11) DEFAULT NULL,
  7. PRIMARY KEY (`id`)
  8. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  9.  
  10. CREATE TABLE IF NOT EXISTS `employee` (
  11. `id` int(11) NOT NULL AUTO_INCREMENT,
  12. `first_name` varchar(20) DEFAULT NULL,
  13. `last_name` varchar(20) DEFAULT NULL,
  14. `salary` int(11) DEFAULT NULL,
  15. PRIMARY KEY (`id`)
  16. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  17.  
  18. package Entity;
  19. // Generated 10/03/2017 11:27:32 PM by Hibernate Tools 4.3.1
  20.  
  21. import java.util.Set;
  22. import javax.persistence.Column;
  23. import javax.persistence.Entity;
  24. import javax.persistence.GeneratedValue;
  25. import static javax.persistence.GenerationType.IDENTITY;
  26. import javax.persistence.Id;
  27. import javax.persistence.Table;
  28.  
  29. /**
  30. * Employee generated by hbm2java
  31. */
  32. @Entity
  33. @Table(name="employee"
  34. ,catalog="java_util_set"
  35. )
  36. public class Employee {
  37.  
  38. private Integer id;
  39. private String firstName;
  40. private String lastName;
  41. private Integer salary;
  42. private Set certificates;
  43.  
  44. public Employee() {
  45. }
  46.  
  47. public Employee(String firstName, String lastName, Integer salary) {
  48. this.firstName = firstName;
  49. this.lastName = lastName;
  50. this.salary = salary;
  51. }
  52.  
  53. @Id @GeneratedValue(strategy=IDENTITY)
  54.  
  55. @Column(name="id", unique=true, nullable=false)
  56. public Integer getId() {
  57. return this.id;
  58. }
  59.  
  60. public void setId(Integer id) {
  61. this.id = id;
  62. }
  63.  
  64. @Column(name="first_name", length=20)
  65. public String getFirstName() {
  66. return this.firstName;
  67. }
  68.  
  69. public void setFirstName(String firstName) {
  70. this.firstName = firstName;
  71. }
  72.  
  73. @Column(name="last_name", length=20)
  74. public String getLastName() {
  75. return this.lastName;
  76. }
  77.  
  78. public void setLastName(String lastName) {
  79. this.lastName = lastName;
  80. }
  81.  
  82. @Column(name="salary")
  83. public Integer getSalary() {
  84. return this.salary;
  85. }
  86.  
  87. public void setSalary(Integer salary) {
  88. this.salary = salary;
  89. }
  90.  
  91. public Set getCertificates() {
  92. return this.certificates;
  93. }
  94.  
  95. public void setCertificates( Set certificates ) {
  96. this.certificates = certificates;
  97. }
  98. }
  99.  
  100. package Entity;
  101. // Generated 10/03/2017 11:27:32 PM by Hibernate Tools 4.3.1
  102.  
  103. import javax.persistence.Column;
  104. import javax.persistence.Entity;
  105. import javax.persistence.GeneratedValue;
  106. import static javax.persistence.GenerationType.IDENTITY;
  107. import javax.persistence.Id;
  108. import javax.persistence.Table;
  109.  
  110. /**
  111. * Certificate generated by hbm2java
  112. */
  113. @Entity
  114. @Table(name = "certificate", catalog = "java_util_set"
  115. )
  116. public class Certificate {
  117.  
  118. private Integer id;
  119. private String certificateName;
  120. private Integer employeeId;
  121.  
  122. public Certificate() {
  123. }
  124.  
  125. public Certificate(String certificateName) {
  126. this.certificateName = certificateName;
  127. //this.employeeId = employeeId;
  128. }
  129.  
  130. @Id
  131. @GeneratedValue(strategy = IDENTITY)
  132.  
  133. @Column(name = "id", unique = true, nullable = false)
  134. public Integer getId() {
  135. return this.id;
  136. }
  137.  
  138. public void setId(Integer id) {
  139. this.id = id;
  140. }
  141.  
  142. @Column(name = "certificate_name", length = 30)
  143. public String getCertificateName() {
  144. return this.certificateName;
  145. }
  146.  
  147. public void setCertificateName(String certificateName) {
  148. this.certificateName = certificateName;
  149. }
  150.  
  151. /* @Column(name = "employee_id")
  152. public Integer getEmployeeId() {
  153. return this.employeeId;
  154. }
  155.  
  156. public void setEmployeeId(Integer employeeId) {
  157. this.employeeId = employeeId;
  158. }*/
  159.  
  160. public boolean equals(Object obj) {
  161. if (obj == null) {
  162. return false;
  163. }
  164. if (!this.getClass().equals(obj.getClass())) {
  165. return false;
  166. }
  167.  
  168. Certificate obj2 = (Certificate) obj;
  169. if ((this.id == obj2.getId()) && (this.certificateName.equals(obj2.getCertificateName()))) {
  170. return true;
  171. }
  172. return false;
  173. }
  174.  
  175. public int hashCode() {
  176. int tmp = 0;
  177. tmp = (id + certificateName).hashCode();
  178. return tmp;
  179. }
  180. }
  181.  
  182. <?xml version="1.0"?>
  183. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  184. "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
  185. <!-- Generated 10/03/2017 11:27:33 PM by Hibernate Tools 4.3.1 -->
  186. <hibernate-mapping>
  187. <class name="Entity.Employee" table="EMPLOYEE">
  188. <meta attribute="class-description">
  189. This class contains the employee detail.
  190. </meta>
  191. <id name="id" type="int" column="id">
  192. <generator class="identity"/>
  193. </id>
  194. <set name="certificates" cascade="all">
  195. <key column="employee_id"/>
  196. <one-to-many class="Certificate"/>
  197. </set>
  198. <property name="firstName" column="first_name" type="string"/>
  199. <property name="lastName" column="last_name" type="string"/>
  200. <property name="salary" column="salary" type="int"/>
  201. </class>
  202. </hibernate-mapping>
  203.  
  204. <?xml version="1.0"?>
  205. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  206. "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
  207. <!-- Generated 10/03/2017 11:27:33 PM by Hibernate Tools 4.3.1 -->
  208. <hibernate-mapping>
  209. <class name="Entity.Certificate" table="CERTIFICATE">
  210. <meta attribute="class-description">
  211. This class contains the certificate records.
  212. </meta>
  213. <id name="id" type="int" column="id">
  214. <generator class="identity"/>
  215. </id>
  216. <property name="certificateName" column="certificate_name" type="string"/>
  217. </class>
  218. </hibernate-mapping>
  219.  
  220. <?xml version="1.0" encoding="UTF-8"?>
  221. <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
  222. <hibernate-configuration>
  223. <session-factory>
  224. <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  225. <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/java_util_set?zeroDateTimeBehavior=convertToNull</property>
  226. <property name="hibernate.connection.username">root</property>
  227. <property name="hibernate.connection.password">Mariana2803</property>
  228. <mapping resource="Entity/Certificate.hbm.xml"/>
  229. <mapping resource="Entity/Employee.hbm.xml"/>
  230. </session-factory>
  231. </hibernate-configuration>
  232.  
  233. <?xml version="1.0" encoding="UTF-8"?>
  234. <!DOCTYPE hibernate-reverse-engineering PUBLIC "-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd">
  235. <hibernate-reverse-engineering>
  236. <schema-selection match-catalog="java_util_set"/>
  237. <table-filter match-name="employee"/>
  238. <table-filter match-name="certificate"/>
  239. </hibernate-reverse-engineering>
  240.  
  241. package java_util_set;
  242.  
  243. import Entity.Certificate;
  244. import Entity.Employee;
  245. import java.util.HashSet;
  246. import java.util.Iterator;
  247. import java.util.List;
  248. import java.util.Set;
  249. import org.hibernate.HibernateException;
  250. import org.hibernate.Session;
  251. import org.hibernate.Transaction;
  252. import org.hibernate.SessionFactory;
  253. import org.hibernate.cfg.Configuration;
  254.  
  255. /**
  256. *
  257. * @author AFAL3D
  258. */
  259. public class Java_util_Set {
  260.  
  261. /**
  262. * @param args the command line arguments
  263. */
  264. private static SessionFactory factory;
  265.  
  266. public static void main(String[] args) {
  267. try {
  268. factory = new Configuration().configure().buildSessionFactory();
  269. } catch (Throwable ex) {
  270. System.err.println("Failed to create sessionFactory object." + ex);
  271. throw new ExceptionInInitializerError(ex);
  272. }
  273. Java_util_Set ME = new Java_util_Set();
  274. /* Let us have a set of certificates for the first employee */
  275. HashSet set1 = new HashSet();
  276. set1.add(new Certificate("MCA"));
  277. set1.add(new Certificate("MBA"));
  278. set1.add(new Certificate("PMP"));
  279.  
  280. /* Add employee records in the database */
  281. Integer empID1 = ME.addEmployee("Manoj", "Kumar", 4000, set1);
  282.  
  283. /* Another set of certificates for the second employee */
  284. HashSet set2 = new HashSet();
  285. set2.add(new Certificate("BCA"));
  286. set2.add(new Certificate("BA"));
  287.  
  288. /* Add another employee record in the database */
  289. Integer empID2 = ME.addEmployee("Dilip", "Kumar", 3000, set2);
  290.  
  291. /* List down all the employees */
  292. ME.listEmployees();
  293.  
  294. /* Update employee's salary records */
  295. ME.updateEmployee(empID1, 5000);
  296.  
  297. /* Delete an employee from the database */
  298. ME.deleteEmployee(empID2);
  299.  
  300. /* List down all the employees */
  301. ME.listEmployees();
  302.  
  303. }
  304.  
  305. /* Method to add an employee record in the database */
  306. public Integer addEmployee(String fname, String lname,
  307. int salary, Set cert) {
  308. Session session = factory.openSession();
  309. Transaction tx = null;
  310. Integer employeeID = null;
  311. try {
  312. tx = session.beginTransaction();
  313. Employee employee = new Employee(fname, lname, salary);
  314. employee.setCertificates(cert);
  315. employeeID = (Integer) session.save(employee);
  316. tx.commit();
  317. } catch (HibernateException e) {
  318. if (tx != null) {
  319. tx.rollback();
  320. }
  321. e.printStackTrace();
  322. } finally {
  323. session.close();
  324. }
  325. return employeeID;
  326. }
  327.  
  328. /* Method to list all the employees detail */
  329. public void listEmployees() {
  330. Session session = factory.openSession();
  331. Transaction tx = null;
  332. try {
  333. tx = session.beginTransaction();
  334. List employees = session.createQuery("FROM Employee").list();
  335. for (Iterator iterator1
  336. = employees.iterator(); iterator1.hasNext();) {
  337. Employee employee = (Employee) iterator1.next();
  338. System.out.print("First Name: " + employee.getFirstName());
  339. System.out.print(" Last Name: " + employee.getLastName());
  340. System.out.println(" Salary: " + employee.getSalary());
  341. Set certificates = employee.getCertificates();
  342. for (Iterator iterator2
  343. = certificates.iterator(); iterator2.hasNext();) {
  344. Certificate certName = (Certificate) iterator2.next();
  345. System.out.println("Certificate: " + certName.getCertificateName());
  346. }
  347. }
  348. tx.commit();
  349. } catch (HibernateException e) {
  350. if (tx != null) {
  351. tx.rollback();
  352. }
  353. e.printStackTrace();
  354. } finally {
  355. session.close();
  356. }
  357. }
  358.  
  359. /* Method to update salary for an employee */
  360. public void updateEmployee(Integer EmployeeID, int salary) {
  361. Session session = factory.openSession();
  362. Transaction tx = null;
  363. try {
  364. tx = session.beginTransaction();
  365. Employee employee
  366. = (Employee) session.get(Employee.class, EmployeeID);
  367. employee.setSalary(salary);
  368. session.update(employee);
  369. tx.commit();
  370. } catch (HibernateException e) {
  371. if (tx != null) {
  372. tx.rollback();
  373. }
  374. e.printStackTrace();
  375. } finally {
  376. session.close();
  377. }
  378. }
  379.  
  380. /* Method to delete an employee from the records */
  381. public void deleteEmployee(Integer EmployeeID) {
  382. Session session = factory.openSession();
  383. Transaction tx = null;
  384. try {
  385. tx = session.beginTransaction();
  386. Employee employee
  387. = (Employee) session.get(Employee.class, EmployeeID);
  388. session.delete(employee);
  389. tx.commit();
  390. } catch (HibernateException e) {
  391. if (tx != null) {
  392. tx.rollback();
  393. }
  394. e.printStackTrace();
  395. } finally {
  396. session.close();
  397. }
  398. }
  399. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement