Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java hibernate是否可以扫描包以自动创建SessionFactory?_Java_Hibernate_Hibernate Mapping - Fatal编程技术网

Java hibernate是否可以扫描包以自动创建SessionFactory?

Java hibernate是否可以扫描包以自动创建SessionFactory?,java,hibernate,hibernate-mapping,Java,Hibernate,Hibernate Mapping,我是否可以将Hibernate配置为自动扫描包,以从@Entity注释bean创建会话工厂 目前我正在使用 Configuration config = new Configuration() ; config.addAnnotatedClass(MyEntity.class); 我不想使用hibernate.cfg.xml来配置映射 请注意,我希望在一个普通的Java项目中实现这一点,而不使用任何Spring或类似的框架 以前使用Spring也回答过类似的问题,但我想在不使用Spring或其

我是否可以将Hibernate配置为自动扫描包,以从
@Entity
注释bean创建
会话工厂

目前我正在使用

Configuration config = new Configuration() ;
config.addAnnotatedClass(MyEntity.class);
我不想使用
hibernate.cfg.xml
来配置映射

请注意,我希望在一个普通的Java项目中实现这一点,而不使用任何Spring或类似的框架


以前使用Spring也回答过类似的问题,但我想在不使用Spring或其他框架的情况下实现它。我愿意使用一些简单的库来实现这一点

不可以。即使是上一个Hibernate 5版本,也不能说Hibernate扫描包中的持久类<代码>配置有方法
addPackage()
,但它用于读取“包级元数据”(
.package info
-文件)

您不想使用Spring,那么您可以做什么:

使用流畅的hibernate

你可以用 从库(除了库之外,您不需要其他jar)

对于Hibernate 4和Hibernate 5:

使用新的Hibernate 5引导API:

List
publicstaticarraylistlistfiles(字符串pckgname){
ArrayList类=新的ArrayList();
试一试{
//获取包的文件对象
文件目录=null;
试试{
目录=新文件(Thread.currentThread().getContextClassLoader().getResource(pckgname.replace('.','/')).getFile());
}捕获(NullPointerException x){
System.out.println(“空指针”);
抛出新的ClassNotFoundException(pckgname+“似乎不是有效的包”);
} 
如果(directory.exists()){
//获取包中包含的文件列表
String[]files=directory.list();
对于(int i=0;i
Configuration configuration = new Configuration();
EntityScanner.scanPackages("my.com.entities", "my.com.other.entities")
    .addTo(configuration);
SessionFactory sessionFactory = configuration.buildSessionFactory();
List<Class<?>> classes = EntityScanner
        .scanPackages("my.com.entities", "my.com.other.entities").result();

MetadataSources metadataSources = new MetadataSources();
for (Class<?> annotatedClass : classes) {
    metadataSources.addAnnotatedClass(annotatedClass);
}

SessionFactory sessionFactory = metadataSources.buildMetadata()
    .buildSessionFactory();
 public static  ArrayList listfiles(String pckgname) {
       ArrayList classes=new ArrayList();
try{

    // Get a File object for the package 
    File directory=null; 
    try { 
      directory=new File(Thread.currentThread().getContextClassLoader().getResource(pckgname.replace('.', '/')).getFile()); 
    } catch(NullPointerException x) { 
      System.out.println("Nullpointer");
      throw new ClassNotFoundException(pckgname+" does not appear to be a valid package"); 
    } 
    if(directory.exists()) { 
      // Get the list of the files contained in the package 
      String[] files=directory.list(); 
      for(int i=0; i<files.length; i++) { 
// we are only interested in .class files 
if(files[i].endsWith(".class")) { 
  // removes the .class extension 
  classes.add(Class.forName(pckgname+'.'+files[i].substring(0, files[i].length()-6))); 
} 
      } 
    } else { 
System.out.println("Directory does not exist");
      throw new ClassNotFoundException(pckgname+" does not appear to be a valid package"); 
    } 
    Class[] classesA=new Class[classes.size()]; 
    classes.toArray(classesA); 

     //       return classesA;

} catch (Exception e) {
e.printStackTrace();
}
return classes;
}