0

두 개의 엔티티가 있으며 아래 코드를 찾으십시오. 간략하게 보일러 플레이트 코드가 생략되었습니다.기본 테이블의 기본 키를 종속 테이블의 외래 키 필드로 전달해야합니다.

public class Customer implements java.io.Serializable { 
    private static final long serialVersionUID = 3116894694769321104L; 
    private short customerId; 
    private Address address; 
    private String firstName; 
    private String lastName; 
    private String email; 
    private boolean active; 
    private Date createDate; 
    private Date lastUpdate; 


    // Property accessors 
    @Id 
    @Column(name="customer_id", unique=true, nullable=false, insertable=true, updatable=true) 

    public short getCustomerId() { 
     return this.customerId; 
    } 

    public void setCustomerId(short customerId) { 
     this.customerId = customerId; 
    } 
    @ManyToOne(cascade={CascadeType.ALL}, 
     fetch=FetchType.LAZY) 

     @JoinColumn(name="address_id", unique=false, nullable=false, insertable=true, updatable=true) 

    public Address getAddress() { 
     return this.address; 
    } 

    public void setAddress(Address address) { 
     this.address = address; 
    } 

및 주소 클래스입니다 : 내가 주소 테이블에서 외래 키로 고객 ID를 지속 할 필요가

public class Address implements java.io.Serializable { 


    // Fields  

    private short addressId; 
    private short customerId; 
    private String address; 
    private String address2; 
    private String district; 
    private String postalCode; 
    private String phone; 
    private Date lastUpdate; 
    private Set<Customer> customers_1 = new HashSet<Customer>(0); 


    // Constructors 

    /** default constructor */ 
    public Address() { 
    } 

    // Property accessors 
    @Id 
    @Column(name="address_id", unique=true, nullable=false, insertable=true, updatable=true) 

    public short getAddressId() { 
     return this.addressId; 
    } 

    public void setAddressId(short addressId) { 
     this.addressId = addressId; 
    } 

    /** 
    * ??????what goes here 
    */ 
    public short getCustomerId() { 
     return customerId; 
    } 

    /** 
    * @param customerId the customerId to set 
    */ 
    public void setCustomerId(short customerId) { 
     this.customerId = customerId; 
    } 

.

답변

1

@ManyToOne 관계를 Customer과 그냥 사용하십시오. 그래서, customerId 대신에 자바 코드에서 Customer 객체로 동작하지만, 데이터베이스 레벨에서 Hibernate는 고객과 테이블에 외래 키를 사용할 것입니다.

관련 문제