fix: 实体类 ID 生成策略设置为 none 时,调用insertSelective() 方法传入一个 id 为 null 的对象会报错;close #I873OZ

This commit is contained in:
开源海哥 2023-10-17 11:16:55 +08:00
parent ec82caa582
commit 5e7ac5628b
4 changed files with 98 additions and 7 deletions

View File

@ -448,10 +448,12 @@ public class TableInfo {
public String[] obtainInsertColumns(Object entity, boolean ignoreNulls) {
if (!ignoreNulls) {
return ArrayUtil.concat(insertPrimaryKeys, columns);
} else {
}
// 忽略 null 字段
else {
MetaObject metaObject = EntityMetaObject.forObject(entity, reflectorFactory);
List<String> retColumns = new ArrayList<>();
for (String insertColumn : columns) {
for (String insertColumn : allColumns) {
if (onInsertColumns != null && onInsertColumns.containsKey(insertColumn)) {
retColumns.add(insertColumn);
} else {
@ -462,7 +464,7 @@ public class TableInfo {
retColumns.add(insertColumn);
}
}
return ArrayUtil.concat(insertPrimaryKeys, retColumns.toArray(new String[0]));
return retColumns.toArray(new String[0]);
}
}

View File

@ -396,6 +396,8 @@ public class TableInfoFactory {
tableInfo.setDefaultQueryColumns(defaultQueryColumns.toArray(new String[0]));
}
// 此处需要保证顺序先设置 PrimaryKey在设置其他 Column
// 否则会影响 SQL 的字段构建顺序
tableInfo.setPrimaryKeyList(idInfos);
tableInfo.setColumnInfoList(columnInfoList);

View File

@ -0,0 +1,78 @@
/*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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 com.mybatisflex.test;
import com.mybatisflex.annotation.ColumnMask;
import com.mybatisflex.annotation.Id;
import com.mybatisflex.annotation.KeyType;
import com.mybatisflex.annotation.Table;
import com.mybatisflex.core.mask.Masks;
import java.io.Serializable;
@Table(value = "tb_account", dataSource = "ds2", onSet = AccountOnSetListener.class)
public class Account6 extends BaseEntity implements Serializable, AgeAware {
private static final long serialVersionUID = 1L;
@Id(keyType = KeyType.None)
private Long id;
@ColumnMask(Masks.CHINESE_NAME)
private String userName;
private int age;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
@Override
public int getAge() {
return age;
}
@Override
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", userName='" + userName + '\'' +
", age=" + age +
'}';
}
}

View File

@ -30,6 +30,7 @@ import com.mybatisflex.core.util.UpdateEntity;
import com.mybatisflex.mapper.Account6Mapper;
import com.mybatisflex.mapper.ArticleMapper;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
@ -271,12 +272,20 @@ public class AccountTester {
Account6Mapper mapper = MybatisFlexBootstrap.getInstance()
.getMapper(Account6Mapper.class);
Account6 account = new Account6();
account.setUserName("michael");
account.setAge(5);
Account6 account1 = new Account6();
account1.setId(1L);
account1.setUserName("michael");
account1.setAge(5);
Assert.assertEquals(mapper.insertSelective(account1),1);
mapper.insertSelective(account);
Account6 account2 = new Account6();
// account2.setId(1L); 不设置主键
account2.setUserName("michael");
account2.setAge(5);
Assert.assertEquals(mapper.insertSelective(account2),1);
}