DynamicRelations
1.0.0
在每个关系数据库中,您必须始终知道您的实体可能有哪些关系。但是有时这些关系是未知的,或者可能随时改变。通过动态关系,您可以在运行时添加或删除实体之间的自定义关系。
动态关系可以看作是具有固定输入(sourceObject)和动态输出(目标)的有向图。
流图LR
子图动态相关
方向LR
sourceObject->目标
结尾
例如,具有以下实体:
一个人可以有狗,两个Entites都可以拥有文件(人信息文件和狗信息文件)。现在,您可以向所有可能像这样的实体添加动态关系:
图TD;
人 - >狗;
人 - > person_document
狗 - > dog_document;
每个连接都是一个动态关系,将生成以下关系:
每个关系都有一个动态目标,这意味着您可以与任何其他实体建立关系。
在这种情况下,一个人有一只狗,都有文件,现在您可以在运行时更改关系(不改变您的实体或模型)。例如,您可以删除一个person_document(丢失):
图TD;
人 - >狗;
狗 - > dog_document;
<dependency>
<groupId>io.github.Mom0aut</groupId>
<artifactId>dynamic-relations</artifactId>
<version>1.0.4</version>
</dependency>
只需将@Relation添加到您现有的实体中,即可生成必要的动态关系实体。动态关系仅与@entity注释的分类合作!
@ Relation ( sourceClass = Person . class )
@ Entity
@ Getter
@ Setter
public class Person implements RelationIdentity {
@ Id
@ GeneratedValue ( strategy = GenerationType . IDENTITY )
private Long id ;
@ Override
public String getType () {
return "PersonType" ;
}
}实施关系身份,每个动态关系都需要一个长ID和一个可以定义的字符串类型。
@ Relation ( sourceClass = Person . class )
@ Entity
@ Getter
@ Setter
public class Person implements RelationIdentity {
@ Id
@ GeneratedValue ( strategy = GenerationType . IDENTITY )
private Long id ;
@ Override
public String getType () {
return "PersonType" ;
}
}导入春季启动应用程序中的drmconfig,以便您可以使用关系服务
@ SpringBootApplication
@ Import ( DrmConfig . class )
public class App {
public static void main ( String [] args ) {
SpringApplication . run ( App . class , args );
}
} @ Autowired
private RelationService relationService ;
void createRelation () {
Person person = new person ();
personDao . save ( person );
Dog dog = new Dog ();
dogDao . save ( dog );
//Dynamic Relation can only be created with persisted Entities!
RelationLink relationLinkPersonToDog = relationService . createRelation ( person , dog );
}动态关系只能使用持久的实体创建!
@ Autowired
private RelationService relationService ;
void deleteRelation () {
relationService . deleteRelation ( relationToBeDeleted );
} @ Autowired
private RelationService relationService ;
void findRelations () {
Person person = new person ();
personDao . save ( person );
Dog dog = new Dog ();
dogDao . save ( dog );
Document document = new Document ();
documentDaio . save ( document );
//Dynamic Relation can only be created with persisted Entities!
RelationLink relationLinkPersonToDog = relationService . createRelation ( person , dog );
RelationLink relationLinkPersonToDocument = relationService . createRelation ( person , document );
RelationLink relationLinkDogToDocument = relationService . createRelation ( dog , document );
//Return 1 Relation person -> dog
RelationLink foundRelation = relationService . findRelationBySourceObjectAndRelationIdentity ( person , dog );
//Returns 2 Relations person -> dog and person -> document
List < RelationLink > relationBySourcePerson = relationService . findRelationBySourceObject ( person );
//Returns 2 Relations from person -> document and dog -> document
Set < RelationLink > relationByTargetDocument = relationService . findRelationByTargetRelationIdentity ( document );
} @ Autowired
private RelationService relationService ;
void getSourceObject () {
RelationLink foundRelation = relationService . findRelationBySourceObjectAndRelationIdentity ( person , dog );
//Can be cast to Person because we know it is from Person.class
Person sourceObject = ( Person ) foundRelation . getSourceObject ();
}欢迎每一个贡献,请遵循贡献指南
查看我们的行为准则