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

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-107-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 @@ -74,7 +74,7 @@ protected Association<JdbcPersistentProperty> createAssociation() {
* @see org.springframework.data.jdbc.mapping.model.JdbcPersistentProperty#getColumnName()
*/
public String getColumnName() {
return getName();
return this.context.getNamingStrategy().getColumnName(this);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not the only place where we need to use the naming strategy. In SqlGenerator.cascadeConditions we use the table name of a referenced table and use it as a column name for a foreign key column. This should be controllable by the NamingStrategy as well.

Note that the FK reference goes in the opposite direction than the property (at least in the cases we have right now).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That whole class appears to do entity.getTableName() and entity.getIdColumn which DOES use the naming strategy via BasicJdbcPersistentProperty.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was aiming at something else but didn't really explain it. The current version works fine for now. I'll create a separate ticket.

}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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;

/**
* Basic implementation of {@link NamingStrategy} with no schema, table based on {@link Class} and
* column name based on {@link JdbcPersistentProperty}.
*
* NOTE: Can also be used as an adapter. Create an anonymous subclass and override any settings to implement
* a different strategy on the fly.
*
* @author Greg Turnquist
*/
public class DefaultNamingStrategy implements NamingStrategy {

/**
* No schema at all!
*/
@Override
public String getSchema() {
return "";
}

/**
* Look up the {@link Class}'s simple name.
*/
@Override
public String getTableName(Class<?> type) {
return type.getSimpleName();
}


/**
* Look up the {@link JdbcPersistentProperty}'s name.
*/
@Override
public String getColumnName(JdbcPersistentProperty property) {
return property.getName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import static java.util.Arrays.*;

import lombok.Getter;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.temporal.Temporal;
Expand Down Expand Up @@ -46,7 +48,11 @@ public class JdbcMappingContext extends AbstractMappingContext<JdbcPersistentEnt
Temporal.class //
));

public JdbcMappingContext() {
private final @Getter NamingStrategy namingStrategy;

public JdbcMappingContext(NamingStrategy namingStrategy) {

this.namingStrategy = namingStrategy;
setSimpleTypeHolder(new SimpleTypeHolder(CUSTOM_SIMPLE_TYPES, true));
}

Expand Down Expand Up @@ -80,7 +86,7 @@ public List<PropertyPath> referencedEntities(Class<?> rootType, PropertyPath pat
*/
@Override
protected <T> JdbcPersistentEntity<T> createPersistentEntity(TypeInformation<T> typeInformation) {
return new JdbcPersistentEntityImpl<>(typeInformation);
return new JdbcPersistentEntityImpl<>(typeInformation, this.namingStrategy);
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,20 @@
class JdbcPersistentEntityImpl<T> extends BasicPersistentEntity<T, JdbcPersistentProperty>
implements JdbcPersistentEntity<T> {

private final NamingStrategy namingStrategy;
private final @Getter String tableName;

/**
* Creates a new {@link JdbcPersistentEntityImpl} for the given {@link TypeInformation}.
*
* @param information must not be {@literal null}.
*/
JdbcPersistentEntityImpl(TypeInformation<T> information) {
JdbcPersistentEntityImpl(TypeInformation<T> information, NamingStrategy namingStrategy) {

super(information);

tableName = getType().getSimpleName();
this.namingStrategy = namingStrategy;
this.tableName = this.namingStrategy.getQualifiedTableName(getType());
}

/*
Expand All @@ -49,7 +51,7 @@ class JdbcPersistentEntityImpl<T> extends BasicPersistentEntity<T, JdbcPersisten
*/
@Override
public String getIdColumn() {
return getRequiredIdProperty().getName();
return this.namingStrategy.getColumnName(getRequiredIdProperty());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -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.model;

/**
* @author Greg Turnquist
*/
public interface NamingStrategy {

String getSchema();

String getTableName(Class<?> type);

String getColumnName(JdbcPersistentProperty property);

default String getQualifiedTableName(Class<?> type) {
return this.getSchema() + (this.getSchema().equals("") ? "" : ".") + this.getTableName(type);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@
*/
package org.springframework.data.jdbc.repository.support;

import lombok.RequiredArgsConstructor;

import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.jdbc.core.JdbcEntityTemplate;
import org.springframework.data.jdbc.mapping.model.BasicJdbcPersistentEntityInformation;
import org.springframework.data.jdbc.mapping.model.JdbcMappingContext;
import org.springframework.data.jdbc.mapping.model.JdbcPersistentEntity;
import org.springframework.data.jdbc.mapping.model.JdbcPersistentEntityInformation;
import org.springframework.data.jdbc.mapping.model.NamingStrategy;
import org.springframework.data.jdbc.repository.SimpleJdbcRepository;
import org.springframework.data.repository.core.EntityInformation;
import org.springframework.data.repository.core.RepositoryInformation;
Expand All @@ -34,13 +33,19 @@
* @author Jens Schauder
* @since 2.0
*/
@RequiredArgsConstructor
public class JdbcRepositoryFactory extends RepositoryFactorySupport {

private final JdbcMappingContext context = new JdbcMappingContext();
private final JdbcMappingContext context;
private final NamedParameterJdbcOperations jdbcOperations;
private final ApplicationEventPublisher publisher;

public JdbcRepositoryFactory(NamedParameterJdbcOperations namedParameterJdbcOperations, ApplicationEventPublisher publisher, NamingStrategy namingStrategy) {

this.jdbcOperations = namedParameterJdbcOperations;
this.publisher = publisher;
this.context = new JdbcMappingContext(namingStrategy);
}

@SuppressWarnings("unchecked")
@Override
public <T, ID> EntityInformation<T, ID> getEntityInformation(Class<T> aClass) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.jdbc.mapping.model.DefaultNamingStrategy;
import org.springframework.data.jdbc.mapping.model.NamingStrategy;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
import org.springframework.data.repository.core.support.TransactionalRepositoryFactoryBeanSupport;
Expand All @@ -45,9 +47,12 @@ public class JdbcRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extend
"No unique NamedParameterJdbcOperation could be found, " //
+ "nor JdbcOperations or DataSource to construct one from.";

private static final String NO_NAMING_STRATEGY_ERROR_MESSAGE = "No unique NamingStrategy could be found.";

private static final String NAMED_PARAMETER_JDBC_OPERATIONS_BEAN_NAME = "namedParameterJdbcTemplate";
private static final String JDBC_OPERATIONS_BEAN_NAME = "jdbcTemplate";
private static final String DATA_SOURCE_BEAN_NAME = "dataSource";
private static final String NAMING_STRATEGY_BEAN_NAME = "namingStrategy";

private final ApplicationEventPublisher applicationEventPublisher;
private final ApplicationContext context;
Expand All @@ -62,7 +67,7 @@ public class JdbcRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extend

@Override
protected RepositoryFactorySupport doCreateRepositoryFactory() {
return new JdbcRepositoryFactory(findOrCreateJdbcOperations(), applicationEventPublisher);
return new JdbcRepositoryFactory(findOrCreateJdbcOperations(), applicationEventPublisher, findOrCreateNamingStrategy());
}

private NamedParameterJdbcOperations findOrCreateJdbcOperations() {
Expand All @@ -75,6 +80,12 @@ private NamedParameterJdbcOperations findOrCreateJdbcOperations() {
.orElseThrow(() -> new IllegalStateException(NO_NAMED_PARAMETER_JDBC_OPERATION_ERROR_MESSAGE));
}

private NamingStrategy findOrCreateNamingStrategy() {

return getNamingStrategy()
.orElse(new DefaultNamingStrategy());
}

private Optional<NamedParameterJdbcOperations> getNamedParameterJdbcOperations() {
return getBean(NamedParameterJdbcOperations.class, NAMED_PARAMETER_JDBC_OPERATIONS_BEAN_NAME);
}
Expand All @@ -87,6 +98,10 @@ private Optional<DataSource> getDataSource() {
return getBean(DataSource.class, DATA_SOURCE_BEAN_NAME);
}

private Optional<NamingStrategy> getNamingStrategy() {
return getBean(NamingStrategy.class, NAMING_STRATEGY_BEAN_NAME);
}

private <R> Optional<R> getBean(Class<R> type, String name) {

Map<String, R> beansOfType = context.getBeansOfType(type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.annotation.Id;
import org.springframework.data.jdbc.mapping.model.DefaultNamingStrategy;
import org.springframework.data.jdbc.mapping.model.JdbcMappingContext;
import org.springframework.data.jdbc.testing.TestConfiguration;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
Expand Down Expand Up @@ -253,7 +254,7 @@ Class<?> testClass() {
JdbcEntityOperations operations(ApplicationEventPublisher publisher,
NamedParameterJdbcOperations namedParameterJdbcOperations) {

return new JdbcEntityTemplate(publisher, namedParameterJdbcOperations, new JdbcMappingContext());
return new JdbcEntityTemplate(publisher, namedParameterJdbcOperations, new JdbcMappingContext(new DefaultNamingStrategy()));
}
}
}
Loading