mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-12 22:41:35 +00:00
NEW: PersistVisitor
This commit is contained in:
1 parent
f045d5e1b0
commit
50c612f8ec
8 files changed
+282
-2
No files matched your search
@@ -293,6 +293,11 @@ public class TDSpiServer implements SpiServer {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitSave(Object start, PersistVisitor visitor) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean delete(Object bean) throws OptimisticLockException {
|
||||
return false;
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
package org.tests.basic;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import io.ebean.DB;
|
||||
import io.ebean.PersistVisitor;
|
||||
import io.ebean.bean.EntityBean;
|
||||
import io.ebean.plugin.Property;
|
||||
/**
|
||||
* Sample persist visitor that converts the visited beans in a XML-like structure
|
||||
*/
|
||||
public class TPersistVisitor implements PersistVisitor {
|
||||
|
||||
private final StringBuilder sb;
|
||||
private final String indent;
|
||||
private final String tag;
|
||||
private boolean empty = true;
|
||||
|
||||
public TPersistVisitor() {
|
||||
this(new StringBuilder(), "", "root");
|
||||
}
|
||||
private TPersistVisitor(StringBuilder sb, String indent, String tag) {
|
||||
this.sb = sb;
|
||||
this.indent = indent;
|
||||
this.tag = tag;
|
||||
this.sb.append(indent).append('<').append(tag);
|
||||
}
|
||||
|
||||
TPersistVisitor newVisitor(String tag) {
|
||||
if (empty) {
|
||||
sb.append(">\n");
|
||||
empty = false;
|
||||
}
|
||||
return new TPersistVisitor(sb, indent + " ", tag);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitEnd() {
|
||||
if (empty) {
|
||||
sb.append("/>\n");
|
||||
} else {
|
||||
this.sb.append(indent).append("</").append(tag).append(">\n");
|
||||
}
|
||||
}
|
||||
|
||||
TPersistVisitor attr(String attr, Object value) {
|
||||
sb.append(' ').append(attr).append('=').append('\'').append(value).append('\'');
|
||||
return this;
|
||||
}
|
||||
|
||||
public TPersistVisitor visitBean(EntityBean bean) {
|
||||
return newVisitor("bean").attr("type", bean.getClass().getSimpleName()).attr("newOrDirty", DB.beanState(bean).isNewOrDirty());
|
||||
}
|
||||
|
||||
@Override
|
||||
public PersistVisitor visitProperty(Property prop) {
|
||||
return newVisitor("property").attr("name", prop.name());
|
||||
}
|
||||
|
||||
@Override
|
||||
public PersistVisitor visitCollection(Collection<?> collection) {
|
||||
return newVisitor("collection").attr("size", collection.size());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
@@ -3,12 +3,15 @@ package org.tests.basic;
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Query;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.MRole;
|
||||
import org.tests.model.basic.MUser;
|
||||
|
||||
public class TestM2MCascadeOne extends BaseTestCase {
|
||||
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
@@ -30,8 +33,31 @@ public class TestM2MCascadeOne extends BaseTestCase {
|
||||
u1.addRole(r0);
|
||||
u1.addRole(r1);
|
||||
|
||||
TPersistVisitor tv = new TPersistVisitor();
|
||||
DB.visitSave(u1, tv);
|
||||
assertThat(tv.toString()).isEqualTo("<root>\n"
|
||||
+ " <bean type='MUser' newOrDirty='false'>\n"
|
||||
+ " <property name='roles'>\n"
|
||||
+ " <collection size='2'>\n"
|
||||
+ " <bean type='MRole' newOrDirty='false'/>\n"
|
||||
+ " <bean type='MRole' newOrDirty='true'/>\n"
|
||||
+ " </collection>\n"
|
||||
+ " </property>\n"
|
||||
+ " </bean>\n"
|
||||
+ "</root>\n");
|
||||
|
||||
DB.save(u1);
|
||||
|
||||
|
||||
u1 = DB.find(MUser.class, u.getUserid());
|
||||
|
||||
tv = new TPersistVisitor();
|
||||
DB.visitSave(u1, tv);
|
||||
// collection is unloaded
|
||||
assertThat(tv.toString()).isEqualTo("<root>\n"
|
||||
+ " <bean type='MUser' newOrDirty='false'/>\n"
|
||||
+ "</root>\n");
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in new issue
Block a user