Spring JPA Repository转换器未找到问题求助(附实体类代码)
Hey there, let's figure out how to fix that "Converter not found" error you're running into with Spring JPA Repository. First off, I noticed your UserModel code cuts off right at u_mobile_numb—that’s almost certainly where the issue originates! Let’s walk through the most common causes and their fixes:
1. You’re using a custom data type without a JPA Converter
If your u_mobile_numb field uses a custom class (like a PhoneNumber object instead of a plain String), JPA has no built-in way to map it to a database column type.
Fix: Build a JPA Attribute Converter
Suppose your mobile number is stored as a custom PhoneNumber class. You’ll need a converter to handle translating between your custom type and a database-compatible type (like String):
import jakarta.persistence.AttributeConverter; import jakarta.persistence.Converter; @Converter(autoApply = true) // Automatically applies this converter to all PhoneNumber fields public class PhoneNumberConverter implements AttributeConverter<PhoneNumber, String> { @Override public String convertToDatabaseColumn(PhoneNumber phoneNumber) { if (phoneNumber == null) { return null; } // Convert your PhoneNumber object to a storable string (adjust logic to match your class) return phoneNumber.getCountryCode() + "-" + phoneNumber.getLocalNumber(); } @Override public PhoneNumber convertToEntityAttribute(String dbValue) { if (dbValue == null || dbValue.isBlank()) { return null; } // Parse the database string back into your PhoneNumber object String[] parts = dbValue.split("-"); return new PhoneNumber(parts[0], parts[1]); } }
Then update your UserModel to use the custom type (and make sure the PhoneNumber class is properly defined):
@Column(name = "u_mobile_number") private PhoneNumber mobileNumber;
2. Mismatched data types between entity field and database column
If you’re using a standard type but the database column’s type doesn’t align (e.g., your entity uses Integer but the DB column is VARCHAR), JPA might throw this converter error.
Fix: Align your entity field type with the database column
Double-check the u_mobile_numb column in your database:
- If it’s a string type (like
VARCHAR), useStringin your entity. - If it’s a numeric type (like
BIGINT), useLongorInteger.
3. Missing or incorrect JPA annotations
Typos in column names or missing annotations can sometimes lead to unexpected converter issues (even if the primary error message points to a converter).
Fix: Verify annotations and column name accuracy
Make sure your field’s @Column annotation matches the exact column name in your database:
@Column(name = "u_mobile_number") // Match this to your actual DB column name private String mobileNumber;
4. You’re using an enum without proper mapping
If u_mobile_numb is an enum (e.g., a mobile number type like HOME, WORK), JPA needs explicit instructions on how to map it to the database.
Fix: Use the @Enumerated annotation
@Enumerated(EnumType.STRING) // Stores the enum's name as a VARCHAR in the database @Column(name = "u_mobile_type") // Example enum field private MobileNumberType mobileType;
Since your code cuts off at u_mobile_numb, sharing the full definition of that field would help pinpoint the exact problem. But these fixes cover the majority of "Converter not found" scenarios in Spring JPA, so they’re a great starting point.
内容的提问来源于stack exchange,提问作者Naanavanalla

