2011-05-09 1 views
3

나는 엔터티가 아닌 개체의 동일한 인스턴스를 데이터베이스에 유지하지 않고 JPA 엔터티에 저장해야하는 재생 프레임 워크에서 응용 프로그램을 만들고 있습니다. 특수 효과를 사용하거나 사용하지 않을 수 있는지 여부를 알기 위해 내가 찾던의 샘플 코드는 다음과 같습니다JPA에 개체를 저장하지 않고 엔터티에 저장

public class anEntity extends Model { 
    @ManyToOne 
    public User user; 

    @ManyToOne 
    public Question question; 


    //Encrypted candidate name for the answer 
    @Column(columnDefinition = "text") 
    public BigInteger candidateName; 

    //I want that field not to be inserted into the database 
    TestObject p= new TestObject(); 

내가 @Embedded 주석하지만 시도는 엔티티 테이블에 객체 필드를 포함 할 예정. 개체 열을 엔티티 테이블에 숨긴 채로 @Embedded을 사용합니까?

+0

나는 항상 동일한 일시적인 인스턴스가 사용되어야한다는 것을 명확히하기 위해 질문을 편집하고 약간 대답했다. 당신이 신경 쓰지 않기를 바래 .. – fasseg

답변

7

체크 아웃 @Transient 주석 :

"..이 주석 속성 또는 필드가 지속되지 않도록 지정이 속성 또는 필드 엔티티 클래스의 매핑 슈퍼 클래스, 또는 임베드 클래스에 주석을 추가하는 데 사용됩니다"

public class anEntity extends Model { 
    @Transient 
    private TransientSingleton t; 

    public anEntity(){ // JPA calls this so you can use the constructor to set the transient instance. 
     super(); 
     t=TransientSingleton.getInstance(); 
    } 


public class TransientSingleton { // simple unsecure singleton from wikipedia 

    private static final TransientSingleton INSTANCE = new TransientSingleton(); 
    private TransientSingleton() { 
     [...do stuff..] 
    } 
    public static TransientSingleton getInstance() { 
     return INSTANCE; 
    } 
} 
: 그래서 이것은 트릭을 할해야

:

당신의 실체가 과도 객체를 설정하기 위해 getInstance() 방법을 사용할 수 있도록 항상 당신이 Singleton 패턴을 구현할 수있는 동일한 개체를 얻을 수 있는지 확인하려면

+0

나는 그것을 시도했지만 데이터베이스에서 엔티티을 가져온 후에 transient 오브젝트를 사용하려고 할 때 널 포인터 예외를 준다. – deadlock

+0

클래스에 Constroctur를 추가하고 거기에 Testobject를 설정합니다. 대답에서 지적한대로 ... – fasseg

+0

나는 그것을했다고 믿습니다! – deadlock