Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Main {
- private static SessionFactory factory;
- public static void main(String[] args)
- {
- // User u = new User();
- //
- // UserDAO ud=new UserDAO(new Configuration().configure().buildSessionFactory());
- // ud.listUsers();
- // factory = ((AnnotationConfiguration) new AnnotationConfiguration().
- // configure()).
- // //addPackage("com.xyz") //add package if used.
- // addAnnotatedClass(User.class).
- // buildSessionFactory();
- factory = new Configuration().configure().buildSessionFactory();
- UserDAO ud=new UserDAO(factory);
- ud.listUsers();
- }
- }
- public void listUsers( ){
- Session session = factory.openSession();
- Transaction tx = null;
- try{
- tx = session.beginTransaction();
- List users = session.createQuery("FROM user").list();
- for (Iterator iterator =
- users.iterator(); iterator.hasNext();){
- User employee = (User) iterator.next();
- System.out.print("First Name: " + employee.getUsername());
- System.out.print("Last Name: " + employee.getPassword());
- System.out.println("Address: " + employee.getAddress());
- }
- tx.commit();
- }catch (HibernateException e) {
- if (tx!=null) tx.rollback();
- e.printStackTrace();
- }finally {
- session.close();
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <!DOCTYPE hibernate-configuration SYSTEM
- "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
- <hibernate-configuration>
- <session-factory>
- <property name="hibernate.dialect">
- org.hibernate.dialect.MySQLDialect
- </property>
- <property name="hibernate.connection.driver_class">
- com.mysql.jdbc.Driver
- </property>
- <!-- Assume test is the database name -->
- <property name="hibernate.connection.url">
- jdbc:mysql://localhost:3306/licenta
- </property>
- <property name="hibernate.connection.username">
- root
- </property>
- <property name="hibernate.connection.password">root</property>
- <mapping resource="user.hbm.xml" />
- </session-factory>
- </hibernate-configuration>
- <?xml version="1.0" encoding="utf-8"?>
- <!DOCTYPE hibernate-mapping PUBLIC
- "-//Hibernate/Hibernate Mapping DTD//EN"
- "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
- <hibernate-mapping>
- <class name="models.User" table="user">
- <meta attribute="class-description">
- This class contains the user detail.
- </meta>
- <id name="id" type="int" column="id">
- <generator class="native" />
- </id>
- <property name="username" column="username" type="string" />
- <property name="password" column="password" type="string" />
- <property name="address" column="address" type="string" />
- <property name="email" column="email" type="string" />
- <property name="phone" column="phone" type="string" />
- <property name="year" column="year" type="int" />
- <property name="type" column="type" type="string" />
- </class>
- </hibernate-mapping>
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>org.paul.licenta</groupId>
- <artifactId>teachApp</artifactId>
- <packaging>war</packaging>
- <version>0.0.1-SNAPSHOT</version>
- <name>teachApp</name>
- <build>
- <finalName>teachApp</finalName>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>2.5.1</version>
- <inherited>true</inherited>
- <configuration>
- <source>1.7</source>
- <target>1.7</target>
- </configuration>
- </plugin>
- </plugins>
- </build>
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.glassfish.jersey</groupId>
- <artifactId>jersey-bom</artifactId>
- <version>${jersey.version}</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- </dependencies>
- </dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.glassfish.jersey.containers</groupId>
- <artifactId>jersey-container-servlet-core</artifactId>
- <!-- use the following artifactId if you don't need servlet 2.x compatibility -->
- <!-- artifactId>jersey-container-servlet</artifactId -->
- </dependency>
- <dependency>
- <groupId>org.hibernate</groupId>
- <artifactId>hibernate-commons-annotations</artifactId>
- <version>3.0.0.ga</version>
- </dependency>
- <dependency>
- <groupId>org.hibernate</groupId>
- <artifactId>hibernate-annotations</artifactId>
- <version>3.3.0.GA</version>
- </dependency>
- <dependency>
- <groupId>javax.transaction</groupId>
- <artifactId>jta</artifactId>
- <version>1.1</version>
- </dependency>
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>javax.servlet-api</artifactId>
- <version>3.0.1</version>
- </dependency>
- <dependency>
- <groupId>commons-codec</groupId>
- <artifactId>commons-codec</artifactId>
- <version>1.10</version>
- </dependency>
- <dependency>
- <groupId>commons-logging</groupId>
- <artifactId>commons-logging</artifactId>
- <version>1.1.1</version>
- </dependency>
- <dependency>
- <groupId>org.hibernate</groupId>
- <artifactId>hibernate-core</artifactId>
- <version>5.2.10.Final</version>
- </dependency>
- <!-- for JPA, use hibernate-entitymanager instead of hibernate-core -->
- <dependency>
- <groupId>org.hibernate</groupId>
- <artifactId>hibernate-entitymanager</artifactId>
- <version>5.2.10.Final</version>
- </dependency>
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>5.1.6</version>
- </dependency>
- <!-- uncomment this to get JSON support
- <dependency>
- <groupId>org.glassfish.jersey.media</groupId>
- <artifactId>jersey-media-moxy</artifactId>
- </dependency>
- -->
- </dependencies>
- <properties>
- <jersey.version>2.26-b03</jersey.version>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- </properties>
- </project>
- Exception in thread "main" java.lang.NoSuchMethodError: org.hibernate.SessionFactory.openSession()Lorg/hibernate/classic/Session;
- at dao.UserDAO.listUsers(UserDAO.java:58)
- at Main.main(Main.java:31)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement