来自hibernate.cfg.xml的Hibernate映射类

时间:2015-09-28 13:54:50

标签: java xml hibernate

对于来自hibernate.cfg.xml的类映射,我使用以下格式:

@Singular

如何使用一个映射行映射包中的所有类? 例如:<mapping class="packageName.className1"/>
<mapping class="packageName.className2"/>
<mapping class="packageName.className3"/>
使用bla-star不起作用!

2 个答案:

答案 0 :(得分:0)

错误&#34;解析XML时出错:hibernate2.cfg.xml(22)属性&#34;值&#34;必须声明元素类型&#34; property&#34; - 与包映射无关。

<mapping class="packageName.*"/>应该有效。

问题在于property元素。 property元素没有任何名为value的属性。

尝试:

<property name="hibernate.archive.autodetection">class, hbm</property>

而不是:

<property name="hibernate.archive.autodetection" value="class, hbm" />

答案 1 :(得分:0)

据我所知,没有直接的方法可以从hibernate.cfg.xml扫描包裹。您可以使用其他框架将会话工厂创建包装在自己的类中。

例如,您可以在创建会话工厂实例时使用spring-orm扫描包。

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="your-own-datasource"/>
    <property name="configLocation" value="classpath*:hibernate.cfg.xml"/>
    <property name="packagesToScan" value="your.package.name"/>
</bean>

或者您可以编写自己的SessionFactoryWrapper。创建SessionFactory时,您可以扫描包,然后在运行时添加它们。

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

import javax.persistence.Entity;
import javax.tools.FileObject;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.StandardLocation;
import javax.tools.ToolProvider;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

public class SessionFactoryWrapper {

    private final SessionFactory sessionFactory;

    public SessionFactoryWrapper(final String...packagesToScan) {
        this.sessionFactory = this.createSessionFactory(packagesToScan);
    }

    private SessionFactory createSessionFactory(final String[] packagesToScan) {
        final Configuration configuration = new Configuration();
        configuration.configure(); // Reads hibernate.cfg.xml from classpath

        for (String packageToScan : packagesToScan) {
            this.getEntityClasses(packageToScan).stream().forEach( configuration::addAnnotatedClass);
        }

        final ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
        return configuration.buildSessionFactory(serviceRegistry);
    }

    private Collection<Class> getEntityClasses(final String pack) {
        final StandardJavaFileManager fileManager = ToolProvider.getSystemJavaCompiler().getStandardFileManager(null, null, null);
        try {
            return StreamSupport.stream(fileManager.list(StandardLocation.CLASS_PATH, pack, Collections.singleton(JavaFileObject.Kind.CLASS), false).spliterator(), false)
                    .map(FileObject::getName)
                    .map(name -> {
                        try {
                            final String[] split = name
                                    .replace(".class", "")
                                    .replace(")", "")
                                    .split(Pattern.quote(File.separator));

                            final String fullClassName = pack + "." + split[split.length - 1];
                            return Class.forName(fullClassName);
                        } catch (ClassNotFoundException e) {
                            throw new RuntimeException(e);
                        }

                    })
                    .filter(aClass -> aClass.isAnnotationPresent(Entity.class))
                    .collect(Collectors.toCollection(ArrayList::new));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}