모든 관계형 데이터베이스에서는 항상 귀하의 엔티티에 어떤 관계가 있는지 알아야합니다. 그러나 때때로 이러한 관계는 알려지지 않았거나 언제든지 변경 될 수 있습니다. 동적 관계를 사용하면 런타임 동안 엔티티간에 사용자 정의 관계를 추가하거나 삭제할 수 있습니다.
동적 관계는 고정 된 입력 (SourceObject) 및 동적 출력 (대상)을 갖춘 방향 그래프로 볼 수 있습니다.
흐름도 LR
하위 그래프 동적 관계
방향 LR
sourceObject-> 대상
끝
예를 들어 다음 엔티티의 경우 :
사람은 개를 가질 수 있으며 두 앙트는 문서를 가질 수 있습니다 (개인 정보 문서 및 개 정보 문서). 이제 다음과 같이 보일 수있는 모든 엔티티에 동적 관계를 추가 할 수 있습니다.
그래프 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" ;
}
}Spring Boot 애플리케이션에서 DRMConfig를 가져 와서 RelationService를 사용할 수 있습니다.
@ 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 ();
}모든 기부금을 환영합니다. 기여 가이드 라인을 따르십시오
우리의 행동 강령을 참조하십시오