두 모델 클래스 사이의 관계를 가져와야합니다. 예 : 데이터베이스에는 두 개의 테이블 table1과 table2가 있습니다. 여기서 table2는 외래 키 table1을가집니다. 이 테이블에 각각 매핑되는 테이블 1과 테이블 2의 두 클래스가 있으며 일대일 관계 매핑이 있습니다.최대 절전 모드에서 관계 매핑을 검색하는 방법은 무엇입니까?
이제 Java 코드에서 어떤 관계가 있는지 검색해야합니까? 즉 출력은 위의 예에서 일대일이어야합니다.
두 모델 클래스 사이의 관계를 가져와야합니다. 예 : 데이터베이스에는 두 개의 테이블 table1과 table2가 있습니다. 여기서 table2는 외래 키 table1을가집니다. 이 테이블에 각각 매핑되는 테이블 1과 테이블 2의 두 클래스가 있으며 일대일 관계 매핑이 있습니다.최대 절전 모드에서 관계 매핑을 검색하는 방법은 무엇입니까?
이제 Java 코드에서 어떤 관계가 있는지 검색해야합니까? 즉 출력은 위의 예에서 일대일이어야합니다.
나는이 개 최대 절전 모드 엔티티 클래스 사이의 연결 유형을 찾으려면이 데모를 준비했습니다. 다른 커뮤니티 사용자가이 답변을 게시하는 데 도움이 될 수 있습니다.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Map;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.metadata.ClassMetadata;
import org.hibernate.metadata.CollectionMetadata;
import org.hibernate.persister.collection.BasicCollectionPersister;
import org.hibernate.persister.collection.OneToManyPersister;
import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.type.ManyToOneType;
import org.hibernate.type.OneToOneType;
import org.hibernate.type.Type;
/**
*
* @author shruti
*/
public class RelationDemo {
public static void main(String[] args) throws Exception {
Configuration configuration = new Configuration().configure();
ServiceRegistryBuilder builder = new ServiceRegistryBuilder().applySettings(configuration.getProperties());
SessionFactory sessionfactory = configuration.buildSessionFactory(builder.buildServiceRegistry());
//1. Read class names
System.out.println("Enter first class names (make sure you enter fully qualified name): ");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
String class1 = bufferedReader.readLine();
System.out.println("Enter second class names (make sure you enter fully qualified name): ");
String class2 = bufferedReader.readLine();
ClassMetadata classMetadata = null;
//2. Create ClassMetaData for the first class name and check that both the classes exists.
try {
classMetadata = sessionfactory.getClassMetadata(Class.forName(class1));
Class.forName(class2);
} catch (ClassNotFoundException classNotFoundException) {
System.err.println("Invalid class name.\nHint: Enter fully qualified class names for eg. packagename.Classname");
classNotFoundException.printStackTrace(System.err);
return;
}
//3. Retrieve all collection metada for collection type properties
@SuppressWarnings("unchecked")
Map<String, CollectionMetadata> allCollectionMetadata = sessionfactory.getAllCollectionMetadata();
//Retrieve all properties of the first class
String[] propertyNames = classMetadata.getPropertyNames();
//Loop through the retrieved properties
for (String name : propertyNames) {
//Retrieve type of each property
Type type = classMetadata.getPropertyType(name.trim());
//Check if the type is association type
if (type.isAssociationType()) {
//Check if it is collection type.
if (type.isCollectionType()) {
//From retrieved collection metadata (Strp 3) get value of the property we are refering to.
CollectionMetadata collectionMetadata = allCollectionMetadata.get(class1 + "." + name);
//Check if the elements of the collection are of desiered type
if (collectionMetadata.getElementType().getName().trim().equals(class2)) {
System.out.println("Property Name: " + name);
//Check if the value is of type OneToManyPersister
if (collectionMetadata instanceof OneToManyPersister) {
System.out.println("ONE TO MANY TYPE");
return;
} //Check if the value is of type BasicCollectionPersister. Note that for many to many relationship it would return an object of type BasicCollectionPersister.
else if (collectionMetadata instanceof BasicCollectionPersister) {
if (((BasicCollectionPersister) collectionMetadata).isManyToMany()) {
System.out.println("MANY TO MANY");
}
}
}
} //If property is not a collection then retrieve the class of the type and check if it is the same as Second class.
else if (type.getReturnedClass().getTypeName().equals(class2)) {
System.out.println("Property Name: " + name);
if (type instanceof ManyToOneType) {
System.out.println("MANY TO ONE TYPE");
return;
} else if (type instanceof OneToOneType) {
System.out.println("ONE TO ONE TYPE");
return;
}
}
}
}
System.out.println("NO RELATIONSHIP FOUND BETWEEN GIVEN CLASSES");
}
}
Hibernate는 당신을 도울 메타 데이터 패키지 org.hibernate.metadta를 가지고있다. 당신은 엔티티의 컬렉션 정보를 사용할 수 표시 코드 아래에 시도 할 수 있습니다 https://docs.jboss.org/hibernate/orm/3.5/api/org/hibernate/metadata/ClassMetadata.html
를 참조하십시오 :
Map<String,CollectionMetadata> map = sessionFactory.getAllCollectionMetadata();
Set<Entry<String,CollectionMetadata>> set = map.entrySet();
for (Entry e : set) {
System.out.println(e.getKey());
System.out.println(e.getValue());
}