gpt4 book ai didi

java - Hibernate 升级到 5.2 - 创建 session 工厂并替换 PersistentClass 以获取实体类属性

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:46:14 24 4
gpt4 key购买 nike

我目前正在将我的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com