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
  • Loading branch information
mawiesne committed Nov 13, 2023
1 parent 731081d commit 471a690
Show file tree
Hide file tree
Showing 8 changed files with 242 additions and 169 deletions.
68 changes: 36 additions & 32 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 @@ -181,17 +181,18 @@ public int getPageId()
*/
public Set<Category> getParents()
{
Session session = this.wiki.__getHibernateSession();
session.beginTransaction();
session.lock(hibernateCategory, LockMode.NONE);
Set<Integer> tmpSet = new HashSet<>(hibernateCategory.getInLinks());
session.getTransaction().commit();

Set<Category> categories = new HashSet<>();
for (int pageID : tmpSet) {
categories.add(this.wiki.getCategory(pageID));
try (Session session = this.wiki.__getHibernateSession()) {
session.beginTransaction();
session.lock(hibernateCategory, LockMode.NONE);
Set<Integer> tmpSet = new HashSet<>(hibernateCategory.getInLinks());
session.getTransaction().commit();

Set<Category> categories = new HashSet<>();
for (int pageID : tmpSet) {
categories.add(this.wiki.getCategory(pageID));
}
return categories;
}
return categories;
}

/**
Expand Down Expand Up @@ -223,30 +224,32 @@ public int getNumberOfParents()
*/
public Set<Integer> getParentIDs()
{
Session session = this.wiki.__getHibernateSession();
session.beginTransaction();
session.lock(hibernateCategory, LockMode.NONE);
Set<Integer> tmpSet = new HashSet<>(hibernateCategory.getInLinks());
session.getTransaction().commit();
return tmpSet;
try (Session session = this.wiki.__getHibernateSession()) {
session.beginTransaction();
session.lock(hibernateCategory, LockMode.NONE);
Set<Integer> tmpSet = new HashSet<>(hibernateCategory.getInLinks());
session.getTransaction().commit();
return tmpSet;
}
}

/**
* @return A set containing the children (subcategories) of this category.
*/
public Set<Category> getChildren()
{
Session session = this.wiki.__getHibernateSession();
session.beginTransaction();
session.lock(hibernateCategory, LockMode.NONE);
Set<Integer> tmpSet = new HashSet<>(hibernateCategory.getOutLinks());
session.getTransaction().commit();

Set<Category> categories = new HashSet<>();
for (int pageID : tmpSet) {
categories.add(this.wiki.getCategory(pageID));
try (Session session = this.wiki.__getHibernateSession()) {
session.beginTransaction();
session.lock(hibernateCategory, LockMode.NONE);
Set<Integer> tmpSet = new HashSet<>(hibernateCategory.getOutLinks());
session.getTransaction().commit();

Set<Category> categories = new HashSet<>();
for (int pageID : tmpSet) {
categories.add(this.wiki.getCategory(pageID));
}
return categories;
}
return categories;
}

/**
Expand Down Expand Up @@ -322,13 +325,14 @@ public Set<Page> getArticles() throws WikiApiException
*/
public Set<Integer> getArticleIds()
{
Session session = this.wiki.__getHibernateSession();
session.beginTransaction();
session.lock(hibernateCategory, LockMode.NONE);
Set<Integer> tmpSet = new HashSet<>(hibernateCategory.getPages());
session.getTransaction().commit();
try (Session session = this.wiki.__getHibernateSession()) {
session.beginTransaction();
session.lock(hibernateCategory, LockMode.NONE);
Set<Integer> tmpSet = new HashSet<>(hibernateCategory.getPages());
session.getTransaction().commit();

return tmpSet;
return tmpSet;
}
}

/**
Expand Down
131 changes: 68 additions & 63 deletions dkpro-jwpl-api/src/main/java/org/dkpro/jwpl/api/Page.java
Original file line number Diff line number Diff line change
Expand Up @@ -276,18 +276,19 @@ public int getPageId()
*/
public Set<Category> getCategories()
{
Session session = this.wiki.__getHibernateSession();
session.beginTransaction();
session.buildLockRequest(LockOptions.NONE).lock(hibernatePage);
Set<Integer> tmp = new UnmodifiableArraySet<>(hibernatePage.getCategories());
session.getTransaction().commit();
try (Session session = this.wiki.__getHibernateSession()) {
session.beginTransaction();
session.buildLockRequest(LockOptions.NONE).lock(hibernatePage);
Set<Integer> tmp = new UnmodifiableArraySet<>(hibernatePage.getCategories());
session.getTransaction().commit();

Set<Category> categories = new HashSet<>();
for (int pageID : tmp) {
categories.add(wiki.getCategory(pageID));
}

Set<Category> categories = new HashSet<>();
for (int pageID : tmp) {
categories.add(wiki.getCategory(pageID));
return categories;
}

return categories;
}

/**
Expand Down Expand Up @@ -325,25 +326,26 @@ public int getNumberOfCategories()
*/
public Set<Page> getInlinks()
{
Session session = wiki.__getHibernateSession();
session.beginTransaction();
session.buildLockRequest(LockOptions.NONE).lock(hibernatePage);
// Have to copy links here since getPage later will close the session.
Set<Integer> pageIDs = new UnmodifiableArraySet<>(hibernatePage.getInLinks());
session.getTransaction().commit();

Set<Page> pages = new HashSet<>();
for (int pageID : pageIDs) {
try {
pages.add(wiki.getPage(pageID));
}
catch (WikiApiException e) {
// Silently ignore if a page could not be found
// There may be inlinks that do not come from an existing page.
try (Session session = wiki.__getHibernateSession()) {
session.beginTransaction();
session.buildLockRequest(LockOptions.NONE).lock(hibernatePage);
// Have to copy links here since getPage later will close the session.
Set<Integer> pageIDs = new UnmodifiableArraySet<>(hibernatePage.getInLinks());
session.getTransaction().commit();

Set<Page> pages = new HashSet<>();
for (int pageID : pageIDs) {
try {
pages.add(wiki.getPage(pageID));
}
catch (WikiApiException e) {
// Silently ignore if a page could not be found
// There may be inlinks that do not come from an existing page.
}
}
}

return pages;
return pages;
}
}

/**
Expand Down Expand Up @@ -378,14 +380,15 @@ public int getNumberOfInlinks()
*/
public Set<Integer> getInlinkIDs()
{
Session session = wiki.__getHibernateSession();
session.beginTransaction();
session.buildLockRequest(LockOptions.NONE).lock(hibernatePage);
try (Session session = wiki.__getHibernateSession()) {
session.beginTransaction();
session.buildLockRequest(LockOptions.NONE).lock(hibernatePage);

Set<Integer> tmpSet = new HashSet<>(hibernatePage.getInLinks());
Set<Integer> tmpSet = new HashSet<>(hibernatePage.getInLinks());

session.getTransaction().commit();
return tmpSet;
session.getTransaction().commit();
return tmpSet;
}
}

/**
Expand All @@ -398,25 +401,26 @@ public Set<Integer> getInlinkIDs()
*/
public Set<Page> getOutlinks()
{
Session session = wiki.__getHibernateSession();
session.beginTransaction();
// session.lock(hibernatePage, LockMode.NONE);
session.buildLockRequest(LockOptions.NONE).lock(hibernatePage);
// Have to copy links here since getPage later will close the session.
Set<Integer> tmpSet = new UnmodifiableArraySet<>(hibernatePage.getOutLinks());
session.getTransaction().commit();

Set<Page> pages = new HashSet<>();
for (int pageID : tmpSet) {
try {
pages.add(wiki.getPage(pageID));
}
catch (WikiApiException e) {
// Silently ignore if a page could not be found.
// There may be outlinks pointing to non-existing pages.
try (Session session = wiki.__getHibernateSession()) {
session.beginTransaction();
// session.lock(hibernatePage, LockMode.NONE);
session.buildLockRequest(LockOptions.NONE).lock(hibernatePage);
// Have to copy links here since getPage later will close the session.
Set<Integer> tmpSet = new UnmodifiableArraySet<>(hibernatePage.getOutLinks());
session.getTransaction().commit();

Set<Page> pages = new HashSet<>();
for (int pageID : tmpSet) {
try {
pages.add(wiki.getPage(pageID));
}
catch (WikiApiException e) {
// Silently ignore if a page could not be found.
// There may be outlinks pointing to non-existing pages.
}
}
return pages;
}
return pages;
}

/**
Expand Down Expand Up @@ -451,15 +455,15 @@ public int getNumberOfOutlinks()
*/
public Set<Integer> getOutlinkIDs()
{
try (Session session = wiki.__getHibernateSession()) {
session.beginTransaction();
session.buildLockRequest(LockOptions.NONE).lock(hibernatePage);

Session session = wiki.__getHibernateSession();
session.beginTransaction();
session.buildLockRequest(LockOptions.NONE).lock(hibernatePage);

Set<Integer> tmpSet = new HashSet<>(hibernatePage.getOutLinks());
Set<Integer> tmpSet = new HashSet<>(hibernatePage.getOutLinks());

session.getTransaction().commit();
return tmpSet;
session.getTransaction().commit();
return tmpSet;
}
}

/**
Expand All @@ -481,12 +485,13 @@ public Title getTitle() throws WikiTitleParsingException
*/
public Set<String> getRedirects()
{
Session session = wiki.__getHibernateSession();
session.beginTransaction();
session.buildLockRequest(LockOptions.NONE).lock(hibernatePage);
Set<String> tmpSet = new HashSet<>(hibernatePage.getRedirects());
session.getTransaction().commit();
return tmpSet;
try (Session session = wiki.__getHibernateSession()) {
session.beginTransaction();
session.buildLockRequest(LockOptions.NONE).lock(hibernatePage);
Set<String> tmpSet = new HashSet<>(hibernatePage.getRedirects());
session.getTransaction().commit();
return tmpSet;
}
}

/**
Expand Down
29 changes: 15 additions & 14 deletions dkpro-jwpl-api/src/main/java/org/dkpro/jwpl/api/Wikipedia.java
Original file line number Diff line number Diff line change
Expand Up @@ -592,23 +592,24 @@ public Set<Category> getCategories(String pageTitle) throws WikiPageNotFoundExce
throw new WikiPageNotFoundException();
}

Session session = this.__getHibernateSession();
session.beginTransaction();
String sql = "select c from Page p left join p.categories c where p.name = :pageTitle";
List<Integer> categoryHibernateIds = session.createQuery(sql, Integer.class)
.setParameter("pageTitle", pageTitle).list();
session.getTransaction().commit();
try (Session session = this.__getHibernateSession()) {
session.beginTransaction();
String sql = "select c from Page p left join p.categories c where p.name = :pageTitle";
List<Integer> categoryHibernateIds = session.createQuery(sql, Integer.class)
.setParameter("pageTitle", pageTitle).list();
session.getTransaction().commit();

Set<Category> categorySet = new HashSet<>(categoryHibernateIds.size());
for (int hibernateId : categoryHibernateIds) {
try {
categorySet.add(new Category(this, hibernateId));
}
catch (WikiPageNotFoundException e) {
logger.warn("Could not load Category by it's HibernateId = '" + hibernateId + "'");
Set<Category> categorySet = new HashSet<>(categoryHibernateIds.size());
for (int hibernateId : categoryHibernateIds) {
try {
categorySet.add(new Category(this, hibernateId));
}
catch (WikiPageNotFoundException e) {
logger.warn("Could not load Category by it's HibernateId = '" + hibernateId + "'");
}
}
return categorySet;
}
return categorySet;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,6 @@ public Set<Integer> getOutLinks()
return outLinks;
}

public int getOutDegree()
{
return outLinks.size();
}

public void setOutLinks(Set<Integer> outLinks)
{
this.outLinks = outLinks;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ 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 @@ -138,10 +141,10 @@ else if (jdbcURL.toLowerCase().contains("hsql")) {

private static Configuration getConfiguration(DatabaseConfiguration config)
{
Configuration cfg = new Configuration().addClass(Category.class).addClass(MetaData.class)
.addClass(Page.class).addClass(PageMapLine.class)
// .addClass(RelatednessCacheLine.class)
.addProperties(getProperties(config));
Configuration cfg = new Configuration();
cfg.addProperties(getProperties(config));
cfg.addURL(WikiHibernateUtil.class.getResource("jwpl-orm.hbm.xml"));
// cfg.addClass(Category.class).addClass(MetaData.class).addClass(Page.class).addClass(PageMapLine.class);
return cfg;
}

Expand Down
22 changes: 22 additions & 0 deletions dkpro-jwpl-api/src/main/resources/META-INF/orm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?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">
<attributes>
<id name="id">
<generated-value strategy="AUTO"/>
</id>
<basic name="pageId">
<column unique="true"/>
</basic>
<basic name="name">
TODO index...
</basic>
<one-to-many name="inLinks">
</one-to-many>
</attributes>
</entity>
</entity-mappings>
Loading

0 comments on commit 471a690

Please sign in to comment.