Advertisement
Guest User

Untitled

a guest
Mar 4th, 2015
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.62 KB | None | 0 0
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
  3. <hibernate-configuration>
  4. <session-factory>
  5. <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  6. <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
  7. <property name="connection.url">jdbc:mysql://localhost:3306/hb4?UseUnicode=true&characterEncoding=utf8</property>
  8. <property name="hibernate.connection.username">root</property>
  9. <property name="hibernate.connection.password">2323</property>
  10.  
  11. <property name="hibernate.connection.CharSet">utf8</property>
  12. <property name="hibernate.connection.characterEncoding">utf8</property>
  13. <property name="hibernate.connection.useUnicode">true</property>
  14.  
  15. <property name="hibernate.hbm2ddl.auto">update</property>
  16. <property name="hibernate.current_session_context_class">thread</property>
  17.  
  18. <mapping class="sajjad.htlo.book.Books" />
  19. <mapping class="sajjad.htlo.customer.Customers" />
  20. <mapping class="sajjad.htlo.order.Orders" />
  21. <mapping class="sajjad.htlo.order.OrderItems" />
  22. </session-factory>
  23. </hibernate-configuration>
  24.  
  25. Caused by: org.hibernate.exception.GenericJDBCException: could not execute statement
  26. Caused by: java.sql.SQLException: Incorrect string value: 'xD8xADxD8xADxD8xAD...' for column 'password' at row 1
  27. ...
  28.  
  29. @Entity
  30. public class Customers implements Serializable {
  31.  
  32. @Id
  33. @GeneratedValue
  34. private Integer cID;
  35. @Column(nullable = false)
  36. private String email;
  37. @Column(nullable = false)
  38. private String username;
  39. @Column(nullable = false)
  40. private String password;
  41.  
  42. public Customers() {
  43. }
  44.  
  45. //this is for login
  46. public Customers(String username, String password) {
  47. this.username = username;
  48. this.password = password;
  49. }
  50.  
  51. public Customers(String email, String username, String password) {
  52. this.email = email;
  53. this.username = username;
  54. this.password = password;
  55. }
  56.  
  57. private Session session;
  58. private Transaction transaction;
  59.  
  60. public CustomerDao() {
  61. }
  62.  
  63. public SessionFactory getSessionFactory() {
  64. Configuration configuration = new Configuration().configure();
  65. StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().
  66. applySettings(configuration.getProperties());
  67. SessionFactory sessionFactory = configuration.buildSessionFactory(builder.build());
  68. return sessionFactory;
  69. }
  70.  
  71. public Session openSession() {
  72. session = getSessionFactory().openSession();
  73. return session;
  74. }
  75.  
  76. public void closeSession() {
  77. getSession().close();
  78. }
  79.  
  80. public Session openSessionWithTransaction() {
  81. session = getSessionFactory().openSession();
  82. transaction = session.beginTransaction();
  83. return session;
  84. }
  85.  
  86. public void closeSessionWithTransaction() {
  87. getTransaction().commit();
  88. getSession().close();
  89. }
  90.  
  91. @Override
  92. public boolean persist(Customers entity) {
  93. if(getSession().save(entity).getClass().getName()!=null){
  94. return true;
  95. }
  96. return false;
  97. }
  98.  
  99. @Override
  100. public void delete(Customers entity) {
  101. getSession().delete(entity);
  102. }
  103.  
  104. @Override
  105. public void update(Customers entity) {
  106. getSession().update(entity);
  107. }
  108.  
  109. @Override
  110. public void deleteAll() {
  111. List<Customers> allCustomers = findAll();
  112. for (Customers eachCustomer : allCustomers) {
  113. delete(eachCustomer);
  114. }
  115. }
  116.  
  117. @Override
  118. public Customers findById(Integer id) {
  119. Customers foundCustomer = (Customers) getSession().get(Customers.class, id);
  120. return foundCustomer;
  121. }
  122.  
  123. @Override
  124. public List<Customers> findAll() {
  125. List<Customers> allCustomers = getSession().createQuery("from Customers").list();
  126. return allCustomers;
  127. }
  128.  
  129. public Session getSession() {
  130. return session;
  131. }
  132.  
  133. public void setSession(Session session) {
  134. this.session = session;
  135. }
  136.  
  137. public Transaction getTransaction() {
  138. return transaction;
  139. }
  140.  
  141. public void setTransaction(Transaction transaction) {
  142. this.transaction = transaction;
  143. }
  144.  
  145. private CustomerDao customerDao;
  146.  
  147. public CustomerService() {
  148. customerDao = new CustomerDao();
  149. }
  150.  
  151. public boolean persistCustomer(Customers entity) {
  152. boolean result;
  153. customerDao.openSessionWithTransaction();
  154. result = customerDao.persist(entity);
  155. customerDao.closeSessionWithTransaction();
  156. return result;
  157. }
  158.  
  159. public void deleteCustomer(Integer id) {
  160. customerDao.openSessionWithTransaction();
  161. Customers customerToDelete = customerDao.findById(id);
  162. customerDao.delete(customerToDelete);
  163. customerDao.closeSessionWithTransaction();
  164. }
  165.  
  166. public void updateCustomer(Customers entity) {
  167. customerDao.openSessionWithTransaction();
  168. customerDao.update(entity);
  169. customerDao.closeSessionWithTransaction();
  170. }
  171.  
  172. public Customers findCustomerById(Integer id) {
  173. customerDao.openSession();
  174. Customers foundCustomer = customerDao.findById(id);
  175. customerDao.closeSession();
  176. return foundCustomer;
  177. }
  178.  
  179. public List<Customers> findAllCustomers() {
  180. customerDao.openSession();
  181. List<Customers> allCustomers = customerDao.findAll();
  182. customerDao.closeSession();
  183. return allCustomers;
  184. }
  185.  
  186. public void deleteAllCustomers() {
  187. customerDao.openSessionWithTransaction();
  188. customerDao.deleteAll();
  189. customerDao.closeSessionWithTransaction();
  190. }
  191.  
  192. public CustomerDao customerDao() {
  193. return customerDao;
  194. }
  195. }
  196.  
  197. public String registeration() {
  198.  
  199. Customers newCustomer = new Customers(email, username, password);
  200. CustomerService customerService = new CustomerService();
  201. if (customerService.persistCustomer(newCustomer) == true) {
  202. return "Succ";
  203. }
  204. return "fail";
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement