【不兼容】删掉javax.persistence.GeneratedValue功能
This commit is contained in:
@@ -30,7 +30,6 @@ public class DataCallAttribute implements Attribute<Object, Serializable> {
|
||||
Class cltmp = clazz;
|
||||
do {
|
||||
for (Field field : cltmp.getDeclaredFields()) {
|
||||
if (field.getAnnotation(javax.persistence.GeneratedValue.class) == null) continue;
|
||||
try {
|
||||
rs = Attribute.create(cltmp, field);
|
||||
attributes.put(clazz, rs);
|
||||
|
||||
@@ -114,21 +114,7 @@ public class DataJdbcSource extends DataSqlSource<Connection> {
|
||||
c1 += cc;
|
||||
}
|
||||
c = c1;
|
||||
}
|
||||
if (info.autoGenerated) { //由数据库自动生成主键值
|
||||
ResultSet set = prestmt.getGeneratedKeys();
|
||||
int i = -1;
|
||||
while (set.next()) {
|
||||
if (primaryType == int.class) {
|
||||
primary.set(entitys[++i], set.getInt(1));
|
||||
} else if (primaryType == long.class) {
|
||||
primary.set(entitys[++i], set.getLong(1));
|
||||
} else {
|
||||
primary.set(entitys[++i], set.getObject(1));
|
||||
}
|
||||
}
|
||||
set.close();
|
||||
}
|
||||
}
|
||||
prestmt.close();
|
||||
//------------------------------------------------------------
|
||||
if (info.isLoggable(logger, Level.FINEST)) { //打印调试信息
|
||||
@@ -166,10 +152,9 @@ public class DataJdbcSource extends DataSqlSource<Connection> {
|
||||
protected <T> PreparedStatement createInsertPreparedStatement(final Connection conn, final String sql,
|
||||
final EntityInfo<T> info, T... entitys) throws SQLException {
|
||||
Attribute<T, Serializable>[] attrs = info.insertAttributes;
|
||||
final PreparedStatement prestmt = info.autoGenerated ? conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS) : conn.prepareStatement(sql);
|
||||
final PreparedStatement prestmt = conn.prepareStatement(sql);
|
||||
|
||||
for (final T value : entitys) {
|
||||
if (info.autouuid) info.createPrimaryValue(value);
|
||||
batchStatementParameters(conn, prestmt, info, attrs, value);
|
||||
prestmt.addBatch();
|
||||
}
|
||||
|
||||
@@ -316,11 +316,6 @@ public abstract class DataSqlSource<DBChannel> extends AbstractService implement
|
||||
if (entitys.length == 0) return 0;
|
||||
checkEntity("insert", false, entitys);
|
||||
final EntityInfo<T> info = loadEntityInfo((Class<T>) entitys[0].getClass());
|
||||
if (info.autouuid) {
|
||||
for (T value : entitys) {
|
||||
info.createPrimaryValue(value);
|
||||
}
|
||||
}
|
||||
if (isOnlyCache(info)) return insertCache(info, entitys);
|
||||
return insertDB(info, entitys).whenComplete((rs, t) -> {
|
||||
if (t != null) {
|
||||
@@ -337,11 +332,6 @@ public abstract class DataSqlSource<DBChannel> extends AbstractService implement
|
||||
CompletableFuture future = checkEntity("insert", true, entitys);
|
||||
if (future != null) return future;
|
||||
final EntityInfo<T> info = loadEntityInfo((Class<T>) entitys[0].getClass());
|
||||
if (info.autouuid) {
|
||||
for (T value : entitys) {
|
||||
info.createPrimaryValue(value);
|
||||
}
|
||||
}
|
||||
if (isOnlyCache(info)) {
|
||||
return CompletableFuture.supplyAsync(() -> insertCache(info, entitys), getExecutor());
|
||||
}
|
||||
|
||||
@@ -154,12 +154,6 @@ public final class EntityInfo<T> {
|
||||
//Flipper.sort转换成以ORDER BY开头SQL的缓存
|
||||
private final Map<String, String> sortOrderbySqls = new ConcurrentHashMap<>();
|
||||
|
||||
//是否由数据库生成主键值
|
||||
final boolean autoGenerated;
|
||||
|
||||
//是否UUID主键
|
||||
final boolean autouuid;
|
||||
|
||||
//所属的DataSource
|
||||
final DataSource source;
|
||||
|
||||
@@ -287,8 +281,6 @@ public final class EntityInfo<T> {
|
||||
List<Attribute<T, Serializable>> insertattrs = new ArrayList<>();
|
||||
List<String> updatecols = new ArrayList<>();
|
||||
List<Attribute<T, Serializable>> updateattrs = new ArrayList<>();
|
||||
boolean auto = false;
|
||||
boolean uuid = false;
|
||||
Map<Class, Creator<CryptHandler>> cryptCreatorMap = new HashMap<>();
|
||||
do {
|
||||
for (Field field : cltmp.getDeclaredFields()) {
|
||||
@@ -318,19 +310,8 @@ public final class EntityInfo<T> {
|
||||
}
|
||||
if (field.getAnnotation(javax.persistence.Id.class) != null && idAttr0 == null) {
|
||||
idAttr0 = attr;
|
||||
GeneratedValue gv = field.getAnnotation(GeneratedValue.class);
|
||||
auto = gv != null;
|
||||
// if (gv != null && gv.strategy() != GenerationType.IDENTITY) {
|
||||
// throw new RuntimeException(cltmp.getName() + "'s @ID primary not a GenerationType.IDENTITY");
|
||||
// }
|
||||
if (gv != null && field.getType() == String.class) { //UUID
|
||||
uuid = true;
|
||||
auto = false;
|
||||
}
|
||||
if (!auto) {
|
||||
insertcols.add(sqlfield);
|
||||
insertattrs.add(attr);
|
||||
}
|
||||
insertcols.add(sqlfield);
|
||||
insertattrs.add(attr);
|
||||
} else {
|
||||
if (col == null || col.insertable()) {
|
||||
insertcols.add(sqlfield);
|
||||
@@ -456,8 +437,6 @@ public final class EntityInfo<T> {
|
||||
this.deleteNamesPrepareSQL = null;
|
||||
this.queryNamesPrepareSQL = null;
|
||||
}
|
||||
this.autoGenerated = auto;
|
||||
this.autouuid = uuid;
|
||||
//----------------cache--------------
|
||||
Cacheable c = type.getAnnotation(Cacheable.class);
|
||||
if (this.table == null || (!cacheForbidden && c != null && c.value())) {
|
||||
@@ -482,15 +461,6 @@ public final class EntityInfo<T> {
|
||||
return jsonConvert;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建主键值,目前只支持UUID赋值
|
||||
*
|
||||
* @param src Entity对象
|
||||
*/
|
||||
public void createPrimaryValue(T src) {
|
||||
if (autouuid) getPrimary().set(src, Utility.uuid());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Entity缓存器
|
||||
*
|
||||
@@ -536,14 +506,6 @@ public final class EntityInfo<T> {
|
||||
return table == null;
|
||||
}
|
||||
|
||||
public boolean isAutoGenerated() {
|
||||
return autoGenerated;
|
||||
}
|
||||
|
||||
public boolean isAutouuid() {
|
||||
return autouuid;
|
||||
}
|
||||
|
||||
public DistributeTableStrategy<T> getTableStrategy() {
|
||||
return tableStrategy;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user