Skip to content

Commit

Permalink
fix no generatedKey
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreampie committed Jan 4, 2016
1 parent 8908a0c commit e8de8d9
Showing 1 changed file with 23 additions and 25 deletions.
48 changes: 23 additions & 25 deletions resty-orm/src/main/java/cn/dreampie/orm/Base.java
Original file line number Diff line number Diff line change
Expand Up @@ -449,15 +449,13 @@ private Object[] getPrimaryValues(TableMeta tableMeta) {
*/
protected void setGeneratedKey(PreparedStatement pst, TableMeta tableMeta) throws SQLException {
String generatedKey = tableMeta.getGeneratedKey();
if (!generatedKey.isEmpty()) {
boolean generated = tableMeta.getGenerator() == null && !generatedKey.isEmpty();
if (generated) {
if (get(generatedKey) == null) {
ResultSet rs = pst.getGeneratedKeys();
if (rs.next()) {
set(generatedKey, rs.getObject(1)); // It returns Long object for int colType
rs.close();
}
boolean generated = tableMeta.getGenerator() == null && !generatedKey.isEmpty();
if (generated) {
if (get(generatedKey) == null) {
ResultSet rs = pst.getGeneratedKeys();
if (rs.next()) {
set(generatedKey, rs.getObject(1)); // It returns Long object for int colType
rs.close();
}
}
}
Expand All @@ -468,19 +466,17 @@ protected void setGeneratedKey(PreparedStatement pst, TableMeta tableMeta) throw
*/
protected void setGeneratedKey(PreparedStatement pst, TableMeta tableMeta, List<? extends Entity> models) throws SQLException {
String generatedKey = tableMeta.getGeneratedKey();
if (!generatedKey.isEmpty()) {
boolean generated = tableMeta.getGenerator() == null && !generatedKey.isEmpty();
if (generated) {
ResultSet rs = pst.getGeneratedKeys();
for (Entity<?> model : models) {
if (model.get(generatedKey) == null) {
if (rs.next()) {
model.set(generatedKey, rs.getObject(1));
}
boolean generated = tableMeta.getGenerator() == null && !generatedKey.isEmpty();
if (generated) {
ResultSet rs = pst.getGeneratedKeys();
for (Entity<?> model : models) {
if (model.get(generatedKey) == null) {
if (rs.next()) {
model.set(generatedKey, rs.getObject(1));
}
}
rs.close();
}
rs.close();
}
}

Expand Down Expand Up @@ -684,11 +680,13 @@ public boolean save() {
if (tableMeta.isCached()) {
purgeCache();
}

String generatedKey = tableMeta.getGeneratedKey();
Generator generator = tableMeta.getGenerator();

boolean generated = tableMeta.getGenerator() == null && !generatedKey.isEmpty();
if (!generated && get(generatedKey) == null) {
set(generatedKey, tableMeta.getGenerator().generateKey());
boolean generated = generator == null && !generatedKey.isEmpty();
if (!generated && generator != null && get(generatedKey) == null) {
set(generatedKey, generator.generateKey());
}

DataSourceMeta dsm = getDataSourceMeta();
Expand Down Expand Up @@ -760,11 +758,11 @@ public boolean save(List<M> models) {
}

String generatedKey = tableMeta.getGeneratedKey();

//是否需要主键生成器生成值
boolean generated = tableMeta.getGenerator() == null && !generatedKey.isEmpty();
Generator generator = tableMeta.getGenerator();
if (!generated && get(generatedKey) == null) {

boolean generated = generator == null && !generatedKey.isEmpty();
if (!generated && generator != null && get(generatedKey) == null) {
firstModel.set(generatedKey, generator.generateKey());
}

Expand Down

0 comments on commit e8de8d9

Please sign in to comment.