Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jdbc</artifactId>
<version>1.0.0.BUILD-SNAPSHOT</version>
<version>1.0.0.DATAJDBC-120-SNAPSHOT</version>

<name>Spring Data JDBC</name>
<description>Spring Data module for JDBC repositories.</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.springframework.data.jdbc.core.conversion.DbAction.Insert;
import org.springframework.data.jdbc.core.conversion.DbAction.Update;
import org.springframework.data.jdbc.core.conversion.Interpreter;
import org.springframework.data.jdbc.mapping.event.Identifier;
import org.springframework.data.jdbc.mapping.model.JdbcMappingContext;
import org.springframework.data.jdbc.mapping.model.JdbcPersistentEntity;

Expand Down Expand Up @@ -74,7 +73,7 @@ public <T> void interpret(Update<T> update) {
public <T> void interpret(Delete<T> delete) {

if (delete.getPropertyPath() == null) {
template.doDelete(Identifier.of(delete.getRootId()), Optional.ofNullable(delete.getEntity()),
template.doDelete(delete.getRootId(), Optional.ofNullable(delete.getEntity()),
delete.getEntityType());
} else {
template.doDelete(delete.getRootId(), delete.getPropertyPath());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,11 @@ public <T> T getParameterValue(Parameter<T, JdbcPersistentProperty> parameter) {
return null;
}

String column = prefix + name;
try {
return conversionService.convert(resultSet.getObject(prefix + name), parameter.getType().getType());
return conversionService.convert(resultSet.getObject(column), parameter.getType().getType());
} catch (SQLException o_O) {
throw new MappingException(String.format("Couldn't read column %s from ResultSet.", name), o_O);
throw new MappingException(String.format("Couldn't read column %s from ResultSet.", column), o_O);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public T mapRow(ResultSet resultSet, int i) throws SQLException {

T instance = delegate.mapRow(resultSet, i);

publisher.publishEvent(new AfterCreation(Identifier.of(entityInformation.getRequiredId(instance)), instance));
publisher.publishEvent(new AfterCreation(Identifier.of(entityInformation.getRequiredId(instance)), instance, null));

return instance;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,9 @@
import org.springframework.data.jdbc.core.conversion.JdbcEntityDeleteWriter;
import org.springframework.data.jdbc.core.conversion.JdbcEntityWriter;
import org.springframework.data.jdbc.mapping.event.AfterDelete;
import org.springframework.data.jdbc.mapping.event.AfterInsert;
import org.springframework.data.jdbc.mapping.event.AfterUpdate;
import org.springframework.data.jdbc.mapping.event.AfterSave;
import org.springframework.data.jdbc.mapping.event.BeforeDelete;
import org.springframework.data.jdbc.mapping.event.BeforeInsert;
import org.springframework.data.jdbc.mapping.event.BeforeUpdate;
import org.springframework.data.jdbc.mapping.event.BeforeSave;
import org.springframework.data.jdbc.mapping.event.Identifier;
import org.springframework.data.jdbc.mapping.event.Identifier.Specified;
import org.springframework.data.jdbc.mapping.model.BasicJdbcPersistentEntityInformation;
Expand Down Expand Up @@ -102,14 +100,29 @@ private static GenericConversionService getDefaultConversionService() {

@Override
public <T> void save(T instance, Class<T> domainType) {
createChange(instance).executeWith(interpreter);

JdbcPersistentEntityInformation<T, ?> entityInformation = context.getRequiredPersistentEntityInformation(domainType);

AggregateChange change = createChange(instance);

publisher.publishEvent(new BeforeSave( //
Identifier.ofNullable(entityInformation.getId(instance)), //
instance, //
change //
));

change.executeWith(interpreter);

publisher.publishEvent(new AfterSave( //
Identifier.of(entityInformation.getId(instance)), //
instance, //
change //
));
}

@Override
public <T> void insert(T instance, Class<T> domainType, Map<String, Object> additionalParameters) {

publisher.publishEvent(new BeforeInsert(instance));

KeyHolder holder = new GeneratedKeyHolder();
JdbcPersistentEntity<T> persistentEntity = getRequiredPersistentEntity(domainType);
JdbcPersistentEntityInformation<T, ?> entityInformation = context
Expand All @@ -132,23 +145,17 @@ public <T> void insert(T instance, Class<T> domainType, Map<String, Object> addi
throw new IllegalStateException(String.format(ENTITY_NEW_AFTER_INSERT, persistentEntity));
}

publisher.publishEvent(new AfterInsert(Identifier.of(entityInformation.getRequiredId(instance)), instance));

}

@Override
public <S> void update(S instance, Class<S> domainType) {

JdbcPersistentEntity<S> persistentEntity = getRequiredPersistentEntity(domainType);
JdbcPersistentEntityInformation<S, ?> entityInformation = context
.getRequiredPersistentEntityInformation(domainType);

Specified specifiedId = Identifier.of(entityInformation.getRequiredId(instance));
publisher.publishEvent(new BeforeUpdate(specifiedId, instance));
operations.update(sql(domainType).getUpdate(), getPropertyMap(instance, persistentEntity));
publisher.publishEvent(new AfterUpdate(specifiedId, instance));
}

@SuppressWarnings("ConstantConditions")
@Override
public long count(Class<?> domainType) {
return operations.getJdbcOperations().queryForObject(sql(domainType).getCount(), Long.class);
Expand Down Expand Up @@ -211,9 +218,21 @@ public void deleteAll(Class<?> domainType) {
change.executeWith(interpreter);
}

void doDelete(Object rootId, PropertyPath propertyPath) {
private void deleteTree(Object id, Object entity, Class<?> domainType) {

JdbcPersistentEntity<?> entityToDelete = context.getRequiredPersistentEntity(propertyPath.getTypeInformation());
AggregateChange change = createDeletingChange(id, entity, domainType);

Specified specifiedId = Identifier.of(id);
Optional<Object> optionalEntity = Optional.ofNullable(entity);
publisher.publishEvent(new BeforeDelete(specifiedId, optionalEntity, change));

change.executeWith(interpreter);

publisher.publishEvent(new AfterDelete(specifiedId, optionalEntity, change));

}

void doDelete(Object rootId, PropertyPath propertyPath) {

JdbcPersistentEntity<?> rootEntity = context.getRequiredPersistentEntity(propertyPath.getOwningType());

Expand All @@ -228,23 +247,12 @@ void doDelete(Object rootId, PropertyPath propertyPath) {

}

void doDelete(Specified specifiedId, Optional<Object> optionalEntity, Class<?> domainType) {

publisher.publishEvent(new BeforeDelete(specifiedId, optionalEntity));
void doDelete(Object id, Optional<Object> optionalEntity, Class<?> domainType) {

String deleteByIdSql = sql(domainType).getDeleteById();
MapSqlParameterSource parameter = createIdParameterSource(specifiedId.getValue(), domainType);
MapSqlParameterSource parameter = createIdParameterSource(id, domainType);

operations.update(deleteByIdSql, parameter);

publisher.publishEvent(new AfterDelete(specifiedId, optionalEntity));
}

private void deleteTree(Object id, Object entity, Class<?> domainType) {

AggregateChange change = createDeletingChange(id, entity, domainType);

change.executeWith(interpreter);
}

private <T> AggregateChange createChange(T instance) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.springframework.data.jdbc.mapping.event;

import org.springframework.data.jdbc.core.conversion.AggregateChange;
import org.springframework.data.jdbc.mapping.event.Identifier.Specified;

/**
Expand All @@ -31,8 +32,9 @@ public class AfterCreation extends JdbcEventWithIdAndEntity {
/**
* @param id of the entity
* @param entity the newly instantiated entity.
* @param change
*/
public AfterCreation(Specified id, Object entity) {
super(id, entity);
public AfterCreation(Specified id, Object entity, AggregateChange change) {
super(id, entity, change);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.util.Optional;

import org.springframework.data.jdbc.core.conversion.AggregateChange;
import org.springframework.data.jdbc.mapping.event.Identifier.Specified;

/**
Expand All @@ -33,8 +34,9 @@ public class AfterDelete extends JdbcEventWithId {
/**
* @param id of the entity.
* @param instance the deleted entity if it is available.
* @param change the {@link AggregateChange} encoding the planned actions to be performed on the database.
*/
public AfterDelete(Specified id, Optional<Object> instance) {
super(id, instance);
public AfterDelete(Specified id, Optional<Object> instance, AggregateChange change) {
super(id, instance, change);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.springframework.data.jdbc.mapping.event;

import org.springframework.data.jdbc.core.conversion.AggregateChange;
import org.springframework.data.jdbc.mapping.event.Identifier.Specified;

/**
Expand All @@ -30,8 +31,9 @@ public class AfterSave extends JdbcEventWithIdAndEntity {
/**
* @param id identifier of
* @param instance the newly saved entity.
* @param change the {@link AggregateChange} encoding the planned actions to be performed on the database.
*/
AfterSave(Specified id, Object instance) {
super(id, instance);
public AfterSave(Specified id, Object instance, AggregateChange change) {
super(id, instance, change);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.util.Optional;

import org.springframework.data.jdbc.core.conversion.AggregateChange;
import org.springframework.data.jdbc.mapping.event.Identifier.Specified;

/**
Expand All @@ -32,8 +33,9 @@ public class BeforeDelete extends JdbcEventWithId {
/**
* @param id the id of the entity
* @param entity the entity about to get deleted. Might be empty.
* @param change the {@link AggregateChange} encoding the planned actions to be performed on the database.
*/
public <T> BeforeDelete(Specified id, Optional<Object> entity) {
super(id, entity);
public <T> BeforeDelete(Specified id, Optional<Object> entity, AggregateChange change) {
super(id, entity, change);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package org.springframework.data.jdbc.mapping.event;

import org.springframework.data.jdbc.core.conversion.AggregateChange;

/**
* Subclasses of this get published before an entity gets saved to the database.
*
Expand All @@ -28,8 +30,9 @@ public class BeforeSave extends JdbcEventWithEntity {
/**
* @param id of the entity to be saved.
* @param instance the entity about to get saved.
* @param change
*/
BeforeSave(Identifier id, Object instance) {
super(id, instance);
public BeforeSave(Identifier id, Object instance, AggregateChange change) {
super(id, instance, change);
}
}
Loading