Skip to content

Commit

Permalink
🎨 优化 ID 通过 EntityRef 注解关联,减少Id字段定义
Browse files Browse the repository at this point in the history
  • Loading branch information
TAKETODAY committed Feb 20, 2025
1 parent f525161 commit b456260
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ private static PropertyUpdateStrategy updateExcludeId(PropertyUpdateStrategy str
public int updateById(Object entity, Object id, @Nullable PropertyUpdateStrategy strategy) {
Assert.notNull(id, "Entity id is required");
EntityMetadata metadata = entityMetadataFactory.getEntityMetadata(entity.getClass());
EntityProperty idProperty = metadata.idProperty();
EntityProperty idProperty = idProperty(metadata, "Updating an entity, Id property not found");
Assert.isTrue(idProperty.property.isInstance(id), "Entity Id matches failed");

if (strategy == null) {
Expand Down Expand Up @@ -597,15 +597,13 @@ public int saveOrUpdate(Object entity, @Nullable PropertyUpdateStrategy strategy
public int delete(Class<?> entityClass, Object id) throws DataAccessException {
EntityMetadata metadata = entityMetadataFactory.getEntityMetadata(entityClass);

if (metadata.idProperty == null) {
throw new InvalidDataAccessApiUsageException("Deleting an entity, Id property not found");
}
EntityProperty idProperty = idProperty(metadata, "Deleting an entity, Id property not found");

StringBuilder sql = new StringBuilder();
sql.append("DELETE FROM ");
sql.append(metadata.tableName);
sql.append(" WHERE `");
sql.append(metadata.idProperty.columnName);
sql.append(idProperty.columnName);
sql.append("` = ? ");

if (stmtLogger.isDebugEnabled()) {
Expand All @@ -616,7 +614,7 @@ public int delete(Class<?> entityClass, Object id) throws DataAccessException {
PreparedStatement statement = null;
try {
statement = con.prepareStatement(sql.toString());
metadata.idProperty.setParameter(statement, 1, id);
idProperty.setParameter(statement, 1, id);
return statement.executeUpdate();
}
catch (SQLException ex) {
Expand Down Expand Up @@ -1168,6 +1166,17 @@ private boolean isNew(Object entity) throws IllegalEntityException {
return false;
}

private static EntityProperty idProperty(EntityMetadata metadata, String error) {
EntityProperty idProperty = metadata.idProperty;
if (idProperty == null) {
idProperty = metadata.refIdProperty;
if (idProperty == null) {
throw new InvalidDataAccessApiUsageException(error);
}
}
return idProperty;
}

//

private static int setParameters(Object entity, ArrayList<EntityProperty> properties, PreparedStatement statement) throws SQLException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ public EntityMetadata createEntityMetadata(Class<?> entityClass) {
ArrayList<EntityProperty> entityProperties = new ArrayList<>();

EntityProperty idProperty = null;
EntityProperty refIdProperty = null;
for (BeanProperty property : metadata) {
if (isFiltered(property)) {
continue;
Expand Down Expand Up @@ -163,16 +164,16 @@ public EntityMetadata createEntityMetadata(Class<?> entityClass) {
refMetadata = getRefMetadata(entityClass);
}
if (refMetadata != null) {
idProperty = refMetadata.idProperty;
refIdProperty = refMetadata.idProperty;
}
}

if (idProperty == null && entityProperties.isEmpty()) {
throw new IllegalEntityException("Cannot determine properties for entity: " + entityClass);
}

return new EntityMetadata(metadata, entityClass,
idProperty, tableName, beanProperties, columnNames, entityProperties);
return new EntityMetadata(metadata, entityClass, idProperty, tableName,
refIdProperty, beanProperties, columnNames, entityProperties);
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ public class EntityMetadata {
@Nullable
public final EntityProperty idProperty;

@Nullable
public final EntityProperty refIdProperty;

public final BeanProperty[] beanProperties;

public final String[] columnNames;
Expand All @@ -65,11 +68,12 @@ public class EntityMetadata {
private final HashMap<String, EntityProperty> propertyMap;

protected EntityMetadata(BeanMetadata root, Class<?> entityClass, @Nullable EntityProperty idProperty, String tableName,
List<BeanProperty> beanProperties, List<String> columnNames, List<EntityProperty> entityProperties) {
@Nullable EntityProperty refIdProperty, List<BeanProperty> beanProperties, List<String> columnNames, List<EntityProperty> entityProperties) {
this.root = root;
this.tableName = tableName;
this.idProperty = idProperty;
this.entityClass = entityClass;
this.refIdProperty = refIdProperty;
this.propertyMap = mapProperties(entityProperties);
this.idColumnName = idProperty != null ? idProperty.columnName : null;
this.columnNames = StringUtils.toStringArray(columnNames);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,8 @@ void updateById(RepositoryManager repositoryManager) {
.hasMessage("Entity Id matches failed");

assertThatThrownBy(() -> entityManager.updateById(new NoIdModel(), "errorId"))
.isInstanceOf(IllegalEntityException.class)
.hasMessage("ID property is required");
.isInstanceOf(InvalidDataAccessApiUsageException.class)
.hasMessage("Updating an entity, Id property not found");

UserModel update = UserModel.forId(1);

Expand Down

0 comments on commit b456260

Please sign in to comment.