mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#1818 - Cannot auto-generate UUID
This commit is contained in:
+6
@@ -27,6 +27,8 @@ public class GeneratedPropertyFactory {
|
||||
|
||||
private final HashSet<String> numberTypes = new HashSet<>();
|
||||
|
||||
private final UuidGeneratedProperty generatedUuid = new UuidGeneratedProperty();
|
||||
|
||||
private final GeneratedWhoModified generatedWhoModified;
|
||||
|
||||
private final GeneratedWhoCreated generatedWhoCreated;
|
||||
@@ -123,6 +125,10 @@ public class GeneratedPropertyFactory {
|
||||
return idGeneratorMap.get(generatorName);
|
||||
}
|
||||
|
||||
public void setUuid(DeployBeanProperty prop) {
|
||||
prop.setGeneratedProperty(generatedUuid);
|
||||
}
|
||||
|
||||
/**
|
||||
* Wraps the custom IdGenerator to implement PlatformIdGenerator.
|
||||
*/
|
||||
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
package io.ebeaninternal.server.deploy.generatedproperty;
|
||||
|
||||
import io.ebean.bean.EntityBean;
|
||||
import io.ebeaninternal.server.deploy.BeanProperty;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class UuidGeneratedProperty implements GeneratedProperty {
|
||||
|
||||
@Override
|
||||
public Object getInsertValue(BeanProperty prop, EntityBean bean, long now) {
|
||||
return UUID.randomUUID();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getUpdateValue(BeanProperty prop, EntityBean bean, long now) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean includeInInsert() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDDLNotNullable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean includeInUpdate() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean includeInAllUpdates() {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -202,12 +202,11 @@ public class AnnotationFields extends AnnotationParser {
|
||||
prop.setDbColumn(dbColumn);
|
||||
}
|
||||
|
||||
Id id = get(prop, Id.class);
|
||||
GeneratedValue gen = get(prop, GeneratedValue.class);
|
||||
if (gen != null) {
|
||||
readGenValue(gen, prop);
|
||||
readGenValue(gen, id, prop);
|
||||
}
|
||||
|
||||
Id id = get(prop, Id.class);
|
||||
if (id != null) {
|
||||
readIdScalar(prop);
|
||||
}
|
||||
@@ -519,8 +518,13 @@ public class AnnotationFields extends AnnotationParser {
|
||||
return util.createDataEncryptSupport(table, column);
|
||||
}
|
||||
|
||||
private void readGenValue(GeneratedValue gen, DeployBeanProperty prop) {
|
||||
|
||||
private void readGenValue(GeneratedValue gen, Id id, DeployBeanProperty prop) {
|
||||
if (id == null) {
|
||||
if (UUID.class.equals(prop.getPropertyType())) {
|
||||
generatedPropFactory.setUuid(prop);
|
||||
return;
|
||||
}
|
||||
}
|
||||
descriptor.setIdGeneratedValue();
|
||||
String genName = gen.generator();
|
||||
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
package org.tests.insert;
|
||||
|
||||
import io.ebean.Model;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import java.util.UUID;
|
||||
|
||||
@Entity
|
||||
public class EIdUidBean extends Model {
|
||||
|
||||
@Id
|
||||
private long id;
|
||||
|
||||
@GeneratedValue
|
||||
@Column(unique = true)
|
||||
private UUID uuid;
|
||||
|
||||
String name;
|
||||
|
||||
public EIdUidBean(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public UUID getUuid() {
|
||||
return uuid;
|
||||
}
|
||||
|
||||
public void setUuid(UUID uuid) {
|
||||
this.uuid = uuid;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package org.tests.insert;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestInsertWithIdAndUuid extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void insert() {
|
||||
|
||||
EIdUidBean bean = new EIdUidBean("one");
|
||||
bean.save();
|
||||
|
||||
assertThat(bean.getId()).isGreaterThan(0);
|
||||
assertThat(bean.getUuid()).isNotNull();
|
||||
|
||||
final EIdUidBean found = DB.find(EIdUidBean.class, bean.getId());
|
||||
|
||||
assertThat(bean.getId()).isEqualTo(found.getId());
|
||||
assertThat(bean.getUuid()).isEqualTo(found.getUuid());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user