Skip to content

Commit b823403

Browse files
committed
1 parent 709aa57 commit b823403

File tree

3 files changed

+82
-24
lines changed

3 files changed

+82
-24
lines changed

project/jimmer-sql-kotlin/src/main/kotlin/org/babyfish/jimmer/sql/kt/KSqlClient.kt

+33-21
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ import org.babyfish.jimmer.sql.kt.cfg.KSqlClientDsl
1717
import org.babyfish.jimmer.sql.kt.filter.KFilterDsl
1818
import org.babyfish.jimmer.sql.kt.filter.KFilters
1919
import org.babyfish.jimmer.sql.kt.impl.KSqlClientImpl
20+
import org.babyfish.jimmer.sql.runtime.ConnectionManager
2021
import org.babyfish.jimmer.sql.runtime.EntityManager
2122
import org.babyfish.jimmer.sql.runtime.Executor
2223
import org.babyfish.jimmer.sql.runtime.JSqlClientImplementor
24+
import org.babyfish.jimmer.sql.transaction.AbstractTxConnectionManager
2325
import org.babyfish.jimmer.sql.transaction.Propagation
2426
import java.sql.Connection
2527
import kotlin.reflect.KClass
@@ -79,27 +81,18 @@ interface KSqlClient : KSaver {
7981
val triggers: KTriggers
8082

8183
/**
82-
* <ul>
83-
* <li>
84-
* If trigger type is 'BINLOG_ONLY'
85-
* <ul>
86-
* <li>If `transaction` is true, throws exception</li>
87-
* <li>If `transaction` is false, return binlog trigger</li>
88-
* </ul>
89-
* </li>
90-
* <li>
91-
* If trigger type is 'TRANSACTION_ONLY', returns transaction trigger
92-
* no matter what the `transaction` is
93-
* </li>
94-
* <li>
95-
* If trigger type is 'BOTH'
96-
* <ul>
97-
* <li>If `transaction` is true, return transaction trigger</li>
98-
* <li>If `transaction` is false, return binlog trigger</li>
99-
* </ul>
100-
* Note that the objects returned by different parameters are independent of each other.
101-
* </li>
102-
* </ul>
84+
* - If trigger type is 'BINLOG_ONLY'
85+
* - If `transaction` is true, throws exception
86+
* - If `transaction` is false, return binlog trigger
87+
*
88+
* - If trigger type is 'TRANSACTION_ONLY',
89+
* returns transaction trigger no matter what the `transaction` is
90+
*
91+
* - If trigger type is 'BOTH'
92+
* - If `transaction` is true, return transaction trigger
93+
* - If `transaction` is false, return binlog trigger
94+
* > Note that the objects returned by different parameters are independent of each other.
95+
*
10396
* @param transaction
10497
* @return Trigger
10598
*/
@@ -253,6 +246,25 @@ interface KSqlClient : KSaver {
253246
fun <E : Any> deleteByIds(type: KClass<E>, ids: Iterable<*>, block: KDeleteCommandDsl.() -> Unit): KDeleteResult =
254247
entities.deleteAll(type, ids, block = block)
255248

249+
/**
250+
* Execute a transaction by the specified [Propagation] behavior
251+
*
252+
* - If an IOC framework is used, its implementation
253+
* should be an encapsulation of the transaction management
254+
* within the IOC framework. Taking `jimmer-spring-starter`
255+
* as an example, it is the `SpringConnectionManager`
256+
* which will be created and enabled automatically.
257+
*
258+
* - If no IOC framework is used, the class
259+
* [AbstractTxConnectionManager] is the
260+
* lightweight implementation provided by jimmer,
261+
* please specify the connection manager of sqlClient
262+
* by [ConnectionManager.simpleConnectionManager]
263+
*
264+
* @param propagation The propagation behavior
265+
* @param block The action to be executed in transaction
266+
* @return The result of transaction
267+
*/
256268
fun <R> transaction(propagation: Propagation = Propagation.REQUIRED, block: () -> R): R
257269

258270
val javaClient: JSqlClientImplementor

project/jimmer-sql/src/main/java/org/babyfish/jimmer/sql/JSqlClient.java

+44
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@
2727
import org.babyfish.jimmer.sql.meta.IdGenerator;
2828
import org.babyfish.jimmer.sql.meta.MetaStringResolver;
2929
import org.babyfish.jimmer.sql.runtime.*;
30+
import org.babyfish.jimmer.sql.transaction.AbstractTxConnectionManager;
3031
import org.babyfish.jimmer.sql.transaction.Propagation;
3132
import org.jetbrains.annotations.NotNull;
3233
import org.jetbrains.annotations.Nullable;
3334

35+
import javax.sql.DataSource;
3436
import java.time.ZoneId;
3537
import java.util.Collection;
3638
import java.util.List;
@@ -193,10 +195,52 @@ default DeleteResult deleteByIds(Class<?> type, Iterable<?> ids) {
193195
return getEntities().deleteAll(type, ids, DeleteMode.AUTO);
194196
}
195197

198+
/**
199+
* Execute a transaction by the default propagation behavior
200+
* {@link Propagation#REQUIRED}
201+
*
202+
* <ul>
203+
* <li>If an IOC framework is used, its implementation
204+
* should be an encapsulation of the transaction management
205+
* within the IOC framework. Taking {@code jimmer-spring-starter}
206+
* as an example, it is the {@code SpringConnectionManager}
207+
* which will be created and enabled automatically.</li>
208+
*
209+
* <li>If no IOC framework is used, the class
210+
* {@link AbstractTxConnectionManager} is the
211+
* lightweight implementation provided by jimmer,
212+
* please specify the connection manager of sqlClient by
213+
* {@link ConnectionManager#simpleConnectionManager(DataSource)}</li>
214+
* </ul>
215+
*
216+
* @param block The action to be executed in transaction
217+
* @return The result of transaction
218+
*/
196219
default <R> R transaction(Supplier<R> block) {
197220
return transaction(Propagation.REQUIRED, block);
198221
}
199222

223+
/**
224+
* Execute a transaction by the specified {@link Propagation} behavior
225+
*
226+
* <ul>
227+
* <li>If an IOC framework is used, its implementation
228+
* should be an encapsulation of the transaction management
229+
* within the IOC framework. Taking {@code jimmer-spring-starter}
230+
* as an example, it is the {@code SpringConnectionManager}
231+
* which will be created and enabled automatically.</li>
232+
*
233+
* <li>If no IOC framework is used, the class
234+
* {@link AbstractTxConnectionManager} is the
235+
* lightweight implementation provided by jimmer,
236+
* please specify the connection manager of sqlClient by
237+
* {@link ConnectionManager#simpleConnectionManager(DataSource)}</li>
238+
* </ul>
239+
*
240+
* @param propagation The propagation behavior
241+
* @param block The action to be executed in transaction
242+
* @return The result of transaction
243+
*/
200244
<R> R transaction(Propagation propagation, Supplier<R> block);
201245

202246
interface Builder {

project/jimmer-sql/src/main/java/org/babyfish/jimmer/sql/ast/impl/mutation/EntityInvestigator.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -288,9 +288,11 @@ private Exception translateAll() {
288288
if (associatedObject == null) {
289289
continue;
290290
}
291-
Object associatedId = ((ImmutableSpi)associatedObject).__get(
292-
prop.getTargetType().getIdProp().getId()
293-
);
291+
PropId targetIdPropId = prop.getTargetType().getIdProp().getId();
292+
if (!((ImmutableSpi)associatedObject).__isLoaded(targetIdPropId)) {
293+
continue;
294+
}
295+
Object associatedId = ((ImmutableSpi)associatedObject).__get(targetIdPropId);
294296
targetIdMultiMap
295297
.computeIfAbsent(prop, it -> new LinkedHashSet<>())
296298
.add(associatedId);

0 commit comments

Comments
 (0)