2011-11-14 25 views
5

có cách nào để lặp lại (ví dụ: thông qua cho) tất cả các lớp có trong gói nào đó không? Tôi phải addAnnotatedClass(Class c) trên AnnotationConfiguration. Làm nó như thế này:Thêm lớp chú thích trong Hibernate bằng cách thêm tất cả các lớp trong một số gói. JAVA

AnnotationConfiguration annotationConfiguration.addAnnotatedClass(AdditionalInformation.class); 
    annotationConfiguration.addAnnotatedClass(AdditionalInformationGroup.class); 
    annotationConfiguration.addAnnotatedClass(Address.class); 
    annotationConfiguration.addAnnotatedClass(BankAccount.class); 
    annotationConfiguration.addAnnotatedClass(City.class); 
    //et cetera 

Tất cả các bảng của tôi là trong gói Tables.Informations.

+1

bằng bất kỳ cơ hội nào sử dụng lò xo hoặc jpa? – Bozho

Trả lời

13

Như đã đề cập trong nhận xét, chức năng tải tất cả các lớp trong gói là không thể với API AnnotationConfiguration. Dưới đây là một số công cụ bạn có thể làm với API đã nói (lưu ý rằng phương thức "addPackage" chỉ đọc siêu dữ liệu gói, chẳng hạn như tìm thấy trong lớp package-info.java, nó KHÔNG tải tất cả các lớp trong gói):

http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html/ch01.html

sessionFactory = new AnnotationConfiguration() 
        .addPackage("test.animals") //the fully qualified package name 
        .addAnnotatedClass(Flight.class) 
        .addAnnotatedClass(Sky.class) 
        .addAnnotatedClass(Person.class) 
        .addAnnotatedClass(Dog.class) 
        .addResource("test/animals/orm.xml") 
        .configure() 
        .buildSessionFactory(); 
+3

Làm thế nào trên trái đất này giải quyết vấn đề phải ghi vào tên lớp trong lớp tiện ích của bạn? (Nó chỉ tiết kiệm được một vài lần nhấn phím). – vbence

+3

@vbence Có, đồng ý, tôi xin lỗi, câu trả lời của tôi là sai: không có cách nào để thêm tất cả các lớp học từ một gói trong một lần sử dụng API AnnotationConfiguration. Ví dụ tôi cho là minh họa rằng bạn có thể làm điều này, cũng như thêm các lớp riêng lẻ và các tài nguyên khác tất cả trong một lần. Tôi đã cơ bản có cùng một sự hiểu lầm về API như là một trong những thảo luận ở đây: https://forum.hibernate.org/viewtopic.php?f=1&t=980723 Tôi muốn tôi có thể xóa bài viết của tôi, nhưng tôi không thể vì đó là một câu trả lời được chấp nhận. –

+2

Ồ, tôi đọc cái đó (bài đăng trên diễn đàn). Có lẽ bạn có thể thêm một ghi chú như * "không thể những gì OP originnaly muốn, nhưng cú pháp này sẽ giúp một chút" * hoặc một cái gì đó như thế. – vbence

1

các mã sau đây đi qua tất cả các lớp trong một gói cụ thể và tạo ra một danh sách những chú thích với "@Entity". Mỗi lớp được thêm vào cấu hình nhà máy Hibernate của bạn, mà không cần phải liệt kê tất cả chúng một cách rõ ràng.

public static void main(String[] args) throws URISyntaxException, IOException, ClassNotFoundException { 
    try { 
     Configuration configuration = new Configuration().configure(); 
     for (Class cls : getEntityClassesFromPackage("com.example.hib.entities")) { 
      configuration.addAnnotatedClass(cls); 
     } 
     sessionFactory = configuration.buildSessionFactory(); 
    } catch (Throwable ex) { 
     System.err.println("Failed to create sessionFactory object." + ex); 
     throw new ExceptionInInitializerError(ex); 
    } 
} 

public static List<Class<?>> getEntityClassesFromPackage(String packageName) throws ClassNotFoundException, IOException, URISyntaxException { 
    List<String> classNames = getClassNamesFromPackage(packageName); 
    List<Class<?>> classes = new ArrayList<Class<?>>(); 
    for (String className : classNames) { 
     Class<?> cls = Class.forName(packageName + "." + className); 
     Annotation[] annotations = cls.getAnnotations(); 

     for (Annotation annotation : annotations) { 
      System.out.println(cls.getCanonicalName() + ": " + annotation.toString()); 
      if (annotation instanceof javax.persistence.Entity) { 
       classes.add(cls); 
      } 
     } 
    } 

    return classes; 
} 

public static ArrayList<String> getClassNamesFromPackage(String packageName) throws IOException, URISyntaxException, ClassNotFoundException { 
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 
    ArrayList<String> names = new ArrayList<String>(); 

    packageName = packageName.replace(".", "/"); 
    URL packageURL = classLoader.getResource(packageName); 

    URI uri = new URI(packageURL.toString()); 
    File folder = new File(uri.getPath()); 
    File[] files = folder.listFiles(); 
    for (File file: files) { 
     String name = file.getName(); 
     name = name.substring(0, name.lastIndexOf('.')); // remove ".class" 
     names.add(name); 
    } 

    return names; 
} 

tài liệu tham khảo hữu ích: https://stackoverflow.com/a/7461653/7255

0

Bạn có thể sử dụng LocalSessionFactoryBuilder cho việc xây dựng nhà máy phiên cho phép bạn chỉ định scanpackages tài sản.

SessionFactory sessionFactory = new LocalSessionFactoryBuilder(hikariDataSource()) 
     .scanPackages("com.animals.entities") 
     .addProperties(properties) 
     .buildSessionFactory(); 
Các vấn đề liên quan