我目前正在将我的 Hibernate 版本升级到最新版本 5.2.10。我在 HibernateUtil 中替换了我的代码以创建 SessionFactory。
4.3.11.Final(上一版):
public class HibernateUtil {
private HibernateUtil() {}
private static SessionFactory sessionFactory;
private static Configuration configuration;
public static Configuration getConfiguration() {
return configuration;
}
private static SessionFactory buildSessionFactory() {
try {
if(sessionFactory == null) {
configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
sessionFactory = configuration
.buildSessionFactory(serviceRegistry);
}
return sessionFactory;
}
catch (Throwable ex) {
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return buildSessionFactory();
}
public static Session getSession() {
Session hibernateSession = getSessionFactory().getCurrentSession();
return hibernateSession;
}
public static void shutdown() {
getSessionFactory().close();
}
}
5.2.10 final (新):
public class HibernateUtil {
private static StandardServiceRegistry registry;
private static SessionFactory sessionFactory;
public static SessionFactory getSessionFactory() {
return buildSessionFactory();
}
public static SessionFactory buildSessionFactory() {
if (sessionFactory == null) {
try {
registry = new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();
MetadataSources sources = new MetadataSources(registry);
Metadata metadata = sources.getMetadataBuilder().build();
sessionFactory = metadata.getSessionFactoryBuilder().build();
} catch (Exception e) {
e.printStackTrace();
shutdown();
}
}
return sessionFactory;
}
public static Session getSession() {
Session hibernateSession = getSessionFactory().getCurrentSession();
return hibernateSession;
}
public static void shutdown() {
if (registry != null) {
StandardServiceRegistryBuilder.destroy(registry);
}
}
}
现在我有一个方法可以通过将数据库表名作为字符串传递给我获取列名列表。我之前在 4.3.11.Final 中这样做过:
public static List<String> getColumnNames(String tableName) {
List<String> columnList=null;
Map<String, ClassMetadata> map = HibernateUtil.getSessionFactory().getAllClassMetadata();
Iterator<Entry<String, ClassMetadata>> itr = map.entrySet().iterator();
while(itr.hasNext()){
ClassMetadata classMetaData = itr.next().getValue();
AbstractEntityPersister aep = (AbstractEntityPersister) classMetaData;
if(aep.getTableName().split("\\.")[1].equalsIgnoreCase(tableName)){
columnList = new ArrayList<String>();
String[] propertyNames = classMetaData.getPropertyNames();
for(String property : propertyNames){
try {
PersistentClass persistentClass = HibernateUtil .getConfiguration().getClassMapping(classMetaData.getEntityName());
String clmName = ((Column) persistentClass.getProperty(property).getColumnIterator().next()).getName();
columnList.add(clmName);
} catch(NoSuchElementException e){
log.error("Element not found idenfied as : "+property);
} catch(Exception e){
log.error(e.getMessage());
}
}
break;
}
}
return columnList;
}
现在,升级后它显示方法 getAllClassMetadata 已弃用,并且在获取 PersistentClass 对象时遇到困难。我看到一个类似的问题here但我无法完全找出解决方案。我必须更改当前代码的哪一部分才能使我的 getColumnNames() 方法完全像以前一样工作。我引用了文档,它说使用 EntityManagerFactory.getMetamodel() 代替,但我找不到合适的引用示例。我还必须为此更改 SessionFactory 创建机制吗?
最佳答案
首先,我们需要创建一个新的 MetadataExtractorIntegrator实现 Hibernate Integrator 的类:
public class MetadataExtractorIntegrator
implements org.hibernate.integrator.spi.Integrator {
public static final MetadataExtractorIntegrator INSTANCE =
new MetadataExtractorIntegrator();
private Database database;
@Override
public void integrate(
Metadata metadata,
SessionFactoryImplementor sessionFactory,
SessionFactoryServiceRegistry serviceRegistry) {
database = metadata.getDatabase();
}
@Override
public void disintegrate(
SessionFactoryImplementor sessionFactory,
SessionFactoryServiceRegistry serviceRegistry) {
}
public Database getDatabase() {
return database;
}
}
然后,我们只需配置 Hibernate 即可使用它。
如果您使用的是 Hibernate 引导机制,那么您可以像这样添加它:
final BootstrapServiceRegistryBuilder bsrb = new BootstrapServiceRegistryBuilder();
bsrb.enableAutoClose();
Integrator integrator = integrator();
if (integrator != null) {
bsrb.applyIntegrator( integrator );
}
final BootstrapServiceRegistry bsr = bsrb.build();
final StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder(bsr);
如果您使用 JPA 进行引导,那么您可以按如下方式进行:
protected EntityManagerFactory newEntityManagerFactory() {
PersistenceUnitInfo persistenceUnitInfo = persistenceUnitInfo(
getClass().getSimpleName()
);
Map<String, Object> configuration = new HashMap<>();
configuration.put("hibernate.integrator_provider",
(IntegratorProvider) () -> Collections.singletonList( MetadataExtractorIntegrator.INSTANCE )
);
EntityManagerFactoryBuilderImpl entityManagerFactoryBuilder = new EntityManagerFactoryBuilderImpl(
new PersistenceUnitInfoDescriptor(persistenceUnitInfo), configuration
);
return entityManagerFactoryBuilder.build();
}
现在,当运行以下测试时:
for(Namespace namespace : MetadataExtractorIntegrator.INSTANCE
.getDatabase()
.getNamespaces()) {
for( Table table : namespace.getTables()) {
LOGGER.info( "Table {} has the following columns: {}",
table,
StreamSupport.stream(
Spliterators.spliteratorUnknownSize(
table.getColumnIterator(),
Spliterator.ORDERED
),
false
)
.collect( Collectors.toList())
);
}
}
Hibernate 在日志中输出所有当前映射的表:
Table org.hibernate.mapping.Table(post) has the following columns: [
org.hibernate.mapping.Column(id),
org.hibernate.mapping.Column(title),
org.hibernate.mapping.Column(version)
]
Table org.hibernate.mapping.Table(post_comment) has the following columns: [
org.hibernate.mapping.Column(id),
org.hibernate.mapping.Column(review),
org.hibernate.mapping.Column(version),
org.hibernate.mapping.Column(post_id)
]
Table org.hibernate.mapping.Table(post_details) has the following columns: [
org.hibernate.mapping.Column(id),
org.hibernate.mapping.Column(created_by),
org.hibernate.mapping.Column(created_on),
org.hibernate.mapping.Column(version)
]
Table org.hibernate.mapping.Table(post_tag) has the following columns: [
org.hibernate.mapping.Column(post_id),
org.hibernate.mapping.Column(tag_id)
]
Table org.hibernate.mapping.Table(tag) has the following columns: [
org.hibernate.mapping.Column(id),
org.hibernate.mapping.Column(name),
org.hibernate.mapping.Column(version)
]
就是这样!
关于java - Hibernate 升级到 5.2 - 创建 session 工厂并替换 PersistentClass 以获取实体类属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43604928/
出于纯粹的兴趣,我很好奇如何按顺序创建PI,而不是在过程结果之后生成数字,而是让数字在过程本身生成时显示。如果是这种情况,那么数字可以自行产生,我可以对以前看到的数字实现垃圾收集,从而创建一个无限系列。结果只是在Pi系列之后每秒生成一个数字。这是我通过互联网筛选的结果:这是流行的计算机友好算法,类机器算法:defarccot(x,unity)xpow=unity/xn=1sign=1sum=0loopdoterm=xpow/nbreakifterm==0sum+=sign*(xpow/n)xpow/=x*xn+=2sign=-signendsumenddefcalc_pi(digits
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
在railstutorial中,作者为什么选择使用这个(代码list10.25):http://ruby.railstutorial.org/chapters/updating-showing-and-deleting-usersnamespace:dbdodesc"Filldatabasewithsampledata"task:populate=>:environmentdoRake::Task['db:reset'].invokeUser.create!(:name=>"ExampleUser",:email=>"example@railstutorial.org",:passwo
尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在
如何使用RSpec::Core::RakeTask初始化RSpecRake任务?require'rspec/core/rake_task'RSpec::Core::RakeTask.newdo|t|#whatdoIputinhere?endInitialize函数记录在http://rubydoc.info/github/rspec/rspec-core/RSpec/Core/RakeTask#initialize-instance_method没有很好的记录;它只是说:-(RakeTask)initialize(*args,&task_block)AnewinstanceofRake
我在我的Rails项目中使用Pow和powifygem。现在我尝试升级我的ruby版本(从1.9.3到2.0.0,我使用RVM)当我切换ruby版本、安装所有gem依赖项时,我通过运行railss并访问localhost:3000确保该应用程序正常运行以前,我通过使用pow访问http://my_app.dev来浏览我的应用程序。升级后,由于错误Bundler::RubyVersionMismatch:YourRubyversionis1.9.3,butyourGemfilespecified2.0.0,此url不起作用我尝试过的:重新创建pow应用程序重启pow服务器更新战俘
我实际上是在尝试使用RVM在我的OSX10.7.5上更新ruby,并在输入以下命令后:rvminstallruby我得到了以下回复:Searchingforbinaryrubies,thismighttakesometime.Checkingrequirementsforosx.Installingrequirementsforosx.Updatingsystem.......Errorrunning'requirements_osx_brew_update_systemruby-2.0.0-p247',pleaseread/Users/username/.rvm/log/138121
关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion为什么SecureRandom.uuid创建一个唯一的字符串?SecureRandom.uuid#=>"35cb4e30-54e1-49f9-b5ce-4134799eb2c0"SecureRandom.uuid方法创建的字符串从不重复?