Browse Source
Events now use the id as source. The id is encapsulated in a value object to support null ids in BeforeInsert events. Entity references in events are now Optionals. Dropped redundant Event suffix from events. Added "@since 2.0" to classes. Replaced constructors and getters with Lombok annotations. Simplified pom.xml. Extracted interface JdbcPersistentEntity and JdbcPersistentProperty. Integration tests use the Spring Test framework. Fixed test for entities with primitive id type. Original pull request: #5.pull/6/merge
39 changed files with 924 additions and 584 deletions
@ -0,0 +1,2 @@ |
|||||||
|
lombok.nonNull.exceptionType = IllegalArgumentException |
||||||
|
lombok.log.fieldName = LOG |
||||||
@ -0,0 +1,35 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2017 the original author or authors. |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package org.springframework.data.jdbc.mapping.event; |
||||||
|
|
||||||
|
import org.springframework.data.jdbc.mapping.event.Identifier.Specified; |
||||||
|
|
||||||
|
/** |
||||||
|
* Gets published after instantiation and setting of all the properties of an entity. This allows to do some postprocessing of entities. |
||||||
|
* |
||||||
|
* @author Jens Schauder |
||||||
|
* @since 2.0 |
||||||
|
*/ |
||||||
|
public class AfterCreation extends JdbcEventWithIdAndEntity { |
||||||
|
|
||||||
|
/** |
||||||
|
* @param id of the entity |
||||||
|
* @param entity the newly instantiated entity. |
||||||
|
*/ |
||||||
|
public AfterCreation(Specified id, Object entity) { |
||||||
|
super(id, entity); |
||||||
|
} |
||||||
|
} |
||||||
@ -1,36 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright 2017 the original author or authors. |
|
||||||
* |
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
||||||
* you may not use this file except in compliance with the License. |
|
||||||
* You may obtain a copy of the License at |
|
||||||
* |
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
* |
|
||||||
* Unless required by applicable law or agreed to in writing, software |
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS, |
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||||
* See the License for the specific language governing permissions and |
|
||||||
* limitations under the License. |
|
||||||
*/ |
|
||||||
package org.springframework.data.jdbc.mapping.event; |
|
||||||
|
|
||||||
import java.util.function.Function; |
|
||||||
|
|
||||||
/** |
|
||||||
* gets published after instantiation and setting of all the properties of an entity. This allows to do some |
|
||||||
* postprocessing of entities. |
|
||||||
* |
|
||||||
* @author Jens Schauder |
|
||||||
*/ |
|
||||||
public class AfterCreationEvent extends JdbcEvent{ |
|
||||||
|
|
||||||
/** |
|
||||||
* @param instance the newly instantiated entity. |
|
||||||
* @param idProvider a function providing the id, for the instance. |
|
||||||
* @param <T> type of the entity and the argument of the {@code idProvider} |
|
||||||
*/ |
|
||||||
public <T> AfterCreationEvent(T instance, Function<T, Object> idProvider) { |
|
||||||
super(instance, idProvider); |
|
||||||
} |
|
||||||
} |
|
||||||
@ -0,0 +1,39 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2017 the original author or authors. |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package org.springframework.data.jdbc.mapping.event; |
||||||
|
|
||||||
|
import java.util.Optional; |
||||||
|
|
||||||
|
import org.springframework.data.jdbc.mapping.event.Identifier.Specified; |
||||||
|
|
||||||
|
/** |
||||||
|
* Gets published after deletion of an entity. It will have a {@link Specified} identifier. |
||||||
|
* |
||||||
|
* If the entity is empty or not depends on the delete method used. |
||||||
|
* |
||||||
|
* @author Jens Schauder |
||||||
|
* @since 2.0 |
||||||
|
*/ |
||||||
|
public class AfterDelete extends JdbcEventWithId{ |
||||||
|
|
||||||
|
/** |
||||||
|
* @param id of the entity. |
||||||
|
* @param instance the deleted entity if it is available. |
||||||
|
*/ |
||||||
|
public AfterDelete(Specified id, Optional<Object> instance) { |
||||||
|
super(id, instance); |
||||||
|
} |
||||||
|
} |
||||||
@ -1,36 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright 2017 the original author or authors. |
|
||||||
* |
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
||||||
* you may not use this file except in compliance with the License. |
|
||||||
* You may obtain a copy of the License at |
|
||||||
* |
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
* |
|
||||||
* Unless required by applicable law or agreed to in writing, software |
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS, |
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||||
* See the License for the specific language governing permissions and |
|
||||||
* limitations under the License. |
|
||||||
*/ |
|
||||||
package org.springframework.data.jdbc.mapping.event; |
|
||||||
|
|
||||||
import java.util.function.Function; |
|
||||||
|
|
||||||
/** |
|
||||||
* get published after deletion of an entity. The source might contain the Id or the actual entity, depending on the |
|
||||||
* {@code delete(...)} method used. |
|
||||||
* |
|
||||||
* @author Jens Schauder |
|
||||||
*/ |
|
||||||
public class AfterDeleteEvent extends JdbcEvent{ |
|
||||||
|
|
||||||
/** |
|
||||||
* @param instance the deleted entity. |
|
||||||
* @param idProvider a function providing the id, for the instance. |
|
||||||
* @param <T> type of the entity and the argument of the {@code idProvider} |
|
||||||
*/ |
|
||||||
public <T> AfterDeleteEvent(T instance, Function<T, Object> idProvider) { |
|
||||||
super(instance, idProvider); |
|
||||||
} |
|
||||||
} |
|
||||||
@ -0,0 +1,37 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2017 the original author or authors. |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package org.springframework.data.jdbc.mapping.event; |
||||||
|
|
||||||
|
import java.util.Optional; |
||||||
|
|
||||||
|
import org.springframework.data.jdbc.mapping.event.Identifier.Specified; |
||||||
|
|
||||||
|
/** |
||||||
|
* Gets published when an entity is about to get deleted. |
||||||
|
* |
||||||
|
* @author Jens Schauder |
||||||
|
* @since 2.0 |
||||||
|
*/ |
||||||
|
public class BeforeDelete extends JdbcEventWithId { |
||||||
|
|
||||||
|
/** |
||||||
|
* @param id the id of the entity |
||||||
|
* @param entity the entity about to get deleted. Might be empty. |
||||||
|
*/ |
||||||
|
public <T> BeforeDelete(Specified id, Optional<Object> entity) { |
||||||
|
super(id, entity); |
||||||
|
} |
||||||
|
} |
||||||
@ -1,38 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright 2017 the original author or authors. |
|
||||||
* |
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
||||||
* you may not use this file except in compliance with the License. |
|
||||||
* You may obtain a copy of the License at |
|
||||||
* |
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
* |
|
||||||
* Unless required by applicable law or agreed to in writing, software |
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS, |
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||||
* See the License for the specific language governing permissions and |
|
||||||
* limitations under the License. |
|
||||||
*/ |
|
||||||
package org.springframework.data.jdbc.mapping.event; |
|
||||||
|
|
||||||
import java.util.function.Function; |
|
||||||
|
|
||||||
import org.springframework.context.ApplicationEvent; |
|
||||||
|
|
||||||
/** |
|
||||||
* gets published when an entity is about to get deleted. {@link ApplicationEvent#getSource()} might contain either the |
|
||||||
* entity or the id of the entity, depending on which delete method was used. |
|
||||||
* |
|
||||||
* @author Jens Schauder |
|
||||||
*/ |
|
||||||
public class BeforeDeleteEvent extends JdbcEvent { |
|
||||||
|
|
||||||
/** |
|
||||||
* @param instance the entity about to get deleted. Might be {@literal NULL} |
|
||||||
* @param idProvider a function providing the id, for the instance. Must provide a not {@literal NULL} id, when called with {@link #instance} |
|
||||||
* @param <T> type of the entity and the argument of the {@code idProvider} |
|
||||||
*/ |
|
||||||
public <T> BeforeDeleteEvent(T instance, Function<T, Object> idProvider) { |
|
||||||
super(instance, idProvider); |
|
||||||
} |
|
||||||
} |
|
||||||
@ -1,37 +0,0 @@ |
|||||||
/* |
|
||||||
* Copyright 2017 the original author or authors. |
|
||||||
* |
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
||||||
* you may not use this file except in compliance with the License. |
|
||||||
* You may obtain a copy of the License at |
|
||||||
* |
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
* |
|
||||||
* Unless required by applicable law or agreed to in writing, software |
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS, |
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||||
* See the License for the specific language governing permissions and |
|
||||||
* limitations under the License. |
|
||||||
*/ |
|
||||||
package org.springframework.data.jdbc.mapping.event; |
|
||||||
|
|
||||||
import java.util.function.Function; |
|
||||||
|
|
||||||
import org.springframework.context.ApplicationEvent; |
|
||||||
|
|
||||||
/** |
|
||||||
* subclasses of this get published before an entity gets saved to the database. |
|
||||||
* |
|
||||||
* @author Jens Schauder |
|
||||||
*/ |
|
||||||
public class BeforeSaveEvent extends JdbcEvent { |
|
||||||
|
|
||||||
/** |
|
||||||
* @param instance the entity about to get saved. |
|
||||||
* @param idProvider a function providing the id, for the instance. |
|
||||||
* @param <T> type of the entity and the argument of the {@code idProvider} |
|
||||||
*/ |
|
||||||
<T> BeforeSaveEvent(T instance, Function<T, Object> idProvider) { |
|
||||||
super(instance, idProvider); |
|
||||||
} |
|
||||||
} |
|
||||||
@ -0,0 +1,35 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2017 the original author or authors. |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package org.springframework.data.jdbc.mapping.event; |
||||||
|
|
||||||
|
import org.springframework.data.jdbc.mapping.event.Identifier.Specified; |
||||||
|
|
||||||
|
/** |
||||||
|
* Gets published before an entity gets updated in the database. |
||||||
|
* |
||||||
|
* @author Jens Schauder |
||||||
|
* @since 2.0 |
||||||
|
*/ |
||||||
|
public class BeforeUpdate extends BeforeSave implements WithId { |
||||||
|
|
||||||
|
/** |
||||||
|
* @param id of the entity about to get updated |
||||||
|
* @param instance the entity about to get updated. |
||||||
|
*/ |
||||||
|
public BeforeUpdate(Specified id, Object instance) { |
||||||
|
super(id, instance); |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,65 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2017 the original author or authors. |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package org.springframework.data.jdbc.mapping.event; |
||||||
|
|
||||||
|
import lombok.Data; |
||||||
|
import lombok.NonNull; |
||||||
|
|
||||||
|
import java.util.Optional; |
||||||
|
|
||||||
|
/** |
||||||
|
* Wrapper for an identifier of an entity. Might either be a {@link Specified} or {@link Unset#UNSET} |
||||||
|
* |
||||||
|
* @author Jens Schauder |
||||||
|
* @since 2.0 |
||||||
|
*/ |
||||||
|
public interface Identifier { |
||||||
|
|
||||||
|
Optional<Object> getOptionalValue(); |
||||||
|
|
||||||
|
static Identifier fromNullable(Object value) { |
||||||
|
return (value != null) ? new Specified(value) : Unset.UNSET; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* An unset identifier. Always returns {@link Optional#empty()} as value. |
||||||
|
*/ |
||||||
|
enum Unset implements Identifier { |
||||||
|
UNSET { |
||||||
|
@Override |
||||||
|
public Optional<Object> getOptionalValue() { |
||||||
|
return Optional.empty(); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* An {@link Identifier} guaranteed to have a non empty value. |
||||||
|
* |
||||||
|
* Since it is guaranteed to exist the value can get access directly. |
||||||
|
*/ |
||||||
|
@Data |
||||||
|
class Specified implements Identifier { |
||||||
|
|
||||||
|
@NonNull |
||||||
|
private final Object value; |
||||||
|
|
||||||
|
@Override |
||||||
|
public Optional<Object> getOptionalValue() { |
||||||
|
return Optional.of(value); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,31 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2017 the original author or authors. |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package org.springframework.data.jdbc.mapping.event; |
||||||
|
|
||||||
|
import java.util.Optional; |
||||||
|
|
||||||
|
/** |
||||||
|
* A {@link JdbcEvent} which is guaranteed to have an entity. |
||||||
|
* |
||||||
|
* @author Jens Schauder |
||||||
|
* @since 2.0 |
||||||
|
*/ |
||||||
|
public class JdbcEventWithEntity extends JdbcEvent implements WithEntity { |
||||||
|
|
||||||
|
public JdbcEventWithEntity(Identifier id, Object entity) { |
||||||
|
super(id, Optional.of(entity)); |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,33 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2017 the original author or authors. |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package org.springframework.data.jdbc.mapping.event; |
||||||
|
|
||||||
|
import java.util.Optional; |
||||||
|
|
||||||
|
import org.springframework.data.jdbc.mapping.event.Identifier.Specified; |
||||||
|
|
||||||
|
/** |
||||||
|
* A {@link JdbcEvent} guaranteed to have an identifier. |
||||||
|
* |
||||||
|
* @author Jens Schauder |
||||||
|
* @since 2.0 |
||||||
|
*/ |
||||||
|
public class JdbcEventWithId extends JdbcEvent implements WithId{ |
||||||
|
|
||||||
|
public JdbcEventWithId(Specified id, Optional<Object> entity) { |
||||||
|
super(id, entity); |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,36 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2017 the original author or authors. |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package org.springframework.data.jdbc.mapping.event; |
||||||
|
|
||||||
|
import lombok.Getter; |
||||||
|
|
||||||
|
import java.util.Optional; |
||||||
|
|
||||||
|
import org.springframework.data.jdbc.mapping.event.Identifier.Specified; |
||||||
|
|
||||||
|
/** |
||||||
|
* A {@link JdbcEvent} which is guaranteed to have an identifier and an entity. |
||||||
|
* |
||||||
|
* @author Jens Schauder |
||||||
|
* @since 2.0 |
||||||
|
*/ |
||||||
|
@Getter |
||||||
|
public class JdbcEventWithIdAndEntity extends JdbcEvent implements WithId, WithEntity { |
||||||
|
|
||||||
|
public JdbcEventWithIdAndEntity(Specified id, Object entity) { |
||||||
|
super(id, Optional.of(entity)); |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,30 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2017 the original author or authors. |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package org.springframework.data.jdbc.mapping.event; |
||||||
|
|
||||||
|
/** |
||||||
|
* Interface for {@link JdbcEvent}s which are guaranteed to have an entity. Allows direct access to that entity, without going through an {@link java.util.Optional} |
||||||
|
* |
||||||
|
* @author Jens Schauder |
||||||
|
* @since 2.0 |
||||||
|
*/ |
||||||
|
public interface WithEntity { |
||||||
|
|
||||||
|
default Object getEntity() { |
||||||
|
return ((JdbcEvent) this).getOptionalEntity() |
||||||
|
.orElseThrow(() -> new IllegalStateException("Entity must not be NULL")); |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,33 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2017 the original author or authors. |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package org.springframework.data.jdbc.mapping.event; |
||||||
|
|
||||||
|
import org.springframework.data.jdbc.mapping.event.Identifier.Specified; |
||||||
|
|
||||||
|
/** |
||||||
|
* Interface for {@link JdbcEvent}s which are guaranteed to have a {@link Specified} identifier. |
||||||
|
* |
||||||
|
* Offers direct access to the {@link Specified} identifier. |
||||||
|
* |
||||||
|
* @author Jens Schauder |
||||||
|
* @since 2.0 |
||||||
|
*/ |
||||||
|
public interface WithId { |
||||||
|
|
||||||
|
default Specified getSpecifiedId(){ |
||||||
|
return (Specified) ((JdbcEvent)this).getId(); |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,60 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2017 the original author or authors. |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package org.springframework.data.jdbc.mapping.model; |
||||||
|
|
||||||
|
import java.beans.PropertyDescriptor; |
||||||
|
import java.lang.reflect.Field; |
||||||
|
|
||||||
|
import org.springframework.data.mapping.Association; |
||||||
|
import org.springframework.data.mapping.PersistentEntity; |
||||||
|
import org.springframework.data.mapping.model.AnnotationBasedPersistentProperty; |
||||||
|
import org.springframework.data.mapping.model.SimpleTypeHolder; |
||||||
|
|
||||||
|
/** |
||||||
|
* Meta data about a property to be used by repository implementations. |
||||||
|
* |
||||||
|
* @author Jens Schauder |
||||||
|
* @since 2.0 |
||||||
|
*/ |
||||||
|
public class BasicJdbcPersistentProperty extends AnnotationBasedPersistentProperty<JdbcPersistentProperty> |
||||||
|
implements JdbcPersistentProperty { |
||||||
|
|
||||||
|
/** |
||||||
|
* Creates a new {@link AnnotationBasedPersistentProperty}. |
||||||
|
* |
||||||
|
* @param field must not be {@literal null}. |
||||||
|
* @param propertyDescriptor can be {@literal null}. |
||||||
|
* @param owner must not be {@literal null}. |
||||||
|
* @param simpleTypeHolder |
||||||
|
*/ |
||||||
|
public BasicJdbcPersistentProperty( //
|
||||||
|
Field field, //
|
||||||
|
PropertyDescriptor propertyDescriptor, //
|
||||||
|
PersistentEntity<?, JdbcPersistentProperty> owner, //
|
||||||
|
SimpleTypeHolder simpleTypeHolder //
|
||||||
|
) { |
||||||
|
super(field, propertyDescriptor, owner, simpleTypeHolder); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
protected Association<JdbcPersistentProperty> createAssociation() { |
||||||
|
return null; |
||||||
|
} |
||||||
|
|
||||||
|
public String getColumnName() { |
||||||
|
return getName(); |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,31 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2017 the original author or authors. |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package org.springframework.data.jdbc.repository; |
||||||
|
|
||||||
|
import org.springframework.dao.NonTransientDataAccessException; |
||||||
|
|
||||||
|
/** |
||||||
|
* Signals failure to set the id property of an entity. |
||||||
|
* |
||||||
|
* @author Jens Schauder |
||||||
|
* @since 2.0 |
||||||
|
*/ |
||||||
|
public class UnableToSetIdException extends NonTransientDataAccessException { |
||||||
|
|
||||||
|
public UnableToSetIdException(String message, Throwable cause) { |
||||||
|
super(message, cause); |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,42 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2017 the original author or authors. |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package org.springframework.data.jdbc.repository.support; |
||||||
|
|
||||||
|
import java.io.Serializable; |
||||||
|
|
||||||
|
import org.springframework.data.jdbc.mapping.model.JdbcPersistentEntity; |
||||||
|
import org.springframework.data.repository.core.support.PersistentEntityInformation; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author Jens Schauder |
||||||
|
* @since 2.0 |
||||||
|
*/ |
||||||
|
public class BasicJdbcPersistentEntityInformation<T, ID extends Serializable> extends PersistentEntityInformation<T, ID> |
||||||
|
implements JdbcPersistentEntityInformation<T, ID> { |
||||||
|
|
||||||
|
private final JdbcPersistentEntity<T> persistentEntity; |
||||||
|
|
||||||
|
public BasicJdbcPersistentEntityInformation(JdbcPersistentEntity<T> persistentEntity) { |
||||||
|
super(persistentEntity); |
||||||
|
|
||||||
|
this.persistentEntity = persistentEntity; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void setId(T instance, Object value) { |
||||||
|
persistentEntity.getPropertyAccessor(instance).setProperty(persistentEntity.getIdProperty(), value); |
||||||
|
} |
||||||
|
} |
||||||
@ -1,3 +1,5 @@ |
|||||||
-- noinspection SqlNoDataSourceInspectionForFile |
-- noinspection SqlNoDataSourceInspectionForFile |
||||||
|
|
||||||
CREATE TABLE ReadOnlyIdEntity (ID BIGINT GENERATED BY DEFAULT AS IDENTITY(START WITH 1) PRIMARY KEY, NAME VARCHAR(100)) |
CREATE TABLE ReadOnlyIdEntity (ID BIGINT GENERATED BY DEFAULT AS IDENTITY(START WITH 1) PRIMARY KEY, NAME VARCHAR(100)) |
||||||
|
|
||||||
|
CREATE TABLE PrimitiveIdEntity (ID BIGINT GENERATED BY DEFAULT AS IDENTITY(START WITH 1) PRIMARY KEY, NAME VARCHAR(100)) |
||||||
Loading…
Reference in new issue