Skip to content

Fixing bytea json conversion error (update to hibernate 6, spring 3) #692

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@
import io.hypersistence.utils.hibernate.type.json.JsonType;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import jakarta.persistence.PersistenceContextType;
import org.commonwl.view.git.GitDetails;
import org.hibernate.query.Query;
import org.springframework.transaction.annotation.Transactional;

public class QueuedWorkflowRepositoryImpl implements QueuedWorkflowRepositoryCustom {

// Tested this query directly, and it works! Problem is elsewhere!
private static final String QUERY_FIND_BY_RETRIEVED_FROM =
"SELECT q.* FROM queued_workflow q WHERE q.temp_representation -> 'retrievedFrom' = :retrievedFrom";

private static final String QUERY_DELETE_BY_RETRIEVED_FROM =
"DELETE FROM queued_workflow q WHERE q.temp_representation -> 'retrievedFrom' = :retrievedFrom";

@PersistenceContext EntityManager entityManager;
@PersistenceContext(type = PersistenceContextType.EXTENDED)
EntityManager entityManager;

@Override
public QueuedWorkflow findByRetrievedFrom(GitDetails retrievedFrom) {
Expand All @@ -27,22 +29,30 @@ public QueuedWorkflow findByRetrievedFrom(GitDetails retrievedFrom) {
// out of ideas, so started testing everything found online):
// Use `new JsonType(GitDetails.class)`. That finally solved it.
// Ref: https://github.com/common-workflow-language/cwlviewer/pull/568
return (QueuedWorkflow)
final Query<?> query =
entityManager
.createNativeQuery(QUERY_FIND_BY_RETRIEVED_FROM, QueuedWorkflow.class)
.unwrap(Query.class)
.setParameter("retrievedFrom", retrievedFrom, new JsonType(GitDetails.class))
.stream()
.findFirst()
.orElse(null);
.unwrap(Query.class);

if (query == null) {
return null;
}

query.setParameter("retrievedFrom", retrievedFrom, new JsonType(GitDetails.class));
return (QueuedWorkflow) query.uniqueResult();
}

@Transactional
@Override
public void deleteByTempRepresentation_RetrievedFrom(GitDetails retrievedFrom) {
entityManager
.createNativeQuery(QUERY_DELETE_BY_RETRIEVED_FROM, QueuedWorkflow.class)
.unwrap(Query.class)
.setParameter("retrievedFrom", retrievedFrom, new JsonType(GitDetails.class))
.executeUpdate();
final Query<?> query =
entityManager
.createNativeQuery(QUERY_DELETE_BY_RETRIEVED_FROM, QueuedWorkflow.class)
.unwrap(Query.class);

if (query != null) {
query.setParameter("retrievedFrom", retrievedFrom, new JsonType(GitDetails.class));
query.executeUpdate();
}
}
}
14 changes: 3 additions & 11 deletions src/main/java/org/commonwl/view/workflow/WorkflowRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@
package org.commonwl.view.workflow;

import java.util.List;
import org.commonwl.view.git.GitDetails;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

/**
Expand All @@ -34,15 +33,8 @@
* href="https://docs.spring.io/spring-data/jpa/docs/current/reference/html/">...</a>
*/
@Repository
public interface WorkflowRepository extends CrudRepository<Workflow, String> {

/**
* Finds a workflow model in the database based on where it was retrieved from
*
* @param retrievedFrom Details of where the workflow is from
* @return The workflow model
*/
Workflow findByRetrievedFrom(GitDetails retrievedFrom);
public interface WorkflowRepository
extends JpaRepository<Workflow, String>, WorkflowRepositoryCustom {

/**
* Finds a workflow model in the database based on a commit ID and path
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
Comment on lines +2 to +8
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cwlviewer is not part of Apache, so perhaps we shouldn't say this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1. Huh, I think I copied the header from another file in the project. I hope I didn't accidentally copy-paste from a file outside the project (or a file that was copied from an AL project).

*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.commonwl.view.workflow;

import org.commonwl.view.git.GitDetails;

public interface WorkflowRepositoryCustom {

/**
* Finds a workflow model in the database based on where it was retrieved from
*
* @param retrievedFrom Details of where the workflow is from
* @return The workflow model
*/
Workflow findByRetrievedFrom(GitDetails retrievedFrom);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.commonwl.view.workflow;

import io.hypersistence.utils.hibernate.type.json.JsonType;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import jakarta.persistence.PersistenceContextType;
import org.commonwl.view.git.GitDetails;
import org.hibernate.query.Query;

public class WorkflowRepositoryImpl implements WorkflowRepositoryCustom {

private static final String QUERY_FIND_BY_RETRIEVED_FROM =
"SELECT w.* FROM workflow w WHERE w.retrieved_from = :retrievedFrom";

@PersistenceContext(type = PersistenceContextType.EXTENDED)
EntityManager entityManager;

@Override
public Workflow findByRetrievedFrom(GitDetails retrievedFrom) {
final Query<?> query =
entityManager
.createNativeQuery(QUERY_FIND_BY_RETRIEVED_FROM, Workflow.class)
.unwrap(Query.class);

if (query == null) {
return null;
}

query.setParameter("retrievedFrom", retrievedFrom, new JsonType(GitDetails.class));
return (Workflow) query.uniqueResult();
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to self: write a test for this!

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class PostgreSQLContextInitializer
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
PostgreSQLContainer<?> postgreSQLContainer =
new PostgreSQLContainer<>("postgres:9.6.12")
new PostgreSQLContainer<>("postgres:17.4")
.withDatabaseName("cwlviewer")
.withUsername("sa")
.withPassword("sa");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@TestPropertySource(locations = "classpath:it-application.properties")
@DataJpaTest(showSql = true)
@Transactional(propagation = Propagation.NOT_SUPPORTED)
@ContextConfiguration(initializers = PostgreSQLContextInitializer.class)
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
public class QueuedWorkflowRepositoryTest {

@Autowired QueuedWorkflowRepository repository;

@Transactional
@Test
public void deleteQueuedWorkflowByRetrievedFromTest() {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.commonwl.view.workflow;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;

import java.util.List;
import org.commonwl.view.git.GitDetails;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@TestPropertySource(locations = "classpath:it-application.properties")
@DataJpaTest(showSql = true)
@Transactional(propagation = Propagation.NOT_SUPPORTED)
@ContextConfiguration(initializers = PostgreSQLContextInitializer.class)
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
public class WorkflowRepositoryTest {

@Autowired WorkflowRepository repository;

@Test
public void deletedWorkflowByRetrievedFromTest() {

assertNotNull(repository);

// create stub workflow
GitDetails gitDetails =
new GitDetails("https://github.com/common-workflow-language/cwlviewer/", "main", "/");
gitDetails.setPackedId("test_packedId");

Workflow workflow = new Workflow();
workflow.setRetrievedFrom(gitDetails);

// save workflow
repository.saveAndFlush(workflow);

List<Workflow> all = repository.findAll();
assertNotNull(all);
assertEquals(1, all.size());

// retrieve saved workflow by git details
Workflow retrievedWorkflowAfterSave =
repository.findByRetrievedFrom(workflow.getRetrievedFrom());
assertNotNull(retrievedWorkflowAfterSave);

// delete saved workflow by git details
repository.delete(workflow);

// retrieve deleted workflow by workflow git details
Workflow retrievedWorkflowAfterDelete =
repository.findByRetrievedFrom(workflow.getRetrievedFrom());
assertNull(retrievedWorkflowAfterDelete);
}
}