猿问

NullPointerException 导致:org.hibernate.Mapping

当我尝试运行本机查询时,当我尝试在 spring jpa 中运行时,出现以下错误native query。我已将方言添加到自定义方言中,扩展为


spring.jpa.database-platform=com.config.HibernateCustomDialect


Caused by: java.lang.NullPointerException: null

at com.vladmihalcea.hibernate.type.array.internal.AbstractArrayTypeDescriptor.setParameterValues(AbstractArrayTypeDescriptor.java:34) ~[hibernate-types-52-2.6.1.jar:na]

at com.vladmihalcea.hibernate.type.array.StringArrayType.setParameterValues(StringArrayType.java:50) ~[hibernate-types-52-2.6.1.jar:na]

at org.hibernate.type.TypeFactory.injectParameters(TypeFactory.java:149) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]

at org.hibernate.type.TypeFactory.type(TypeFactory.java:135) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]

下面是我的实体


@Entity

@Getter

@Setter

@NoArgsConstructor

@TypeDef(

    name = "string-array",

    typeClass = StringArrayType.class

)

public class Session {

   @Id

   @GeneratedValue(strategy = GenerationType.AUTO)

   private Long sessionId;


   @Type(type = "string-array")

   @Column(columnDefinition = "text[]", name = "tags")

   private String[] tags = null;


   private long oId;


   // More Fields


}   

自定义方言


public class HibernateCustomDialect extends org.hibernate.dialect.PostgreSQL94Dialect {

    private static final Logger log = LoggerFactory.getLogger(HibernateCustomDialect.class);



    public HibernateCustomDialect() {

       super();

       log.info("Registering Custom Hibernate Dialect - {}",HibernateCustomDialect.class.getName());

       this.registerHibernateType(Types.ARRAY, StringArrayType.class.getName());

    }

}

Jpa 存储库


@Repository

public interface SessionRepository extends JpaRepository<Session, Long> {

   String sessionSummaryViewQuery ="SELECT session.session_id, session.tags FROM public.session WHERE session.oid=?1"


   @Query(value =sessionSummaryViewQuery,nativeQuery = true)

   List<SessionSummaryView> findByOpportunityId(long opportunityId);//, Pageable pageable);



}


慕神8447489
浏览 93回答 1
1回答

缥缈止盈

在浏览互联网并扫描所有与休眠相关的页面后,我找到了一个解决方法。我实现了一个CustomStringArrayType扩展com.vladmihalcea.hibernate.type.array.StringArrayType.参考 -&nbsp;https://github.com/vladmihalcea/hibernate-types/issues/142我迫不及待地等待 PR 被合并,因此实现了如下解决方法:public class CustomStringArrayType extends StringArrayType {&nbsp; @Override&nbsp; public void setParameterValues(Properties parameters) {&nbsp; &nbsp; if (parameters.containsKey(super.PARAMETER_TYPE)) {&nbsp; &nbsp; &nbsp; &nbsp; super.setParameterValues(parameters);&nbsp; &nbsp; }&nbsp; }}我修改后的HibernateDialect如下:public class HibernateCustomDialect extends org.hibernate.dialect.PostgreSQL94Dialect {&nbsp; private static final Logger log = LoggerFactory.getLogger(HibernateCustomDialect.class);&nbsp; public HibernateCustomDialect() {&nbsp; &nbsp; super();&nbsp; &nbsp; log.info("Registering Custom Hibernate Dialect - {}",HibernateCustomDialect.class.getName());&nbsp; &nbsp; this.registerHibernateType(Types.ARRAY, CustomStringArrayType.class.getName());&nbsp; }}
随时随地看视频慕课网APP

相关分类

Java
我要回答