2011-09-23 3 views
0

이상한 DB 구조로 작업해야하는데, 생산시 더 많은 테이블이 있지만 동일한 열이있을 수 있습니다. 엔티티/JPQL 쿼리를 사용하여 JPA (Hibernate)를 처리하고 싶습니다. 아이디어는 런타임에 JVM 내에서 각 테이블에 대한 엔티티 클래스를 동적으로 생성하는 것입니다. 이 접근법은 잘 작동합니다. 나는와 Javassist와 엔티티를 생성 - 이미 존재하는 컴파일 된 개체를 사용하고 동적으로 (엔티티, 표) 여기에 주석을 추가 :자동 dyscovery에 의한 동적으로 생성 된 엔티티의 로딩

public Class generateGroupStateEntity(String className,String tableName) { 
    ClassPool cp = ClassPool.getDefault(); 
    InputStream is = new FileInputStream(new File("MyTemplateEntity.class")); 
    CtClass c = cp.makeClass(is); 
    c.setName(className); 
    ClassFile cf = c.getClassFile(); 
    ConstPool constPool = cf.getConstPool(); 


    AnnotationsAttribute annotAtr = new AnnotationsAttribute(constPool,AnnotationsAttribute.visibleTag); 
    Annotation annot = new Annotation("javax.persistence.Entity", constPool); 
    annotAtr.addAnnotation(annot); 

    Annotation annotTable = new Annotation("javax.persistence.Table", constPool); 
    annotTable.addMemberValue("name", new StringMemberValue(tableName,cf.getConstPool())); 
    annotAtr.addAnnotation(annotTable); 

    cf.addAttribute(annotAtr); 
    return c.toClass(); 
    } 

내가 구성 객체에서 손으로 하이버 네이트 SessionFactory를 구축,이 클래스를 추가 구성에 addAnnotatedClass 메소드를 사용하면 정상적으로 작동합니다. 자동 발견에 문제가 있습니다. JPA 나 AnnotationSessionFactoryBean에 의해 엔티티가 발견/발견되지 않습니다 - 스프링 컨텍스트 내에서 패키지 스캐닝을 사용할 때. ("GetEyntaxException : [MyEntity]가 매핑되지 않았습니다"예외)

Class.forName (myNewEntityName)은 Classloader 내에서 초기화됨을 의미합니다.

(물론 나는이 이러한 테이블 처리를위한 이상적인 디자인 후두둑을하지 않는 것을 알고있다.)

문제는 왜, 어떻게 유일한 있나요?

(와 Javassist 3.15.0-GA, 최대 절전 모드 코어 3.6.6.Final, 3.5.6-최종-EntityManager의 최대 절전 모드) 내가 패키지 검사가 클래스 경로에서 항아리와 폴더를 어떻게 생각

+0

내 첫 번째로로드 된 클래스 로더에 문제가있을 수 있습니까? – csviri

답변

2

을 -하지에 클래스 로더 레벨. 이것은 모든 클래스를 메모리에로드하지 않도록 최적화하는 것입니다. 이 article에는 수행 방법에 대한 세부 정보가 있습니다.

문제를 해결하려면 AnnotationSessionFactoryBean을 확장하고 클래스 목록을 동적으로 추가해야 할 수 있습니다.

+0

예, 이미 동적 클래스를 추가하여 구현했지만 봄에 스캔을 구현할 것입니다.하지만 아마도 이것이 문제입니다. 고마워 – csviri

관련 문제