Skip to content
This repository was archived by the owner on Jul 9, 2025. It is now read-only.

Commit dde0799

Browse files
committed
Add documentation for external domain-to-DB mapping.
1 parent d8a0b97 commit dde0799

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

docs/domain.rst

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,69 @@ specify a wildcard ``?`` as type arguments to the external domain class:
299299
}
300300
}
301301
302+
Direct mapping of external domain classes to the database
303+
---------------------------------------------------------
304+
305+
All external domain classes can be directly mapped to any database type.
306+
307+
Here's an example of mapping ``java.util.UUID`` to PostgreSQL's UUID type.
308+
309+
First, create an implementation of ``org.seasar.doma.jdbc.type.JdbcType`` to handle the mapping:
310+
311+
.. code-block:: java
312+
313+
class PostgresUUIDJdbcType extends AbstractJdbcType<UUID> {
314+
315+
protected PostgresUUIDJdbcType() {
316+
super(Types.OTHER);
317+
}
318+
319+
@Override
320+
protected UUID doGetValue(ResultSet resultSet, int index) throws SQLException {
321+
String value = resultSet.getString(index);
322+
return value == null ? null : UUID.fromString(value);
323+
}
324+
325+
@Override
326+
protected void doSetValue(PreparedStatement preparedStatement, int index, UUID value)
327+
throws SQLException {
328+
preparedStatement.setObject(index, value, type);
329+
}
330+
331+
@Override
332+
protected UUID doGetValue(CallableStatement callableStatement, int index) throws SQLException {
333+
String value = callableStatement.getString(index);
334+
return value == null ? null : UUID.fromString(value);
335+
}
336+
337+
@Override
338+
protected String doConvertToLogFormat(UUID value) {
339+
return value.toString();
340+
}
341+
}
342+
343+
Next, create a class that extends ``org.seasar.doma.it.domain.JdbcTypeProvider``,
344+
and in the ``getJdbcType`` method, return an instance of the ``JdbcType`` implementation created above.
345+
Don't forget to annotate the class with ``@ExternalDomain``:
346+
347+
.. code-block:: java
348+
349+
@ExternalDomain
350+
public class PostgresUUIDConverter extends JdbcTypeProvider<UUID> {
351+
352+
private static final PostgresUUIDJdbcType jdbcType = new PostgresUUIDJdbcType();
353+
354+
@Override
355+
public JdbcType<UUID> getJdbcType() {
356+
return jdbcType;
357+
}
358+
}
359+
360+
The rest follows the standard approach.
361+
Simply add ``PostgresUUIDConverter`` to the ``@DomainConverters`` annotation,
362+
and specify the fully qualified name of the class with the ``@DomainConverters`` annotation
363+
in the annotation processing options.
364+
302365
Example
303366
=======
304367

0 commit comments

Comments
 (0)