#1739 - Entity with just @Id and @OneToMany fields produces broken insert query

This commit is contained in:
rob bygrave
2019-06-30 19:37:49 +12:00
parent ed71e106df
commit eba521563a
4 changed files with 69 additions and 2 deletions
@@ -177,8 +177,12 @@ public final class InsertMeta {
request.setInsertSetMode();
request.append("insert into ").append(table);
request.append(" (");
if (nullId && noColumnsForInsert(draftTable)) {
request.append(" default values");
return request.toString();
}
request.append(" (");
if (!nullId) {
id.dmlAppend(request);
}
@@ -200,8 +204,16 @@ public final class InsertMeta {
request.append(") values (");
request.append(request.getInsertBindBuffer());
request.append(")");
return request.toString();
}
/**
* Return true if the insert actually contains no columns.
*/
private boolean noColumnsForInsert(boolean draftTable) {
return shadowFKey == null
&& discriminator == null
&& (draftTable ? all.isEmpty() : allExcludeDraftOnly.isEmpty());
}
}
@@ -33,6 +33,10 @@ public class BindableList implements Bindable {
return new BindableList(copy);
}
public boolean isEmpty() {
return items.length == 0;
}
@Override
public boolean isDraftOnly() {
return false;
@@ -0,0 +1,19 @@
package org.tests.insert;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class OnlyIdEntity {
@Id
long id;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
}
@@ -0,0 +1,32 @@
package org.tests.insert;
import io.ebean.DB;
import io.ebean.Database;
import io.ebean.annotation.ForPlatform;
import io.ebean.annotation.Platform;
import org.ebeantest.LoggedSqlCollector;
import org.junit.Test;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
public class TestOnlyIdEntity {
@ForPlatform({Platform.H2, Platform.POSTGRES})
@Test
public void insert() {
final Database database = DB.getDefault();
LoggedSqlCollector.start();
OnlyIdEntity bean = new OnlyIdEntity();
database.save(bean);
List<String> sql = LoggedSqlCollector.stop();
assertThat(bean.getId()).isGreaterThan(0);
assertThat(sql).hasSize(1);
assertThat(sql.get(0)).contains("insert into only_id_entity default values");
}
}