org.springframework.data.relational.repository.support.MappingRelationalEntityInformation Maven / Gradle / Ivy
Show all versions of spring-data-relational Show documentation
/*
* Copyright 2018-2023 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
*
* https://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.relational.repository.support;
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
import org.springframework.data.relational.core.sql.SqlIdentifier;
import org.springframework.data.relational.repository.query.RelationalEntityInformation;
import org.springframework.data.repository.core.support.PersistentEntityInformation;
import org.springframework.lang.Nullable;
/**
* {@link RelationalEntityInformation} implementation using a {@link RelationalPersistentEntity} instance to lookup the
* necessary information. Can be configured with a custom table name.
*
* Entity types that do not declare an explicit Id type fall back to {@link Long} as Id type.
*
* @author Mark Paluch
*/
public class MappingRelationalEntityInformation extends PersistentEntityInformation
implements RelationalEntityInformation {
private final RelationalPersistentEntity entityMetadata;
private final @Nullable SqlIdentifier customTableName;
private final Class fallbackIdType;
/**
* Creates a new {@link MappingRelationalEntityInformation} for the given {@link RelationalPersistentEntity}.
*
* @param entity must not be {@literal null}.
*/
public MappingRelationalEntityInformation(RelationalPersistentEntity entity) {
this(entity, null, null);
}
/**
* Creates a new {@link MappingRelationalEntityInformation} for the given {@link RelationalPersistentEntity} and
* fallback identifier type.
*
* @param entity must not be {@literal null}.
* @param fallbackIdType can be {@literal null}.
*/
public MappingRelationalEntityInformation(RelationalPersistentEntity entity, @Nullable Class fallbackIdType) {
this(entity, null, fallbackIdType);
}
/**
* Creates a new {@link MappingRelationalEntityInformation} for the given {@link RelationalPersistentEntity} and
* custom table name.
*
* @param entity must not be {@literal null}.
* @param customTableName can be {@literal null}.
*/
public MappingRelationalEntityInformation(RelationalPersistentEntity entity, String customTableName) {
this(entity, customTableName, null);
}
/**
* Creates a new {@link MappingRelationalEntityInformation} for the given {@link RelationalPersistentEntity},
* collection name and identifier type.
*
* @param entity must not be {@literal null}.
* @param customTableName can be {@literal null}.
* @param idType can be {@literal null}.
*/
@SuppressWarnings("unchecked")
private MappingRelationalEntityInformation(RelationalPersistentEntity entity, @Nullable String customTableName,
@Nullable Class idType) {
super(entity);
this.entityMetadata = entity;
this.customTableName = customTableName == null ? null : SqlIdentifier.quoted(customTableName);
this.fallbackIdType = idType != null ? idType : (Class) Long.class;
}
/* (non-Javadoc)
* @see org.springframework.data.relational.repository.query.RelationalEntityInformation#getTableName()
*/
public SqlIdentifier getTableName() {
return customTableName == null ? entityMetadata.getTableName() : customTableName;
}
public String getIdAttribute() {
return entityMetadata.getRequiredIdProperty().getName();
}
/* (non-Javadoc)
* @see org.springframework.data.repository.core.support.PersistentEntityInformation#getIdType()
*/
@Override
public Class getIdType() {
if (this.entityMetadata.hasIdProperty()) {
return super.getIdType();
}
return fallbackIdType;
}
}