Skip to content

Commit

Permalink
Experimental attempt to migrate away from hbm.xml files via transitio…
Browse files Browse the repository at this point in the history
…n property provided since Hibernate 6.x

- adds orm.xml file extracted by rzo1
- switches to orm.xml configuration variant to avoid setting the on-the-fly transformation parameter
  • Loading branch information
mawiesne committed Nov 15, 2023
1 parent 471a690 commit 0984982
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 45 deletions.
50 changes: 26 additions & 24 deletions dkpro-jwpl-api/src/main/java/org/dkpro/jwpl/api/Category.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,14 @@ public Category(Wikipedia wiki, String pName) throws WikiApiException
*/
private void createCategory(long id) throws WikiPageNotFoundException
{
Session session = this.wiki.__getHibernateSession();
session.beginTransaction();
hibernateCategory = catDAO.findById(id);
session.getTransaction().commit();
try (Session session = this.wiki.__getHibernateSession()) {
session.beginTransaction();
hibernateCategory = catDAO.findById(id);
session.getTransaction().commit();

if (hibernateCategory == null) {
throw new WikiPageNotFoundException("No category with id " + id + " was found.");
if (hibernateCategory == null) {
throw new WikiPageNotFoundException("No category with id " + id + " was found.");
}
}
}

Expand All @@ -120,27 +121,28 @@ private void createCategory(int pageID) throws WikiPageNotFoundException
private void createCategory(Title title) throws WikiPageNotFoundException
{
String name = title.getWikiStyleTitle();
Session session = this.wiki.__getHibernateSession();
session.beginTransaction();
try (Session session = this.wiki.__getHibernateSession()) {
session.beginTransaction();

Integer returnValue;
Integer returnValue;

String query = "select cat.pageId from Category as cat where cat.name = :name";
if (wiki.getDatabaseConfiguration().supportsCollation()) {
query += Wikipedia.SQL_COLLATION;
}
returnValue = session.createNativeQuery(query, Integer.class)
.setParameter("name", name, StandardBasicTypes.STRING).uniqueResult();
session.getTransaction().commit();
String query = "select cat.pageId from Category as cat where cat.name = :name";
if (wiki.getDatabaseConfiguration().supportsCollation()) {
query += Wikipedia.SQL_COLLATION;
}
returnValue = session.createNativeQuery(query, Integer.class)
.setParameter("name", name, StandardBasicTypes.STRING).uniqueResult();
session.getTransaction().commit();

// if there is no category with this name, the hibernateCategory is null
if (returnValue == null) {
hibernateCategory = null;
throw new WikiPageNotFoundException("No category with name " + name + " was found.");
}
else {
int pageID = returnValue;
createCategory(pageID);
// if there is no category with this name, the hibernateCategory is null
if (returnValue == null) {
hibernateCategory = null;
throw new WikiPageNotFoundException("No category with name " + name + " was found.");
}
else {
int pageID = returnValue;
createCategory(pageID);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,6 @@ else if (jdbcURL.toLowerCase().contains("hsql")) {
// Leave this set 'true' as this is required for dynamic Dialect resolution!
p.setProperty("hibernate.temp.use_jdbc_metadata_defaults", "true");

// TODO @rzo1: The topic / party starts with this 'new' magic property
p.setProperty("hibernate.transform_hbm_xml.enabled", "true");

if (useMySQL || useMariaDB) {
// Set C3P0 Connection Pool in case somebody wants to use it in production settings
// if no C3P0 is available at runtime, related warnings can be ignored safely as the
Expand All @@ -136,14 +133,24 @@ else if (jdbcURL.toLowerCase().contains("hsql")) {
p.setProperty("hibernate.c3p0.max_statements", "100");
p.setProperty("hibernate.c3p0.timeout", "1000");
}

// TODO @rzo1: Required if the old-fashioned HBM-XML approach shall be demonstrated.
// p.setProperty("hibernate.transform_hbm_xml.enabled", "true");

return p;
}

private static Configuration getConfiguration(DatabaseConfiguration config)
{
Configuration cfg = new Configuration();
cfg.addProperties(getProperties(config));
cfg.addURL(WikiHibernateUtil.class.getResource("jwpl-orm.hbm.xml"));
// Variant-1: Modernized, scanning via orm.xml file
cfg.addURL(WikiHibernateUtil.class.getResource("/META-INF/orm.xml"));

// Variant-2: Scanning via combined HBM xml files
// cfg.addURL(WikiHibernateUtil.class.getResource("jwpl-orm.hbm.xml"));

// Variant-3: Auto-Scanning via class files + xml at runtime (DEPRECATED / not flexible)
// cfg.addClass(Category.class).addClass(MetaData.class).addClass(Page.class).addClass(PageMapLine.class);
return cfg;
}
Expand Down
133 changes: 116 additions & 17 deletions dkpro-jwpl-api/src/main/resources/META-INF/orm.xml
Original file line number Diff line number Diff line change
@@ -1,22 +1,121 @@
<?xml version="1.0" encoding="UTF-8" ?>
<entity-mappings xmlns="https://jakarta.ee/xml/ns/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/persistence/orm
https://jakarta.ee/xml/ns/persistence/orm/orm_3_0.xsd"
version="3.0">
<entity class="org.dkpro.jwpl.api.hibernate.Category" metadata-complete="true">
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<entity-mappings xmlns="http://www.hibernate.org/xsd/orm/mapping">
<description>mapping.xml document auto-generated from legacy hbm.xml format via transformation</description>
<persistence-unit-metadata/>
<attribute-accessor>field</attribute-accessor>
<default-cascade>none</default-cascade>
<default-lazy>true</default-lazy>
<entity class="org.dkpro.jwpl.api.hibernate.Category" access="FIELD" metadata-complete="true">
<table name="Category"/>
<dynamic-insert>false</dynamic-insert>
<dynamic-update>false</dynamic-update>
<select-before-update>false</select-before-update>
<batch-size>-1</batch-size>
<mutable>true</mutable>
<optimistic-lock>version</optimistic-lock>
<polymorphism>implicit</polymorphism>
<attributes>
<id name="id">
<generated-value strategy="AUTO"/>
<id name="id" access="FIELD">
<column name="id"/>
</id>
<basic name="pageId">
<column unique="true"/>
</basic>
<basic name="name">
TODO index...
</basic>
<one-to-many name="inLinks">
</one-to-many>
<basic name="pageId" fetch="EAGER" optional="true" access="FIELD" optimistic-lock="true"/>
<basic name="name" fetch="EAGER" optional="true" access="FIELD" optimistic-lock="true"/>
<element-collection name="inLinks" fetch="EAGER" access="FIELD">
<sort>unsorted</sort>
<column name="inLinks" unique="false" nullable="true" insertable="true" updatable="true"/>
<collection-table name="category_inlinks"/>
</element-collection>
<element-collection name="outLinks" fetch="EAGER" access="FIELD">
<sort>unsorted</sort>
<column name="outLinks" unique="false" nullable="true" insertable="true" updatable="true"/>
<collection-table name="category_outlinks"/>
</element-collection>
<element-collection name="pages" fetch="EAGER" access="FIELD">
<sort>unsorted</sort>
<column name="pages" unique="false" nullable="true" insertable="true" updatable="true"/>
<collection-table name="category_pages"/>
</element-collection>
</attributes>
</entity>
<entity class="org.dkpro.jwpl.api.hibernate.MetaData" access="FIELD" metadata-complete="true">
<table name="MetaData"/>
<dynamic-insert>false</dynamic-insert>
<dynamic-update>false</dynamic-update>
<select-before-update>false</select-before-update>
<batch-size>-1</batch-size>
<mutable>true</mutable>
<optimistic-lock>version</optimistic-lock>
<polymorphism>implicit</polymorphism>
<attributes>
<id name="id" access="FIELD">
<column name="id"/>
</id>
<basic name="language" fetch="EAGER" optional="true" access="FIELD" optimistic-lock="true"/>
<basic name="disambiguationCategory" fetch="EAGER" optional="true" access="FIELD" optimistic-lock="true"/>
<basic name="mainCategory" fetch="EAGER" optional="true" access="FIELD" optimistic-lock="true"/>
<basic name="nrofPages" fetch="EAGER" optional="true" access="FIELD" optimistic-lock="true"/>
<basic name="nrofRedirects" fetch="EAGER" optional="true" access="FIELD" optimistic-lock="true"/>
<basic name="nrofDisambiguationPages" fetch="EAGER" optional="true" access="FIELD" optimistic-lock="true"/>
<basic name="nrofCategories" fetch="EAGER" optional="true" access="FIELD" optimistic-lock="true"/>
<basic name="version" fetch="EAGER" optional="true" access="FIELD" optimistic-lock="true"/>
</attributes>
</entity>
<entity class="org.dkpro.jwpl.api.hibernate.Page" access="FIELD" metadata-complete="true">
<table name="Page"/>
<dynamic-insert>false</dynamic-insert>
<dynamic-update>false</dynamic-update>
<select-before-update>false</select-before-update>
<batch-size>-1</batch-size>
<mutable>true</mutable>
<optimistic-lock>version</optimistic-lock>
<polymorphism>implicit</polymorphism>
<attributes>
<id name="id" access="FIELD">
<column name="id"/>
</id>
<basic name="pageId" fetch="EAGER" optional="true" access="FIELD" optimistic-lock="true"/>
<basic name="name" fetch="EAGER" optional="true" access="FIELD" optimistic-lock="true"/>
<basic name="text" fetch="EAGER" optional="true" access="FIELD" optimistic-lock="true"/>
<basic name="isDisambiguation" fetch="EAGER" optional="true" access="FIELD" optimistic-lock="true"/>
<element-collection name="inLinks" fetch="EAGER" access="FIELD">
<sort>unsorted</sort>
<column name="inLinks" unique="false" nullable="true" insertable="true" updatable="true"/>
<collection-table name="page_inlinks"/>
</element-collection>
<element-collection name="outLinks" fetch="EAGER" access="FIELD">
<sort>unsorted</sort>
<column name="outLinks" unique="false" nullable="true" insertable="true" updatable="true"/>
<collection-table name="page_outlinks"/>
</element-collection>
<element-collection name="categories" fetch="EAGER" access="FIELD">
<sort>unsorted</sort>
<column name="pages" unique="false" nullable="true" insertable="true" updatable="true"/>
<collection-table name="page_categories"/>
</element-collection>
<element-collection name="redirects" fetch="EAGER" access="FIELD">
<sort>unsorted</sort>
<column name="redirects" unique="false" nullable="true" insertable="true" updatable="true"/>
<collection-table name="page_redirects"/>
</element-collection>
</attributes>
</entity>
<entity class="org.dkpro.jwpl.api.hibernate.PageMapLine" access="FIELD" metadata-complete="true">
<table name="PageMapLine"/>
<dynamic-insert>false</dynamic-insert>
<dynamic-update>false</dynamic-update>
<select-before-update>false</select-before-update>
<batch-size>-1</batch-size>
<mutable>true</mutable>
<optimistic-lock>version</optimistic-lock>
<polymorphism>implicit</polymorphism>
<attributes>
<id name="id" access="FIELD">
<column name="id"/>
</id>
<basic name="name" fetch="EAGER" optional="true" access="FIELD" optimistic-lock="true"/>
<basic name="pageID" fetch="EAGER" optional="true" access="FIELD" optimistic-lock="true"/>
<basic name="stem" fetch="EAGER" optional="true" access="FIELD" optimistic-lock="true"/>
<basic name="lemma" fetch="EAGER" optional="true" access="FIELD" optimistic-lock="true"/>
</attributes>
</entity>
</entity-mappings>

0 comments on commit 0984982

Please sign in to comment.