MySQL UUID string value without dashes convert to Java UUID

I am using MySql in my project.

I am getting UUID without dashes through native query…
What I get: aa66864883dd2da5aa029ab20eb60d29
Expected: aa668648-83dd-2da5-aa02-9ab20eb60d29

But other queries using dataManager works fine…

Problem: Need to get an entity by UUID that I get throught nativeQuery…
Question: Any method for converting string uuid with truncated dashes to UUID ???

When I try to get UUID id through native query, and try to get reference through datamanager by UUID I’m having trouble converting from String uuid value to UUID…

public List<Stock> getAggregatedStocks() {
    try (Transaction tx = persistence.getTransaction()) {
        EntityManager em = persistence.getEntityManager();

        Query query = em.createNativeQuery(
                "SELECT s.COMPANY_ID, s.BRANCH_ID, s.GOOD_ID, s.SN, sum(s.QUANTITY) as QUANTITY FROM dsm_stock s\n" +
                        "group by s.COMPANY_ID, s.BRANCH_ID, s.GOOD_ID, s.SN\n" +
                        "having QUANTITY <> 0");

        List list = query.getResultList();
        List<Stock> stocks = new ArrayList<>();
        for (Iterator it = list.iterator(); it.hasNext(); ) {
            Object[] row = (Object[]);
            Stock stock = new Stock();

            UUID companyId = UUID.fromString((String) row[0]); // throwing IllegalArgumentException ...
            stock.setCompany(dataManager.getReference(Company.class, companyId));

Tried “(UUID) persistence.getDbTypeConverter().getJavaObject(resultSet, 1)”, but also same…
Same problem generating UUID from String… because no dashes…

I tried “UUID.nameUUIDFromBytes(strUuid.getBytes())” also but it return totally different id generated… which also didn’t work for me…

Any help appreciated… Many thanks!

Solved currently!
Found two solutions using String format & Apache Commons Codec.

But I still think there’s built in solution, in Cuba under the hood it works somehow… May be on db driver level… Not sure exactly, just guessing…


Thank you for reporting the problem.
The framework does the UUID conversion internally in the com.haulmont.cuba.core.sys.UuidConverter class.
I’ve created an issue to support it also in the DbTypeConverter implementations.

1 Like