If you’re working with Apache Cassandra and Spring Data Cassandra, you may come across the following error:
Target bean of type com.datastax.oss.driver.internal.core.data.DefaultUdtValue is not of type of the persistent entity
At first glance it looks confusing, but this error usually points to a mismatch between how User-Defined Types (UDTs) are declared in Cassandra and how they’re mapped in your Java application.
In this article we’ll explain what causes this issue and how you can fix it.
What the Error Means
Cassandra allows you to define User Defined Types (UDTs) to store structured data.
In Spring Data Cassandra, each UDT should be mapped to a Java class annotated with @UserDefinedType
.
The error happens when the driver returns an internal object (DefaultUdtValue
) but Spring Data expected your mapped entity class. In other words, there’s a type mismatch between the database schema and your code.
Common Causes
- Missing or wrong
@UserDefinedType
annotation
Your Java class must have the correct annotation and name matching the Cassandra UDT. - Wrong column type in the entity
If your entity field isDefaultUdtValue
but should be your mapped class, Spring Data cannot convert it. - Out-of-sync schema
The Cassandra schema changed but your entity classes weren’t updated accordingly. - Incompatible driver or Spring Data versions
Using different versions can lead to type conversion problems.
How to Fix It
1. Create a Proper UDT Class
import org.springframework.data.cassandra.core.mapping.UserDefinedType;
@UserDefinedType("address")
public class Address {
private String street;
private String city;
private String zip;
// getters and setters
}
2. Reference It in Your Entity
@Table("customers")
public class Customer {
@PrimaryKey private String id;
private Address address; // Spring maps this automatically
}
3. Double-Check Naming
Make sure the UDT name (address
) in the annotation matches the one defined in Cassandra.
4. Clean and Rebuild
Run mvn clean install
or your build tool to refresh the compiled classes.
5. Update Your Dependencies
Use compatible versions of:
spring-data-cassandra
cassandra-driver-core
SEO Keywords
- Spring Data Cassandra UDT error
- DefaultUdtValue not of type persistent entity
- Fix UserDefinedType mismatch Cassandra
- Spring Data Cassandra mapping example
Conclusion
This error is common when working with Cassandra UDTs in Java. By ensuring your Java classes are properly annotated with @UserDefinedType
and that the Cassandra schema matches your entity definitions, you can resolve it quickly.
Understanding how Spring Data Cassandra maps UDTs to Java objects will help you prevent similar issues in future projects.