Skip to content

Commit 5461159

Browse files
authored
Support overriding identity column values during inserts (#1297)
2 parents 10c3620 + a3d5500 commit 5461159

File tree

14 files changed

+585
-7
lines changed

14 files changed

+585
-7
lines changed

doma-core/src/main/java/org/seasar/doma/internal/Artifact.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public final class Artifact {
2222

2323
private static final String NAME = "Doma";
2424

25-
private static final String VERSION = "3.3.0";
25+
private static final String VERSION = "3.3.1-SNAPSHOT";
2626

2727
public static String getName() {
2828
return NAME;

doma-core/src/main/java/org/seasar/doma/jdbc/dialect/Dialect.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,16 @@ public interface Dialect {
9090
*/
9191
boolean includesIdentityColumn();
9292

93+
/**
94+
* Determines if the identity column is included based on the provided identifier value.
95+
*
96+
* @param idValue an identity value; may be null
97+
* @return true if the identity column is included, false otherwise
98+
*/
99+
default boolean includesIdentityColumn(Object idValue) {
100+
return includesIdentityColumn();
101+
}
102+
93103
/**
94104
* Whether this object supports the IDENTITY column.
95105
*

doma-core/src/main/java/org/seasar/doma/jdbc/entity/GeneratedIdPropertyType.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,15 @@ protected boolean isGenerationTypeSupported(GenerationType generationType, Diale
8484
}
8585
}
8686

87+
@Deprecated
8788
public boolean isIncluded(IdGenerationConfig config) {
8889
return idGenerator.includesIdentityColumn(config);
8990
}
9091

92+
public boolean isIncluded(IdGenerationConfig config, Object idValue) {
93+
return idGenerator.includesIdentityColumn(config, idValue);
94+
}
95+
9196
public boolean isBatchSupported(IdGenerationConfig config) {
9297
return idGenerator.supportsBatch(config);
9398
}

doma-core/src/main/java/org/seasar/doma/jdbc/id/AbstractPreGenerateIdGenerator.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,17 @@ public boolean supportsAutoGeneratedKeys(IdGenerationConfig config) {
6060
return false;
6161
}
6262

63+
@SuppressWarnings("deprecation")
6364
@Override
6465
public boolean includesIdentityColumn(IdGenerationConfig config) {
6566
return true;
6667
}
6768

69+
@Override
70+
public boolean includesIdentityColumn(IdGenerationConfig config, Object idValue) {
71+
return true;
72+
}
73+
6874
@Override
6975
public Long generatePreInsert(IdGenerationConfig config) {
7076
IdContext idContext = getIdContext(config);

doma-core/src/main/java/org/seasar/doma/jdbc/id/BuiltinIdentityIdGenerator.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,17 @@ public boolean supportsBatch(IdGenerationConfig config) {
3737
return config.getDialect().supportsBatchExecutionReturningGeneratedValues();
3838
}
3939

40+
@SuppressWarnings("deprecation")
4041
@Override
4142
public boolean includesIdentityColumn(IdGenerationConfig config) {
4243
return config.getDialect().includesIdentityColumn();
4344
}
4445

46+
@Override
47+
public boolean includesIdentityColumn(IdGenerationConfig config, Object idValue) {
48+
return config.getDialect().includesIdentityColumn(idValue);
49+
}
50+
4551
@Override
4652
public boolean supportsAutoGeneratedKeys(IdGenerationConfig config) {
4753
return config.getDialect().supportsAutoGeneratedKeys();

doma-core/src/main/java/org/seasar/doma/jdbc/id/IdGenerator.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,22 @@ public interface IdGenerator {
5555
* @param config the configuration
5656
* @return {@code true} if supported
5757
*/
58+
@Deprecated
5859
boolean includesIdentityColumn(IdGenerationConfig config);
5960

61+
/**
62+
* Determines whether the identity column is included in the SQL INSERT statements based on the
63+
* given configuration and identity value.
64+
*
65+
* @param config the configuration for identity generation
66+
* @param idValue an identity value; may be null
67+
* @return {@code true} if the identity column is included in the SQL INSERT statements, otherwise
68+
* {@code false}
69+
*/
70+
default boolean includesIdentityColumn(IdGenerationConfig config, Object idValue) {
71+
return includesIdentityColumn(config);
72+
}
73+
6074
/**
6175
* Generates the identity value before an insert.
6276
*

doma-core/src/main/java/org/seasar/doma/jdbc/query/AutoBatchInsertQuery.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,13 @@ protected void prepareTargetPropertyTypes() {
139139
continue;
140140
}
141141
if (propertyType.isId()) {
142+
Property<ENTITY, ?> property = propertyType.createProperty();
143+
property.load(currentEntity);
142144
if (propertyType != generatedIdPropertyType
143-
|| generatedIdPropertyType.isIncluded(idGenerationConfig)) {
145+
|| generatedIdPropertyType.isIncluded(idGenerationConfig, property.get())) {
144146
targetPropertyTypes.add(propertyType);
145147
}
146148
if (generatedIdPropertyType == null) {
147-
Property<ENTITY, ?> property = propertyType.createProperty();
148-
property.load(currentEntity);
149149
if (property.getWrapper().get() == null) {
150150
throw new JdbcException(Message.DOMA2020, entityType.getName(), propertyType.getName());
151151
}

doma-core/src/main/java/org/seasar/doma/jdbc/query/AutoInsertQuery.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ protected void prepareTargetPropertyType() {
102102
property.load(entity);
103103
if (propertyType.isId()) {
104104
if (propertyType != generatedIdPropertyType
105-
|| generatedIdPropertyType.isIncluded(idGenerationConfig)) {
105+
|| generatedIdPropertyType.isIncluded(idGenerationConfig, property.get())) {
106106
targetPropertyTypes.add(propertyType);
107107
}
108108
if (generatedIdPropertyType == null && property.getWrapper().get() == null) {

doma-core/src/main/java/org/seasar/doma/jdbc/query/AutoMultiInsertQuery.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ protected void prepareTargetPropertyType() {
124124
property.load(entity);
125125
if (propertyType.isId()) {
126126
if (propertyType != generatedIdPropertyType
127-
|| generatedIdPropertyType.isIncluded(idGenerationConfig)) {
127+
|| generatedIdPropertyType.isIncluded(idGenerationConfig, property.get())) {
128128
targetPropertyTypes.add(propertyType);
129129
}
130130
if (generatedIdPropertyType == null && property.getWrapper().get() == null) {

0 commit comments

Comments
 (0)