Skip to content

Commit 9e80505

Browse files
authored
Support overriding identity column values during inserts (#1301)
2 parents 1ee1034 + daf696f commit 9e80505

File tree

16 files changed

+855
-6
lines changed

16 files changed

+855
-6
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,19 @@ 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+
* <p>The type of {@code idValue} must be one of the basic types. It must never be a domain class
97+
* or an Optional type that wraps a basic type.
98+
*
99+
* @param idValue an identity value; may be null
100+
* @return true if the identity column is included, false otherwise
101+
*/
102+
default boolean includesIdentityColumn(Object idValue) {
103+
return includesIdentityColumn();
104+
}
105+
93106
/**
94107
* Whether this object supports the IDENTITY column.
95108
*

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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,25 @@ 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+
* <p>The type of {@code idValue} must be one of the basic types. It must never be a domain class
66+
* or an Optional type that wraps a basic type.
67+
*
68+
* @param config the configuration for identity generation
69+
* @param idValue an identity value; may be null
70+
* @return {@code true} if the identity column is included in the SQL INSERT statements, otherwise
71+
* {@code false}
72+
*/
73+
default boolean includesIdentityColumn(IdGenerationConfig config, Object idValue) {
74+
return includesIdentityColumn(config);
75+
}
76+
6077
/**
6178
* Generates the identity value before an insert.
6279
*

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,14 @@ 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(
146+
idGenerationConfig, property.getWrapper().get())) {
144147
targetPropertyTypes.add(propertyType);
145148
}
146149
if (generatedIdPropertyType == null) {
147-
Property<ENTITY, ?> property = propertyType.createProperty();
148-
property.load(currentEntity);
149150
if (property.getWrapper().get() == null) {
150151
throw new JdbcException(Message.DOMA2020, entityType.getName(), propertyType.getName());
151152
}

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

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

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

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

0 commit comments

Comments
 (0)