If all the fields belong to the same UDT, it's possible.
CREATE TYPE udt(a int, b int, c int);
CREATE TABLE t(id int PRIMARY KEY, u udt);
Here's an entity that maps to only two fields of the UDT:
@Entity
public class PartialView {
private int a;
private int b;
... // getters and setters
}
Using a query provider, you can have a DAO method that fetches a row by id, extracts the UDT and maps it:
@Dao
public interface DemoDao {
@QueryProvider(providerClass = FindByIdProvider.class, entityHelpers = PartialView.class)
PartialView findById(int id);
}
The provider implementation:
import static com.datastax.oss.driver.api.querybuilder.QueryBuilder.*;
public class FindByIdProvider {
private static final CqlIdentifier TABLE_ID = CqlIdentifier.fromCql("t");
private final CqlSession session;
private final EntityHelper<PartialView> helper;
private final PreparedStatement statement;
public FindByIdProvider(MapperContext context, EntityHelper<PartialView> helper) {
session = context.getSession();
// SELECT u FROM t WHERE id = ?
String query =
selectFrom(context.getKeyspaceId(), TABLE_ID)
.column("u")
.whereColumn("id").isEqualTo(bindMarker())
.asCql();
statement = session.prepare(query);
this.helper = helper;
}
public PartialView findById(int id) {
Row row = session.execute(statement.bind(id)).one();
UdtValue u;
if (row == null || (u = row.getUdtValue("u")) == null) {
return null;
} else {
return helper.get(u);
}
}
}
If you already have a row from a non-mapped query and you just need the mapping logic, you can define a GetEntity method on your DAO. This gives you direct access to
EntityHelper.get:
@GetEntity
PartialView asPartialView(UdtValue value);
If the fields come from different UDTs, or you have a mix of top-level columns and UDT fields, EntityHelper.get won't work. You could still write a query provider, but you would have to manually read the columns and build the entities, so you're not really taking advantage of the mapper anymore.