Named Constructor RowMapper Implementation Library

Spring's RowMapper implementation for classes having their constructors annotated with JSR330 @Named annotation. Uses constructors for objects instantiation.

License

License

GroupId

GroupId

com.alexkasko.springjdbc
ArtifactId

ArtifactId

springjdbc-constructor-mapper
Last Version

Last Version

1.0.1
Release Date

Release Date

Type

Type

jar
Description

Description

Named Constructor RowMapper Implementation Library
Spring's RowMapper implementation for classes having their constructors annotated with JSR330 @Named annotation. Uses constructors for objects instantiation.
Project URL

Project URL

https://github.com/alexkasko/springjdbc-constructor-mapper
Source Code Management

Source Code Management

https://github.com/alexkasko/springjdbc-constructor-mapper

Download springjdbc-constructor-mapper

How to add to project

<!-- https://jarcasting.com/artifacts/com.alexkasko.springjdbc/springjdbc-constructor-mapper/ -->
<dependency>
    <groupId>com.alexkasko.springjdbc</groupId>
    <artifactId>springjdbc-constructor-mapper</artifactId>
    <version>1.0.1</version>
</dependency>
// https://jarcasting.com/artifacts/com.alexkasko.springjdbc/springjdbc-constructor-mapper/
implementation 'com.alexkasko.springjdbc:springjdbc-constructor-mapper:1.0.1'
// https://jarcasting.com/artifacts/com.alexkasko.springjdbc/springjdbc-constructor-mapper/
implementation ("com.alexkasko.springjdbc:springjdbc-constructor-mapper:1.0.1")
'com.alexkasko.springjdbc:springjdbc-constructor-mapper:jar:1.0.1'
<dependency org="com.alexkasko.springjdbc" name="springjdbc-constructor-mapper" rev="1.0.1">
  <artifact name="springjdbc-constructor-mapper" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.alexkasko.springjdbc', module='springjdbc-constructor-mapper', version='1.0.1')
)
libraryDependencies += "com.alexkasko.springjdbc" % "springjdbc-constructor-mapper" % "1.0.1"
[com.alexkasko.springjdbc/springjdbc-constructor-mapper "1.0.1"]

Dependencies

compile (2)

Group / Artifact Type Version
org.springframework : spring-jdbc jar 3.0.2.RELEASE
javax.inject : javax.inject jar 1

test (2)

Group / Artifact Type Version
junit : junit jar 4.8.2
com.h2database : h2 jar 1.3.154

Project Modules

There are no modules declared in this project.

Spring's RowMapper implementation using annotated constructors

Creates RowMappers for classes having their constructor arguments annotated with JSR330 @Named annotation. Uses constructors for objects instantiation. Supports class hierarchies using discriminator column. May be used as an alternative for BeanPropertyRowMapper

Library depends on spring-jdbc.

Library is available in Maven cental.

Javadocs for the latest release are available here.

Library usage

Maven dependency (available in central repository):

<dependency>
    <groupId>com.alexkasko.springjdbc</groupId>
    <artifactId>springjdbc-constructor-mapper</artifactId>
    <version>1.0.1</version>
</dependency>

Single class example:

// class with annotated constructor
private static class MyClass {
    private final String foo;
    private final int bar;

    private MyClass(@Named("foo") String foo, @Named("bar") Integer bar) {
        this.id = id;
        this.bar = null != bar ? bar : -1;
    }
}
// mapper creation
RowMapper<MyClass> mapper = NamedConstructorMapper.forClass(MyClass.class);
// standard row mapper usage
MyClass obj = jt.queryForObject("select * from my_table where id = 42", mapper);

Subclasses example:

// builder takes discriminator column name, subclasses are registered with their discriminators
RowMapper<Parent> mapper = NamedConstructorMapper.<Parent>builder("discriminator_column")
        .addSubclass("Foo", Foo.class)
        .addSubclass("Bar", Bar.class)
        .build();

####annotated constructors

To support constructor invocation with unordered row data constructor arguments must be named. There are different ways to access constructor names in runtime (see paranamer). This project uses JSR330 javax.inject.Named annotations, that must be applied to all constructor arguments. Constructors without @Named annotations on arguments will be ignored. Constructors with @Named annotations must have all they arguments annotated with not blank values without duplicates.

####choosing from multiple constrcutors

Constructor will be invoken only if row data contains columns for each constructor argument with the same names. Additional columns in row data will be ignored. If class has multiple annotated constructors, they will be checked against row data based on arguments list size in descending order.

####reflection introspection and caching

All reflection introspection is done on mapper instantiation. After that mapper instances hold references to constructors and their arguments names and invoke constructors for incoming row data. NamedConstructorMapper doesn't cache mapper creation: each NamedConstructorMapper.forClass call do reflection introspection. So it's better to create mappers for each class only once (using static final field or application level cache).

####subclasses mapping

NamedConstructorMapper supports subclasses mapping using discriminator column. Subclasses mapper chooses constructor by discriminator column value.

####columns case sensivity

All column names to @Named values comparisons are case-insensitive using Locale.ENGLISH, so all column names and @Named values must be locale insensitive.

License information

This project is released under the Apache License 2.0

Changelog

1.0.1 (2013-05-06)

  • static-import-friendly alias for single classes

1.0 (2012-11-11)

  • initial version

Versions

Version
1.0.1
1.0