【不兼容】删掉javax.persistence.GeneratedValue功能

This commit is contained in:
Redkale
2019-09-16 16:35:41 +08:00
parent 824a6df55a
commit fca13557df
5 changed files with 4 additions and 131 deletions

View File

@@ -1,63 +0,0 @@
/** *****************************************************************************
* Copyright (c) 2008 - 2013 Oracle Corporation. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0
* which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Linda DeMichiel - Java Persistence 2.1
* Linda DeMichiel - Java Persistence 2.0
*
***************************************************************************** */
package javax.persistence;
import java.lang.annotation.Target;
import java.lang.annotation.Retention;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
/**
* Provides for the specification of generation strategies for the
* values of primary keys.
*
* <p>
* The <code>GeneratedValue</code> annotation
* may be applied to a primary key property or field of an entity or
* mapped superclass in conjunction with the {@link Id} annotation.
* The use of the <code>GeneratedValue</code> annotation is only
* required to be supported for simple primary keys. Use of the
* <code>GeneratedValue</code> annotation is not supported for derived
* primary keys.
*
* <pre>
*
* Example 1:
*
* &#064;Id
* &#064;GeneratedValue(strategy=SEQUENCE, generator="CUST_SEQ")
* &#064;Column(name="CUST_ID")
* public Long getId() { return id; }
*
* Example 2:
*
* &#064;Id
* &#064;GeneratedValue(strategy=TABLE, generator="CUST_GEN")
* &#064;Column(name="CUST_ID")
* Long id;
* </pre>
*
* @see Id
*
* @since Java Persistence 1.0
*/
@Target({METHOD, FIELD})
@Retention(RUNTIME)
public @interface GeneratedValue {
}

View File

@@ -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);

View File

@@ -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();
}

View File

@@ -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());
}

View File

@@ -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;
}