Skip to content

Commit

Permalink
Merge pull request #17 from devezhao/n-details
Browse files Browse the repository at this point in the history
n-details
  • Loading branch information
devezhao authored May 19, 2023
2 parents d11432b + fde14cf commit 1fbf24c
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 14 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>cn.devezhao</groupId>
<artifactId>persist4j</artifactId>
<version>1.6.3</version>
<version>1.7.0</version>
<packaging>jar</packaging>

<name>persist4j</name>
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/cn/devezhao/persist4j/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,14 @@ public interface Entity extends BaseMeta {
* @return
*/
Entity getDetailEntity();


/**
* v1.7
*
* @return
*/
Entity[] getDetialEntities();

/**
* 获取字段名称
*
Expand Down
17 changes: 11 additions & 6 deletions src/main/java/cn/devezhao/persist4j/metadata/impl/EntityImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class EntityImpl extends BaseMetaObject implements Entity, Cloneable {
private List<Field> referenceTo = new ArrayList<>();

private Entity mainEntity;
private Entity detailEntity;
private Set<Entity> detailEntities = new HashSet<>();

public EntityImpl(String name, String physicalName, String description, JSONObject extraAttrs,
boolean creatable, boolean updatable, boolean queryable,
Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
}
}
33 changes: 27 additions & 6 deletions src/test/java/cn/devezhao/persist4j/metadata/MetadataTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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");
Expand All @@ -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()) {
Expand All @@ -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);
}
}
15 changes: 15 additions & 0 deletions src/test/resources/metadata-test.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,20 @@
<field name="week" type="string" />
<field name="count" type="string"/>
</entity>

<entity name="MainEntity" type-code="200">
<field name="id" type="primary"/>
<field name="name" type="string"/>
</entity>

<entity name="DetailEntity1" type-code="201" main="MainEntity">
<field name="id" type="primary"/>
<field name="name" type="string"/>
</entity>

<entity name="DetailEntity2" type-code="202" main="MainEntity">
<field name="id" type="primary"/>
<field name="name" type="string"/>
</entity>

</metadata-config>

0 comments on commit 1fbf24c

Please sign in to comment.