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 ();
}歡迎每一個貢獻,請遵循貢獻指南
查看我們的行為準則