2012-08-08 67 views
0
@Entity 
@Table(name = "artist") 
public class Artist implements java.io.Serializable{ 

    @Basic 
    @Column(name = "is_active",nullable=false) 
    private Boolean isActive = false; 

    @OneToMany(mappedBy = "artist", cascade = {CascadeType.ALL}, fetch = FetchType.LAZY) 
    private Set<Project> projects= new HashSet<Project>(); 
} 

初始化一個類成員與另一個

@Entity 
@Table(name = "project") 
public class Project implements java.io.Serializable{ 

    @ManyToOne(fetch = FetchType.LAZY) 
    @JoinColumn(name = "artist_id") 
    private Artist artist; 

    @Basic 
    @Column(name = "is_active",nullable=false) 
    private Boolean isActive = false; 
} 

我可以初始化在ProjectisActive就像爲了證明Project國旗

private Boolean isActive = ((getArtist()!=null)? getArtist().getIsActive(): false); 

的標誌得到由Artist初始化「以下在Project中定義成員的順序是否會導致isActive始終爲假?

回答

1

與聲明:

private Boolean isActive = ((getArtist()!=null)? getArtist().getIsActive(): false); 

的isActive將永遠是假的,因爲這兩個藝術家和isActive將爲項目的默認構造函數之前進行初始化。因此,藝術家被初始化爲null,然後isActive被初始化爲false(因爲getArtist()爲null),那麼Project運行的默認構造函數(但構造函數什麼也不做),所以isActive總是爲false。