Native JPA query for LONGTEXT field in MySQL view results in an error

I have the following JPA SqlResultSetMapping:

@SqlResultSetMappings({ @SqlResultSetMapping(name="GroupParticipantDTO", columns={ @ColumnResult(name="gpId"), @ColumnResult(name="gpRole"), // @ColumnResult(name="gpRemarks") } ) 

What is used as follows:

  StringBuilder sbQuery = new StringBuilder("Select "); sbQuery.append(" gpId, "); sbQuery.append(" gpRole, "); // sbQuery.append(" gpRemarks "); sbQuery.append(" FROM v_group_participants_with_details "); Query query = em.createNativeQuery(sbQuery.toString(), "GroupParticipantDTO"); 

The view is as follows:

 DROP VIEW IF EXISTS `v_group_participants_with_details`; CREATE VIEW `v_group_participants_with_details` AS SELECT gp.id AS gpId, gp.role AS gpRole, gp.remarks AS gpRemarks FROM GroupParticipation gp ; 

The GroupParticipation table has a comment column defined as LONGTEXT (I use Mysql 5.x)

Now for the problem: When the comments field is commented out from the request, everything works fine, but if I try to include the comments field in the request, I get the following error:

  javax.persistence.PersistenceException: org.hibernate.MappingException: No Dialect mapping for JDBC type: -1 at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException (AbstractEntityManagerImpl.java:614) at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:76) 

What gives? How can I get the LONGTEXT column from my own query?

+4
source share
1 answer

This problem is presented in HHH-1483 and HHH-3892 . In short, Hibernate does not know how to match the LONGVARCHAR column returned by its own query.

This issue has been fixed in Hibernate 3.5.0+. For previous versions, a workaround would be the MysqlDialect extension to register the correct Hibernate Type for LONGVARCHAR :

 import java.sql.Types; import org.hibernate.Hibernate; public class MyMySQL5Dialect extends org.hibernate.dialect.MySQL5Dialect { public MyMySQL5Dialect() { super(); // register additional hibernate types for default use in scalar sqlquery type auto detection registerHibernateType(Types.LONGVARCHAR, Hibernate.TEXT.getName()); } } 
+7
source

Source: https://habr.com/ru/post/1286131/


All Articles