gpt4 book ai didi

java - NullPointerException 由 : org. hibernate.MappingException : Could not instantiate Type: com. vladmihalcea.hibernate.type.array.StringArrayType 引起

转载 作者:行者123 更新时间:2023-12-01 22:13:03 39 4
gpt4 key购买 nike

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

的自定义方言中

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);


}

还有我的 DTO:

public class SessionSummaryView {
private Long sessionId;
private String[] tags;
private Long oid;


}

StringTypeArray 是以下库中的实现。

 <dependency>
<groupId>com.vladmihalcea</groupId>
<artifactId>hibernate-types-52</artifactId>
<version>2.6.1</version>
</dependency>

最佳答案

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

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

引用 - 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());
}
}

关于java - NullPointerException 由 : org. hibernate.MappingException : Could not instantiate Type: com. vladmihalcea.hibernate.type.array.StringArrayType 引起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58644346/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com