2011-09-19 24 views
8

tôi xây dựng HibernateUtil của tôi theo cách này:Hibernate: Một ví dụ AnnotationConfiguration là cần thiết để sử dụng ... lỗi

public class HibernateUtil { 

     private static final SessionFactory sessionFactory; 

     static { 
      try { 
       // Create the SessionFactory from standard (hibernate.cfg.xml) config file. 
       sessionFactory = new Configuration().configure().buildSessionFactory(); 

      } catch (Throwable ex) { 
       // Log the exception. 
       System.err.println("Initial SessionFactory creation failed." + ex); 
       throw new ExceptionInInitializerError(ex); 
      } 
     } 

     public static SessionFactory getSessionFactory() { 
      return sessionFactory; 
     } 
} 

Vì vậy, khi tôi cố gắng thực hiện lệnh HQL trong HQL Editor trong Eclipse (với Hibernate Tools) cung cấp cho lỗi sau: enter image description here Tại sao điều này xảy ra? Nó shouln't thay đổi AnnotationConfiguration bởi ConfigureAnnotation?

CẬP NHẬT

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE hibernate-configuration PUBLIC 
     "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
     "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 
<hibernate-configuration> 
    <session-factory> 
     <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 
     <property name="hibernate.connection.password"><password></property> 
     <property name="hibernate.connection.url">jdbc:mysql://<hostname>:3306/<schema></property> 
     <property name="hibernate.connection.username">root</property> 
     <!-- <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> --> 
     <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 

     <!-- SQL --> 
     <property name="hibernate.format_sql">true</property> 
     <property name="hibernate.show_sql">true</property> 
     <!-- C3P0 --> 
     <property name="hibernate.c3p0.acquire_increment">2</property> 
     <property name="hibernate.c3p0.max_size">20</property> 
     <property name="hibernate.c3p0.min_size">5</property> 
     <property name="hibernate.c3p0.timeout">180</property> 
     <property name="hibernate.c3p0.idle_test_period">100</property> 
     <!-- Classes --> 
     <mapping class="com.suaparte.pojo.Area" /> 
    </session-factory> 
</hibernate-configuration> 

Cảm ơn trước.

+0

Can bạn dán hibernate.cfg.xml? Bạn có thể bỏ qua các lớp nếu bạn thích. Các thuộc tính. – ssedano

+0

Tôi cập nhật bài đăng của tôi Udo. –

Trả lời

12

Nếu bạn có lỗi, và bạn đang sử dụng chế độ ngủ đông version = = 4.0, vấn đề có thể là trong cấu hình Hibernate Console.

Cố gắng đi đến:

Run -> Run Configurations

và mở cấu hình mà bạn đã tạo ra, Trên chính Loại thay đổi tab từ Core để chú thích. Dưới đây là một ảnh chụp màn hình: enter image description here

2

Cố gắng xây dựng như:

AnnotationConfiguration().configure().buildSessionFactory(); 

Để làm như vậy, bạn cần điều này:

Hibernate annotations 

trọng.

Udo.

+0

Tôi đã thử Udo, không thành công. –

-1

Tạo một lớp tiện ích mà trả về một đối tượng SessionFactory:

public class HibernateUtil { 

    private static SessionFactory sessionFactory; 

    static { 
     try { 
      sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory(); 
     } 
     catch(Throwable t) { 
      throw new ExceptionInInitializerError(t); 
     } 
    } 

    public static SessionFactory getSessionFactory() { 
     return sessionFactory; 
    } 

    public static void shutdown() { 
     getSessionFactory().close(); 
    } 
} 

và gọi đây là trong lớp học chính của bạn như thế này:

SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); 
Session session = sessionFactory.getCurrentSession(); 
6

Chỉ cần thay đổi cấu hình() để AnnotationConfiguration()

+0

Sử dụng 'AnnotationConfiguration' không được chấp nhận. – SureshS

1

bạn có thể sử dụng AnnotationConfiguration() thay vì cấu hình()

3

Tôi đã thay đổi mã của tôi từ

Configuration cfg=new Configuration(); 
      cfg.configure("hibernate.cfg.xml");   
      SessionFactory factory=cfg.buildSessionFactory(); 

Để

SessionFactory factory=new AnnotationConfiguration().configure("hibernate.cfg.xml").buildSessionFactory(); 

Ngoài ra tôi đã thêm chú thích "@Id" vào lớp POJO của tôi. Sau khi thêm "@Id", tôi đã giải quyết xong vấn đề của mình.

2

Hãy thử tải xuống phiên bản phân phối hibernate 3.6.4., Sử dụng phiên bản jre1.6.0_07. bằng cách thực hiện việc này, bạn có thể tạo thành công đối tượng cấu hình mới như dưới đây thay vì sử dụng AnnotationConfiguration().

SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); 
0
AnnotationConfiguration configuration=new AnnotationConfiguration(); 

    configuration.configure("hibernate.cfg.xml"); 

    SessionFactory factory=configuration.buildSessionFactory(); 

    Session session=factory.openSession(); 
+0

Có thể mô tả những gì bạn đã làm và tại sao nó hoạt động. –

+0

Chào mừng bạn đến ngăn xếp tràn :-) Câu trả lời chỉ có mã không hữu ích cho cộng đồng. Vui lòng xem [answer] – JimHawkins

0

Hãy thử thay đổi cấu hình(). Cấu hình(). BuildSessionFactory() vào AnnotationConfiguration(). Cấu hình(). BuildSessionFactory() và bao gồm ngủ đông-annotations.jar trong đường dẫn lớp

Các vấn đề liên quan