mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#443 - ENH: Enum mapping - Add annotation @DbEnumValue that can be used to on a enum method which provides DB values
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package com.avaje.tests.basic.type;
|
||||
|
||||
import com.avaje.tests.model.basic.EBasicEnumInt;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -11,6 +12,9 @@ import com.avaje.tests.model.basic.EBasic;
|
||||
import com.avaje.tests.model.basic.EBasic.Status;
|
||||
import com.avaje.tests.model.basic.EBasicEnumId;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
public class TestEnumValueAnnotation extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
@@ -28,7 +32,7 @@ public class TestEnumValueAnnotation extends BaseTestCase {
|
||||
SqlRow sqlRow = q.findUnique();
|
||||
String strStatus = sqlRow.getString("status");
|
||||
|
||||
Assert.assertEquals("N", strStatus);
|
||||
assertEquals("N", strStatus);
|
||||
|
||||
EBasic b2 = new EBasic();
|
||||
b2.setName("Apple");
|
||||
@@ -40,9 +44,9 @@ public class TestEnumValueAnnotation extends BaseTestCase {
|
||||
b3.setName("Orange");
|
||||
|
||||
Ebean.save(b3);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAsId() {
|
||||
EBasicEnumId b = new EBasicEnumId();
|
||||
b.setName("Banana");
|
||||
@@ -56,7 +60,7 @@ public class TestEnumValueAnnotation extends BaseTestCase {
|
||||
SqlRow sqlRow = q.findUnique();
|
||||
String strStatus = sqlRow.getString("status");
|
||||
|
||||
Assert.assertEquals("N", strStatus);
|
||||
assertEquals("N", strStatus);
|
||||
|
||||
try {
|
||||
b = Ebean.find(EBasicEnumId.class, b.getStatus());
|
||||
@@ -64,7 +68,32 @@ public class TestEnumValueAnnotation extends BaseTestCase {
|
||||
Assert.fail("The use of an enum as id should work : " + iae.getLocalizedMessage());
|
||||
}
|
||||
|
||||
Assert.assertEquals(EBasicEnumId.Status.NEW, b.getStatus());
|
||||
assertEquals(EBasicEnumId.Status.NEW, b.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDbEnumValueInt() {
|
||||
|
||||
EBasicEnumInt b = new EBasicEnumInt();
|
||||
b.setName("Banana");
|
||||
b.setStatus(EBasicEnumInt.Status.NEW);
|
||||
|
||||
Ebean.save(b);
|
||||
|
||||
SqlQuery q = Ebean.createSqlQuery("select * from e_basic_eni where id = :id");
|
||||
q.setParameter("id", b.getId());
|
||||
|
||||
SqlRow sqlRow = q.findUnique();
|
||||
Integer intStatus = sqlRow.getInteger("status");
|
||||
|
||||
assertEquals(Integer.valueOf(1), intStatus);
|
||||
|
||||
|
||||
EBasicEnumInt b2 = Ebean.find(EBasicEnumInt.class)
|
||||
.where().eq("id", b.getId())
|
||||
.eq("status", EBasicEnumInt.Status.NEW)
|
||||
.findUnique();
|
||||
|
||||
assertNotNull(b2);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ package com.avaje.tests.model.basic;
|
||||
|
||||
import com.avaje.ebean.annotation.ChangeLog;
|
||||
import com.avaje.ebean.annotation.ChangeLogInsertMode;
|
||||
import com.avaje.ebean.annotation.EnumValue;
|
||||
import com.avaje.ebean.annotation.DbEnumValue;
|
||||
import com.avaje.ebean.annotation.JsonIgnore;
|
||||
import com.avaje.ebean.annotation.Where;
|
||||
import com.avaje.tests.model.basic.finder.CustomerFinder;
|
||||
@@ -36,14 +36,19 @@ public class Customer extends BasicDomain {
|
||||
* EnumValue is an Ebean specific mapping for enums.
|
||||
*/
|
||||
public enum Status {
|
||||
@EnumValue("N")
|
||||
NEW,
|
||||
NEW("N"),
|
||||
ACTIVE("A"),
|
||||
INACTIVE("I");
|
||||
|
||||
@EnumValue("A")
|
||||
ACTIVE,
|
||||
String dbValue;
|
||||
Status(String dbValue) {
|
||||
this.dbValue = dbValue;
|
||||
}
|
||||
|
||||
@EnumValue("I")
|
||||
INACTIVE
|
||||
@DbEnumValue
|
||||
public String getValue() {
|
||||
return dbValue;
|
||||
}
|
||||
}
|
||||
|
||||
@Transient
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
package com.avaje.tests.model.basic;
|
||||
|
||||
import com.avaje.ebean.annotation.DbEnumType;
|
||||
import com.avaje.ebean.annotation.DbEnumValue;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
import java.sql.Timestamp;
|
||||
|
||||
@Entity
|
||||
@Table(name = "e_basic_eni")
|
||||
public class EBasicEnumInt {
|
||||
|
||||
public enum Status {
|
||||
NEW("1"),
|
||||
ACTIVE("2"),
|
||||
INACTIVE("3");
|
||||
|
||||
String value;
|
||||
Status(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
@DbEnumValue(storage = DbEnumType.INTEGER)
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
@Id
|
||||
Integer id;
|
||||
|
||||
Status status;
|
||||
|
||||
String name;
|
||||
|
||||
String description;
|
||||
|
||||
Timestamp someDate;
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Status getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(Status status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public Timestamp getSomeDate() {
|
||||
return someDate;
|
||||
}
|
||||
|
||||
public void setSomeDate(Timestamp someDate) {
|
||||
this.someDate = someDate;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,11 +1,13 @@
|
||||
package com.avaje.tests.update;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.SqlQuery;
|
||||
import com.avaje.ebean.SqlRow;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class TestUpdatePartial extends BaseTestCase {
|
||||
|
||||
@@ -18,6 +20,7 @@ public class TestUpdatePartial extends BaseTestCase {
|
||||
c.setSmallnote("a note");
|
||||
|
||||
Ebean.save(c);
|
||||
checkDbStatusValue(c.getId(), "A");
|
||||
|
||||
Customer c2 = Ebean.find(Customer.class)
|
||||
.select("status, smallnote")
|
||||
@@ -28,6 +31,7 @@ public class TestUpdatePartial extends BaseTestCase {
|
||||
c2.setSmallnote("2nd note");
|
||||
|
||||
Ebean.save(c2);
|
||||
checkDbStatusValue(c.getId(), "I");
|
||||
|
||||
Customer c3 = Ebean.find(Customer.class)
|
||||
.select("status")
|
||||
@@ -38,7 +42,15 @@ public class TestUpdatePartial extends BaseTestCase {
|
||||
c3.setSmallnote("3rd note");
|
||||
|
||||
Ebean.save(c3);
|
||||
checkDbStatusValue(c.getId(), "N");
|
||||
}
|
||||
|
||||
private void checkDbStatusValue(Integer custId, String dbStatus) {
|
||||
SqlQuery sqlQuery = Ebean.createSqlQuery("select id, status from o_customer where id = ?");
|
||||
sqlQuery.setParameter(1, custId);
|
||||
SqlRow sqlRow = sqlQuery.findUnique();
|
||||
String status = sqlRow.getString("status");
|
||||
assertEquals(dbStatus, status);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -57,6 +69,6 @@ public class TestUpdatePartial extends BaseTestCase {
|
||||
Ebean.save(customerWithoutChanges);
|
||||
|
||||
// assert
|
||||
Assert.assertEquals(customer.getUpdtime().getTime(), customerWithoutChanges.getUpdtime().getTime());
|
||||
assertEquals(customer.getUpdtime().getTime(), customerWithoutChanges.getUpdtime().getTime());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user