diff --git a/pom.xml b/pom.xml
index d736438..ce44102 100644
--- a/pom.xml
+++ b/pom.xml
@@ -5,7 +5,7 @@
cn.devezhao
persist4j
- 1.6.3
+ 1.7.0
jar
persist4j
diff --git a/src/main/java/cn/devezhao/persist4j/Entity.java b/src/main/java/cn/devezhao/persist4j/Entity.java
index 6b1b351..e36b284 100644
--- a/src/main/java/cn/devezhao/persist4j/Entity.java
+++ b/src/main/java/cn/devezhao/persist4j/Entity.java
@@ -85,7 +85,14 @@ public interface Entity extends BaseMeta {
* @return
*/
Entity getDetailEntity();
-
+
+ /**
+ * v1.7
+ *
+ * @return
+ */
+ Entity[] getDetialEntities();
+
/**
* 获取字段名称
*
diff --git a/src/main/java/cn/devezhao/persist4j/metadata/impl/EntityImpl.java b/src/main/java/cn/devezhao/persist4j/metadata/impl/EntityImpl.java
index b74fb43..24d41a9 100644
--- a/src/main/java/cn/devezhao/persist4j/metadata/impl/EntityImpl.java
+++ b/src/main/java/cn/devezhao/persist4j/metadata/impl/EntityImpl.java
@@ -32,7 +32,7 @@ public class EntityImpl extends BaseMetaObject implements Entity, Cloneable {
private List referenceTo = new ArrayList<>();
private Entity mainEntity;
- private Entity detailEntity;
+ private Set detailEntities = new HashSet<>();
public EntityImpl(String name, String physicalName, String description, JSONObject extraAttrs,
boolean creatable, boolean updatable, boolean queryable,
@@ -113,9 +113,14 @@ public Entity getMainEntity() {
@Override
public Entity getDetailEntity() {
- return detailEntity;
+ return detailEntities.isEmpty() ? null : getDetialEntities()[0];
}
-
+
+ @Override
+ public Entity[] getDetialEntities() {
+ return detailEntities.toArray(new Entity[0]);
+ }
+
@Override
public String[] getFieldNames() {
return fieldMap.keySet().toArray(new String[0]);
@@ -154,7 +159,7 @@ protected Object clone() throws CloneNotSupportedException {
clone.fieldSorted = new LinkedHashSet<>(this.fieldSorted);
clone.referenceTo = new ArrayList<>(this.referenceTo);
clone.mainEntity = null;
- clone.detailEntity = null;
+ clone.detailEntities = new HashSet<>();
return clone;
}
@@ -187,13 +192,13 @@ protected void addField(Field field) {
protected void addReferenceTo(Field field) {
referenceTo.add(field);
}
-
+
/**
* @param entity
*/
protected void setMainEntity(Entity entity) {
Assert.isNull(this.mainEntity, "Cannot reset `mainEntity`");
this.mainEntity = entity;
- ((EntityImpl) entity).detailEntity = this;
+ ((EntityImpl) entity).detailEntities.add(this);
}
}
diff --git a/src/test/java/cn/devezhao/persist4j/metadata/MetadataTest.java b/src/test/java/cn/devezhao/persist4j/metadata/MetadataTest.java
index 17e50e8..8a906ff 100644
--- a/src/test/java/cn/devezhao/persist4j/metadata/MetadataTest.java
+++ b/src/test/java/cn/devezhao/persist4j/metadata/MetadataTest.java
@@ -8,6 +8,8 @@
import org.apache.commons.lang.StringUtils;
import org.junit.Test;
+import java.util.Arrays;
+
/**
* @author zhaofang123@gmail.com
* @since 12/28/2018
@@ -16,8 +18,7 @@ public class MetadataTest {
@Test
public void testAnyReference() throws Exception {
- Dialect dialect = new MySQL5Dialect();
- MetadataFactory metadataFactory = new ConfigurationMetadataFactory("metadata-test.xml", dialect);
+ final MetadataFactory metadataFactory = createMetadataFactory();
Entity allTypes = metadataFactory.getEntity(100);
Field anyReference1 = allTypes.getField("tanyreference");
@@ -29,8 +30,7 @@ public void testAnyReference() throws Exception {
@Test
public void testReferenceToFields() throws Exception {
- Dialect dialect = new MySQL5Dialect();
- MetadataFactory metadataFactory = new ConfigurationMetadataFactory("metadata-test.xml", dialect);
+ final MetadataFactory metadataFactory = createMetadataFactory();
Entity test = metadataFactory.getEntity(102);
for (Field field : test.getReferenceToFields()) {
@@ -40,10 +40,31 @@ public void testReferenceToFields() throws Exception {
@Test
public void testCommon() {
- Dialect dialect = new MySQL5Dialect();
- MetadataFactory metadataFactory = new ConfigurationMetadataFactory("metadata-test.xml", dialect);
+ final MetadataFactory metadataFactory = createMetadataFactory();
Field commonReference = metadataFactory.getEntity("Test2").getField("commonReference");
System.out.println(commonReference);
}
+
+ @Test
+ public void testNDetails() {
+ final MetadataFactory metadataFactory = createMetadataFactory();
+
+ Entity main = metadataFactory.getEntity(200);
+ System.out.println(main.getDetailEntity());
+ System.out.println(Arrays.toString(main.getDetialEntities()));
+
+ Entity detail1 = metadataFactory.getEntity(201);
+ System.out.println(detail1.getMainEntity());
+
+ Entity detail2 = metadataFactory.getEntity(202);
+ System.out.println(detail2.getMainEntity());
+
+ System.out.println(detail1.getMainEntity() == (detail2.getMainEntity()));
+ }
+
+ static MetadataFactory createMetadataFactory() {
+ Dialect dialect = new MySQL5Dialect();
+ return new ConfigurationMetadataFactory("metadata-test.xml", dialect);
+ }
}
diff --git a/src/test/resources/metadata-test.xml b/src/test/resources/metadata-test.xml
index bbe29ee..dfdb65f 100644
--- a/src/test/resources/metadata-test.xml
+++ b/src/test/resources/metadata-test.xml
@@ -53,5 +53,20 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file