故障排除清单:
#1:根据您使用的数据库类型,您可能希望使用以下 SQL 命令查找列名及其各自的数据类型:
SELECT
table_name,
column_name,
data_type
FROM
information_schema.columns
WHERE
table_name = 'the-name-of-the-table-in-the-database';
预期结果将为您提供三列;尤其是“data_type”列。
确保您的 Pojo 类和相应的数据类型正确匹配。
注意:数据库里面的表中的bigint(数据类型)可以与Long无缝匹配。带 int 的整数。字符随 String 或主要的 java 类而变化,例如。一个存储枚举的类,等等。
确认上述后,进行下一步检查 -> 故障排除:
#2:此故障排除的主要检查是检查所有数据类型是否完全匹配。并注意传递给查询的参数。
传递枚举或任何其他数据类型或不符合 SQL 数据类型的枚举类型可能会触发“未映射”错误,即使 pojo 类与数据库中的表结构完全匹配。
pojo 示例:UserAccountBalance.class
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
@Builder//Lombok
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@NoArgsConstructor(access = AccessLevel.PUBLIC)
@Data//Lombok
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
@Schema
@Entity(name = "user_account_balance")
@Table(name = "user_account_balance")
public class UserAccountBalance {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private long id;
@NotNull
@Column(name = "username", nullable = false)
private String userName;
@NotNull
@Column(name="currency_code", nullable = false)
@Enumerated(EnumType.STRING)
private CurrencyCode currencyCode;
@NotNull
@Column(name = "balance", nullable = false)
private BigDecimal balance;
//Could be placed into and AuditModel class
@Column(name = "datecreated", nullable = false, updatable = false)
@JsonIgnore
@DateCreated
@CreationTimestamp
private LocalDateTime dateCreated;
@Column(name = "date_updated", nullable = false, updatable = false)
@JsonIgnore
@DateUpdated
private LocalDateTime dateUpdated;
@NotNull
@Column(name = "active")
@JsonIgnore
private int active;
@Column(name = "deleted")
@JsonIgnore
private int deleted;
}
存储库类:
//选项1:UserAccountBalanceRepository.class
@Repository
public abstract class UserAccountBalanceRepository implements CrudRepository<UserAccountBalance, Long> {
private final EntityManager entityManager;
public UserAccountBalanceRepository(@CurrentSession EntityManager entityManager){
this.entityManager = entityManager;
}
@Transactional(readOnly = true)
@Query(
value="SELECT uab.*" +
" FROM public.user_account_balance uab" +
" WHERE (currency_code =cast(:currencyCode AS text)" +
" AND userName =:userName" +
" AND active =:active)",
countQuery = "SELECT uab.*" +
" FROM public.user_account_balance uab" +
" WHERE (currency_code = cast(:currencyCode AS text)" +
" AND userName =:userName" +
" AND active =:active)",
nativeQuery = true
)
public abstract Optional<UserAccountBalance> findByUserAccountBalance_UserName_And_CurrencyCode(
String userName,
CurrencyCode currencyCode,
int active
);
}
//选项2:UserAccountBalanceRepository.class
@Repository
public abstract class UserAccountBalanceRepository implements CrudRepository<UserAccountBalance, Long> {
private final EntityManager entityManager;
public UserAccountBalanceRepository(@CurrentSession EntityManager entityManager){
this.entityManager = entityManager;
}
@Transactional(readOnly = true)
@Query(
value="SELECT uab.*" +
" FROM public.user_account_balance uab" +
" WHERE (currency_code =:currencyCode" +
" AND userName =:userName" +
" AND active =:active)",
countQuery = "SELECT uab.*" +
" FROM public.user_account_balance uab" +
" WHERE (currency_code = :currencyCode" +
" AND userName =:userName" +
" AND active =:active)",
nativeQuery = true
)
public abstract Optional<UserAccountBalance> findByUserAccountBalance_UserName_And_CurrencyCode(
String userName,
String currencyCode,/*this is what truly worked out for me perfectly*/
int active
);
}
#3。测试并再次测试。如果问题仍然存在,请耐心等待并再次查看所有变量和类。
#4。如果使用选项 #3 进行故障排除仍然没有帮助,请考虑散散步,稍作休息,并以全新的眼光从故障排除 #1 中重新审视它。
我希望这有帮助。欢呼与和平。