Java Hibernate4注释配置
我正在尝试使用Hibernate4,它只包含注释和一个Java Hibernate4注释配置,java,hibernate,hibernate-annotations,hibernate-4.x,hibernate.cfg.xml,Java,Hibernate,Hibernate Annotations,Hibernate 4.x,Hibernate.cfg.xml,我正在尝试使用Hibernate4,它只包含注释和一个Hibernate.cfg.xml文件。我已经做了自己的注释,并使用反射将其添加到配置中。我能够以这种方式使用Hibernate4,但我的配置是使用一种不推荐的方法构建的 final Configuration configuration = new Configuration(); final Reflections reflections = new Reflections(Item.class.getPackage().getName(
Hibernate.cfg.xml
文件。我已经做了自己的注释,并使用反射将其添加到配置中。我能够以这种方式使用Hibernate4,但我的配置是使用一种不推荐的方法构建的
final Configuration configuration = new Configuration();
final Reflections reflections = new Reflections(Item.class.getPackage().getName());
final Set<Class<?>> classes = reflections.getTypesAnnotatedWith(Entity.class);
for (final Class<?> clazz : classes) {
configuration.addAnnotatedClass(clazz);
}
return configuration.configure().buildSessionFactory();
最终配置=新配置();
最终反射=新反射(Item.class.getPackage().getName());
最终设置修改后的代码如下所示:-
final Configuration configuration = new Configuration();
final Reflections reflections = new Reflections(Item.class.getPackage().getName());
final Set<Class<?>> classes = reflections.getTypesAnnotatedWith(Entity.class);
for (final Class<?> clazz : classes) {
configuration.addAnnotatedClass(clazz);
}
ServiceRegistry serviceRegistry=new ServiceRegistryBuilder().applySettings
(configuration.getProperties()).buildServiceRegistry();
return configuration.buildSessionFactory(serviceRegistry);
最终配置=新配置();
最终反射=新反射(Item.class.getPackage().getName());
最后一集你看过这个帖子了吗:这里有一个投票结果很好的答案。