#2326 - [ddl generator] Primary key constraint not generated for @IdClass

This commit is contained in:
rbygrave
2021-08-22 15:55:08 +12:00
parent b22791c15b
commit 09cb5da3d3
5 changed files with 180 additions and 1 deletions
@@ -273,7 +273,7 @@ public class ModelBuildPropertyVisitor extends BaseTablePropertyVisitor {
col.setDraftOnly(p.isDraftOnly());
col.setHistoryExclude(p.isExcludedFromHistory());
if (p.isId()) {
if (p.isId() || p.isImportedPrimaryKey()) {
col.setPrimaryKey(true);
if (p.getBeanDescriptor().isUseIdGenerator()) {
col.setIdentity(true);
@@ -0,0 +1,63 @@
package io.ebeaninternal.dbmigration.model.build;
import io.ebean.BaseTestCase;
import io.ebean.DatabaseFactory;
import io.ebean.config.DatabaseConfig;
import io.ebeaninternal.api.SpiEbeanServer;
import io.ebeaninternal.dbmigration.ddlgeneration.Helper;
import io.ebeaninternal.dbmigration.migration.Migration;
import io.ebeaninternal.dbmigration.migrationreader.MigrationXmlReader;
import io.ebeaninternal.dbmigration.model.CurrentModel;
import io.ebeaninternal.dbmigration.model.MTable;
import io.ebeaninternal.dbmigration.model.ModelContainer;
import org.junit.Test;
import org.tests.model.basic.CKeyAssoc;
import org.tests.model.basic.CKeyDetail;
import org.tests.model.basic.CKeyParent;
import org.tests.model.basic.CKeyParentId;
import org.tests.model.compositekeys.CKEmbId;
import org.tests.model.compositekeys.CKSiteUser;
import java.io.IOException;
import static org.assertj.core.api.Assertions.assertThat;
public class ModelBuild_compound_IdClassTest extends BaseTestCase {
private SpiEbeanServer createServer() {
DatabaseConfig config = new DatabaseConfig();
config.setName("h2");
config.loadFromProperties();
config.setName("h2other");
config.setDdlGenerate(false);
config.setDdlRun(false);
config.setDdlExtra(false);
config.setDefaultServer(false);
config.setRegister(false);
config.addClass(CKSiteUser.class);
config.addClass(CKEmbId.class);
return (SpiEbeanServer) DatabaseFactory.create(config);
}
@Test
public void test() throws IOException {
SpiEbeanServer ebeanServer = createServer();
try {
CurrentModel currentModel = new CurrentModel(ebeanServer);
ModelContainer model = currentModel.read();
MTable parent = model.getTable("cksite_user");
assertThat(parent).isNotNull();
assertThat(parent.primaryKeyColumns()).hasSize(2);
String apply = Helper.asText(this, "/assert/ModelBuild_compound_IdClassTest/apply.sql");
String createDdl = currentModel.getCreateDdl();
assertThat(createDdl).startsWith("-- Generated by ebean").endsWith(apply);
} finally {
ebeanServer.shutdown();
}
}
}
@@ -0,0 +1,53 @@
package org.tests.model.compositekeys;
import javax.persistence.Embeddable;
import java.util.Objects;
import java.util.UUID;
@Embeddable
public class CKEmbId {
private UUID siteId;
private UUID userId;
public CKEmbId(UUID siteId, UUID userId) {
this.siteId = siteId;
this.userId = userId;
}
public UUID getSiteId() {
return siteId;
}
public UUID getUserId() {
return userId;
}
@Override
public String toString() {
return "st:" + siteId + " ui:" + userId;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CKEmbId that = (CKEmbId) o;
return Objects.equals(siteId, that.siteId) && Objects.equals(userId, that.userId);
}
@Override
public int hashCode() {
return Objects.hash(siteId, userId);
}
/**
* Just simulating the hash from Objects.hash(...)
*/
int otherHash() {
int result = 31 + siteId.hashCode();
result = 31 * result + userId.hashCode();
return result;
}
}
@@ -0,0 +1,55 @@
package org.tests.model.compositekeys;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.Version;
import java.util.UUID;
@Entity
@IdClass(CKEmbId.class)
public class CKSiteUser {
@Id
private UUID siteId;
@Id
private UUID userId;
private String accessLevel;
@Version
private long version;
public CKSiteUser(String accessLevel, UUID siteId, UUID userId) {
this.accessLevel = accessLevel;
this.siteId = siteId;
this.userId = userId;
}
public UUID getSiteId() {
return siteId;
}
public void setSiteId(UUID siteId) {
this.siteId = siteId;
}
public UUID getUserId() {
return userId;
}
public void setUserId(UUID userId) {
this.userId = userId;
}
public long getVersion() {
return version;
}
public void setVersion(long version) {
this.version = version;
}
}
@@ -0,0 +1,8 @@
create table cksite_user (
site_id uuid not null,
user_id uuid not null,
access_level varchar(255),
version bigint not null,
constraint pk_cksite_user primary key (site_id,user_id)
);