Advertisement
Guest User

Untitled

a guest
Dec 16th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. private static final EntityManagerFactory ENTITY_FACTORY_HOLDER = Persistence.createEntityManagerFactory("LOCAL_PERSISTENCE");
  2.  
  3. public static int insertUser(UsersBean bean) {
  4. int ret = -1;
  5.  
  6. EntityManager entityManager = ENTITY_FACTORY_HOLDER.createEntityManager();
  7. EntityTransaction tr = null;
  8. try {
  9. tr = entityManager.getTransaction();
  10. tr.begin();
  11. entityManager.persist(bean);
  12. tr.commit();
  13. } finally {
  14. if (tr.isActive()) {
  15. try {
  16. tr.rollback();
  17. return -1;
  18. } catch (Exception e) {
  19. System.out.println("Error rolling back transaction" + e.getMessage());
  20. return -1;
  21. }
  22. }
  23. }
  24.  
  25. return ret;
  26. }
  27.  
  28.  
  29.  
  30.  
  31. <persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
  32. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  33. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
  34. http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
  35. version="2.1">
  36.  
  37. <persistence-unit name="LOCAL_PERSISTENCE">
  38. <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
  39.  
  40. <properties>
  41. <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
  42. <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/postgres?useUnicode=true&amp;characterEncoding=Cp1251" />
  43. <property name="javax.persistence.jdbc.user" value="postgres" />
  44. <property name="javax.persistence.jdbc.password" value="12345" />
  45. <property name="hibernate.show_sql" value="true" />
  46. <property name="hibernate.hbm2ddl.auto" value="update" />
  47. </properties>
  48.  
  49. </persistence-unit>
  50. </persistence>
  51.  
  52.  
  53. package my.curs.hospital;
  54.  
  55. import java.io.Serializable;
  56.  
  57. import javax.persistence.Column;
  58. import javax.persistence.Entity;
  59. import javax.persistence.GeneratedValue;
  60. import javax.persistence.GenerationType;
  61. import javax.persistence.Id;
  62. import javax.persistence.Table;
  63.  
  64. @Entity
  65. @Table(name = "users_curs")
  66. public class UsersBean implements Serializable {
  67. public UsersBean() {
  68. }
  69.  
  70. @Id
  71. @GeneratedValue(strategy = GenerationType.IDENTITY)
  72. @Column(name = "user_id")
  73. int user_id;
  74.  
  75. @Column(name = "user_name")
  76. String user_name;
  77.  
  78. @Column(name = "password")
  79. String password;
  80.  
  81. @Column(name = "e_mail")
  82. String e_mail;
  83.  
  84. @Column(name = "role_id")
  85. int role_id;
  86.  
  87. public int getUser_id() {
  88. return user_id;
  89. }
  90.  
  91. public void setUser_id(int user_id) {
  92. System.out.println("Call setUser_id " + user_id );
  93. this.user_id = user_id;
  94. }
  95.  
  96. public String getUser_name() {
  97. return user_name;
  98. }
  99.  
  100. public void setUser_name(String user_name) {
  101. this.user_name = user_name;
  102. }
  103.  
  104. public String getPassword() {
  105. return password;
  106. }
  107.  
  108. public void setPassword(String password) {
  109. this.password = password;
  110. }
  111.  
  112. public String getE_mail() {
  113. return e_mail;
  114. }
  115.  
  116. public void setE_mail(String e_mail) {
  117. this.e_mail = e_mail;
  118. }
  119.  
  120. public int getRole_id() {
  121. return role_id;
  122. }
  123.  
  124. public void setRole_id(int role_id) {
  125. this.role_id = role_id;
  126. }
  127.  
  128. @Override
  129. public String toString() {
  130. return user_id + "\t" + user_name + "\t" + password + "\t" + e_mail + "\t" + role_id;
  131. }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement