mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#668 - ENH: Add ability to register a custom IdGenerator
This commit is contained in:
@@ -7,12 +7,15 @@ import com.avaje.ebean.bean.BeanCollection;
|
||||
import com.avaje.ebean.common.BeanList;
|
||||
import com.avaje.ebean.config.ServerConfig;
|
||||
import com.avaje.tests.model.basic.EBasic;
|
||||
import com.avaje.tests.model.basic.ECustomId;
|
||||
import org.example.ModUuidGenerator;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class BeanFindControllerTest extends BaseTestCase {
|
||||
@@ -30,7 +33,9 @@ public class BeanFindControllerTest extends BaseTestCase {
|
||||
config.setDdlRun(true);
|
||||
config.setRegister(false);
|
||||
config.setDefaultServer(false);
|
||||
config.add(new ModUuidGenerator());
|
||||
config.getClasses().add(EBasic.class);
|
||||
config.getClasses().add(ECustomId.class);
|
||||
|
||||
EBasicFindController findController = new EBasicFindController();
|
||||
config.getFindControllers().add(findController);
|
||||
@@ -61,6 +66,10 @@ public class BeanFindControllerTest extends BaseTestCase {
|
||||
eBasic = list.get(0);
|
||||
assertEquals(Integer.valueOf(47), eBasic.getId());
|
||||
assertEquals("47", eBasic.getName());
|
||||
|
||||
ECustomId bean = new ECustomId("check");
|
||||
ebeanServer.save(bean);
|
||||
assertNotNull(bean.getId());
|
||||
}
|
||||
|
||||
static class EBasicFindController implements BeanFindController {
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.avaje.tests.idkeys;
|
||||
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.tests.model.basic.ECustomId;
|
||||
import org.avaje.moduuid.ModUUID;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
public class TestCustomId extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void insert() {
|
||||
|
||||
ECustomId bean = new ECustomId("fred");
|
||||
Ebean.save(bean);
|
||||
assertNotNull(bean.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void insert_when_hasValue() {
|
||||
|
||||
ECustomId bean = new ECustomId("gotId");
|
||||
String explicitId = ModUUID.newShortId();
|
||||
bean.setId(explicitId);
|
||||
Ebean.save(bean);
|
||||
|
||||
ECustomId found = Ebean.find(ECustomId.class, explicitId);
|
||||
assertEquals(found.getName(), bean.getName());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.avaje.tests.model.basic;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class ECustomId {
|
||||
|
||||
@Id @GeneratedValue(generator = "shortUid")
|
||||
String id;
|
||||
|
||||
String name;
|
||||
|
||||
public ECustomId(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package org.example;
|
||||
|
||||
import com.avaje.ebean.config.IdGenerator;
|
||||
import org.avaje.moduuid.ModUUID;
|
||||
|
||||
/**
|
||||
* A customer Id Generator that can be assigned by @GeneratedValue(generator="shortUid")
|
||||
*/
|
||||
public class ModUuidGenerator implements IdGenerator {
|
||||
|
||||
@Override
|
||||
public Object nextValue() {
|
||||
return ModUUID.newShortId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "shortUid";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user