2011-10-25 3 views
2

저는 임베디드 모델 체계가 있습니다. 에이전트가 있으며 에이전트에는 에이전트가있는 도시가 있습니다. 임베디드 모델을 게임 프레임 워크에서 올바르게 사용하는 방법은 무엇입니까?

모델

이 찾고있는 다음

도시 :

package models; 

import java.util.*; 
import javax.persistence.*; 

import play.db.jpa.*; 
import play.data.validation.*; 

@Entity 
public class City extends Model { 
    @Required 
    @Column(unique=true) 
    public String name; 

    @ElementCollection 
    @CollectionTable(name="city_translation", 
     [email protected](name="city_id")) 
    @Column(name="translation") 
    @MapKeyColumn(name="language") 
    public Map<String, String> translations = new HashMap<String, String>(); 

    public City(String name) { 
     this.name = name; 
    } 

    public String toString() { 
     return this.name; 
    } 

    public void setTranslation(String language, String translation) { 
     translations.put(language, translation); 
    } 

    public String getTranslation(String language) { 
     return translations.get(language); 
    } 
} 

에이전트 :

package models; 

import java.util.*; 
import javax.persistence.*; 

import play.db.jpa.*; 
import play.data.validation.*; 

@Entity 
public class Agent extends Model { 
    @Required 
    @Column(unique=true) 
    public String name; 

    @Required 
    @Column(unique=true) 
    public String email; 

    @Required 
    public City city; 

    public Agent(String name, String email, City city) { 
     this.name = name; 
     this.email = email; 
     this.city = city; 
    } 
} 

에이전트 모델이 데이터베이스에 만들 때 문제가 (H2 : MEM) city ​​필드는 bigint 대신 varbinary (255)로 선언되므로 City ID를 보유 할 수 없습니다.

따라서 에이전트를 저장할 때 필드가 올바르게 전달되면 (html : agent.city가 선택된 도시 ID 임) params.get ("agent.city")에서 읽을 수 있지만 언제 agent.city이가 null 보유하고 나는 그것을 참조 할 :

public static save(Agent agent) { 
    Logger.info(params.get("agent.id")); // OK - for example: 1 
    if (agent.city == null) 
     Logger.info("agent.city is null"); // this is printed 
} 

나는 도시 클래스에 @Embeddable을 추가하고 에이전트의 도시 속성에 @Embedded 표기법을 추가하면 그때 컴파일에서이 오류가 시간 :

A JPA error occurred (Unable to build EntityManagerFactory): component property not found: id 

모델을 설정하는 방법에 대해 조언 해주십시오. correclty.

City 모델의 참조 ID가 필요하기 때문에 조인 테이블과 OneToOne 관계를 사용하지 않아도되지만 도시 모델은 독립적으로 유지되어야하며 다른 모델에서도 사용됩니다.

은 에이전트 엔티티의 도시 특성에 당신에게

+0

다른 모든 필드가 에이전트에 올바르게 입력 되었습니까? 나는 도시가 당신이 그것을 부르고있는 형태 (나는 추측한다)에서 바르게 모으는 지 궁금하게 생각하고있다. – emt14

+0

예 모든 다른 입력란은 괜찮습니다. 에이전트 필드 : id [bigint], 도시 [varbinary], 이메일 [varchar], 이름 [varchar]. 도시 : 아이디 [bigint], 이름 [varchar]. City_Translation : city_id [bigint], 번역 [varchar] – bdz

답변

1

사용 @OneToOne 감사드립니다. 도시 객체에서 OneToOne을 선언 할 필요는 없습니다. 이것은 관계가 단방향이지만 여전히 유효 함을 의미합니다.

+0

고맙습니다. 그게 내가 놓친 것입니다. – bdz

관련 문제