为了账号安全,请及时绑定邮箱和手机立即绑定

NullPointerException 导致:org.hibernate.Mapping

NullPointerException 导致:org.hibernate.Mapping

慕神8447489 2024-01-17 16:48:32
当我尝试运行本机查询时,当我尝试在 spring jpa 中运行时,出现以下错误native query。我已将方言添加到自定义方言中,扩展为spring.jpa.database-platform=com.config.HibernateCustomDialectCaused by: java.lang.NullPointerException: nullat 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 存储库@Repositorypublic 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);}
查看完整描述

1 回答

?
缥缈止盈

TA贡献2041条经验 获得超4个赞

在浏览互联网并扫描所有与休眠相关的页面后,我找到了一个解决方法。

我实现了一个CustomStringArrayType扩展com.vladmihalcea.hibernate.type.array.StringArrayType.

参考 - https://github.com/vladmihalcea/hibernate-types/issues/142我迫不及待地等待 PR 被合并,因此实现了如下解决方法:

public class CustomStringArrayType extends StringArrayType {


  @Override

  public void setParameterValues(Properties parameters) {


    if (parameters.containsKey(super.PARAMETER_TYPE)) {

        super.setParameterValues(parameters);


    }

  }

}

我修改后的HibernateDialect如下:


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, CustomStringArrayType.class.getName());

  }

}


查看完整回答
反对 回复 2024-01-17
  • 1 回答
  • 0 关注
  • 82 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信