Advertisement
Guest User

Untitled

a guest
Jul 1st, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.20 KB | None | 0 0
  1. public class Main {
  2. private static SessionFactory factory;
  3.  
  4. public static void main(String[] args)
  5. {
  6.  
  7. // User u = new User();
  8. //
  9. // UserDAO ud=new UserDAO(new Configuration().configure().buildSessionFactory());
  10. // ud.listUsers();
  11.  
  12.  
  13. // factory = ((AnnotationConfiguration) new AnnotationConfiguration().
  14. // configure()).
  15. // //addPackage("com.xyz") //add package if used.
  16. // addAnnotatedClass(User.class).
  17. // buildSessionFactory();
  18. factory = new Configuration().configure().buildSessionFactory();
  19.  
  20. UserDAO ud=new UserDAO(factory);
  21. ud.listUsers();
  22. }
  23. }
  24.  
  25. public void listUsers( ){
  26. Session session = factory.openSession();
  27. Transaction tx = null;
  28. try{
  29. tx = session.beginTransaction();
  30. List users = session.createQuery("FROM user").list();
  31. for (Iterator iterator =
  32. users.iterator(); iterator.hasNext();){
  33. User employee = (User) iterator.next();
  34. System.out.print("First Name: " + employee.getUsername());
  35. System.out.print("Last Name: " + employee.getPassword());
  36. System.out.println("Address: " + employee.getAddress());
  37. }
  38. tx.commit();
  39. }catch (HibernateException e) {
  40. if (tx!=null) tx.rollback();
  41. e.printStackTrace();
  42. }finally {
  43. session.close();
  44. }
  45. }
  46.  
  47. <?xml version="1.0" encoding="utf-8"?>
  48. <!DOCTYPE hibernate-configuration SYSTEM
  49. "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
  50.  
  51. <hibernate-configuration>
  52. <session-factory>
  53. <property name="hibernate.dialect">
  54. org.hibernate.dialect.MySQLDialect
  55. </property>
  56. <property name="hibernate.connection.driver_class">
  57. com.mysql.jdbc.Driver
  58. </property>
  59.  
  60. <!-- Assume test is the database name -->
  61. <property name="hibernate.connection.url">
  62. jdbc:mysql://localhost:3306/licenta
  63. </property>
  64. <property name="hibernate.connection.username">
  65. root
  66. </property>
  67. <property name="hibernate.connection.password">root</property>
  68. <mapping resource="user.hbm.xml" />
  69.  
  70. </session-factory>
  71. </hibernate-configuration>
  72.  
  73. <?xml version="1.0" encoding="utf-8"?>
  74. <!DOCTYPE hibernate-mapping PUBLIC
  75. "-//Hibernate/Hibernate Mapping DTD//EN"
  76. "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
  77.  
  78. <hibernate-mapping>
  79. <class name="models.User" table="user">
  80. <meta attribute="class-description">
  81. This class contains the user detail.
  82. </meta>
  83. <id name="id" type="int" column="id">
  84. <generator class="native" />
  85. </id>
  86.  
  87. <property name="username" column="username" type="string" />
  88. <property name="password" column="password" type="string" />
  89. <property name="address" column="address" type="string" />
  90. <property name="email" column="email" type="string" />
  91. <property name="phone" column="phone" type="string" />
  92. <property name="year" column="year" type="int" />
  93. <property name="type" column="type" type="string" />
  94. </class>
  95. </hibernate-mapping>
  96.  
  97. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  98. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  99.  
  100. <modelVersion>4.0.0</modelVersion>
  101.  
  102. <groupId>org.paul.licenta</groupId>
  103. <artifactId>teachApp</artifactId>
  104. <packaging>war</packaging>
  105. <version>0.0.1-SNAPSHOT</version>
  106. <name>teachApp</name>
  107.  
  108. <build>
  109. <finalName>teachApp</finalName>
  110. <plugins>
  111. <plugin>
  112. <groupId>org.apache.maven.plugins</groupId>
  113. <artifactId>maven-compiler-plugin</artifactId>
  114. <version>2.5.1</version>
  115. <inherited>true</inherited>
  116. <configuration>
  117. <source>1.7</source>
  118. <target>1.7</target>
  119. </configuration>
  120. </plugin>
  121. </plugins>
  122. </build>
  123.  
  124. <dependencyManagement>
  125. <dependencies>
  126. <dependency>
  127. <groupId>org.glassfish.jersey</groupId>
  128. <artifactId>jersey-bom</artifactId>
  129. <version>${jersey.version}</version>
  130. <type>pom</type>
  131. <scope>import</scope>
  132. </dependency>
  133. </dependencies>
  134. </dependencyManagement>
  135.  
  136. <dependencies>
  137. <dependency>
  138. <groupId>org.glassfish.jersey.containers</groupId>
  139. <artifactId>jersey-container-servlet-core</artifactId>
  140. <!-- use the following artifactId if you don't need servlet 2.x compatibility -->
  141. <!-- artifactId>jersey-container-servlet</artifactId -->
  142. </dependency>
  143. <dependency>
  144. <groupId>org.hibernate</groupId>
  145. <artifactId>hibernate-commons-annotations</artifactId>
  146. <version>3.0.0.ga</version>
  147. </dependency>
  148.  
  149. <dependency>
  150. <groupId>org.hibernate</groupId>
  151. <artifactId>hibernate-annotations</artifactId>
  152. <version>3.3.0.GA</version>
  153. </dependency>
  154.  
  155. <dependency>
  156. <groupId>javax.transaction</groupId>
  157. <artifactId>jta</artifactId>
  158. <version>1.1</version>
  159. </dependency>
  160. <dependency>
  161. <groupId>javax.servlet</groupId>
  162. <artifactId>javax.servlet-api</artifactId>
  163. <version>3.0.1</version>
  164. </dependency>
  165. <dependency>
  166. <groupId>commons-codec</groupId>
  167. <artifactId>commons-codec</artifactId>
  168. <version>1.10</version>
  169. </dependency>
  170. <dependency>
  171. <groupId>commons-logging</groupId>
  172. <artifactId>commons-logging</artifactId>
  173. <version>1.1.1</version>
  174. </dependency>
  175. <dependency>
  176. <groupId>org.hibernate</groupId>
  177. <artifactId>hibernate-core</artifactId>
  178. <version>5.2.10.Final</version>
  179. </dependency>
  180.  
  181. <!-- for JPA, use hibernate-entitymanager instead of hibernate-core -->
  182. <dependency>
  183. <groupId>org.hibernate</groupId>
  184. <artifactId>hibernate-entitymanager</artifactId>
  185. <version>5.2.10.Final</version>
  186. </dependency>
  187. <dependency>
  188. <groupId>mysql</groupId>
  189. <artifactId>mysql-connector-java</artifactId>
  190. <version>5.1.6</version>
  191. </dependency>
  192. <!-- uncomment this to get JSON support
  193. <dependency>
  194. <groupId>org.glassfish.jersey.media</groupId>
  195. <artifactId>jersey-media-moxy</artifactId>
  196. </dependency>
  197. -->
  198. </dependencies>
  199. <properties>
  200. <jersey.version>2.26-b03</jersey.version>
  201. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  202. </properties>
  203. </project>
  204.  
  205. Exception in thread "main" java.lang.NoSuchMethodError: org.hibernate.SessionFactory.openSession()Lorg/hibernate/classic/Session;
  206. at dao.UserDAO.listUsers(UserDAO.java:58)
  207. at Main.main(Main.java:31)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement